首页 > 其他分享 >thread同步测试

thread同步测试

时间:2022-11-09 14:26:07浏览次数:45  
标签:同步 thread blank number NUM 测试 pthread sem NULL

thread同步测试

任务

1 编译运行附件中的代码,提交运行结果截图,并说明程序功能
2 修改代码,把同步资源个数减少为3个,把使用资源的线程增加到 (你的学号%3 + 4)个,编译代码,提交修改后的代码和运行结果截图。

代码

附件代码

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <semaphore.h>

#define NUM 5
int queue[NUM];
sem_t blank_number, product_number;

void *producer ( void * arg )
{
	static int p = 0;

	for ( ;; ) {
		sem_wait( &blank_number );
		queue[p] = rand() % 1000;
		printf("Product %d \n", queue[p]);
		p = (p+1) % NUM;
		sleep ( rand() % 5);
		sem_post( &product_number );
	}
}
void *consumer ( void * arg )
{

	static int c = 0;
	for( ;; ) {
		sem_wait( &product_number );
		printf("Consume %d\n", queue[c]);
		c = (c+1) % NUM;
		sleep( rand() % 5 );
		sem_post( &blank_number );
	}
}

int main(int argc, char *argv[] )
{
	pthread_t pid, cid;
    
	sem_init( &blank_number, 0, NUM );
	sem_init( &product_number, 0, 0);
	pthread_create( &pid, NULL, producer, NULL);
	pthread_create( &cid, NULL, consumer, NULL);
	pthread_join( pid, NULL );
	pthread_join( cid, NULL );
	sem_destroy( &blank_number );
	sem_destroy( &product_number );
	return 0;
}

修改代码

20201226%3+4=4,所以消费者线程增加到个数为4,且需要互斥地去访问产品(最多为三个)

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <semaphore.h>

#define NUM 3
int queue[NUM];
sem_t blank_number, product_number;

void *producer ( void * arg )
{
	static int p = 0;

	for ( ;; ) {
		sem_wait( &blank_number );
		queue[p] = rand() % 1000;
		printf("Product %d \n", queue[p]);
		p = (p+1) % NUM;
		sleep ( rand() % 5);
		sem_post( &product_number );
	}
}
void *consumer ( void * arg )
{

	static int c = 0;
	for( ;; ) {
		sem_wait( &product_number );
		printf("Consume %d\n", queue[c]);
		c = (c+1) % NUM;
		sleep( rand() % 5 );
		sem_post( &blank_number );
	}
}

int main(int argc, char *argv[] )
{
	pthread_t pid, cid1,cid2,cid3,cid4,cid5,cid6;
    
	sem_init( &blank_number, 0, NUM );
	sem_init( &product_number, 0, 0);
	pthread_create( &pid, NULL, producer, NULL);
	pthread_create( &cid1, NULL, consumer, NULL);
	pthread_create( &cid2, NULL, consumer, NULL);
	pthread_create( &cid3, NULL, consumer, NULL);
	pthread_create( &cid4, NULL, consumer, NULL);
	pthread_join( pid, NULL );
	pthread_join( cid1, NULL );
	pthread_join( cid2, NULL );
	pthread_join( cid3, NULL );
	pthread_join( cid4, NULL );
	sem_destroy( &blank_number );
	sem_destroy( &product_number );
	return 0;
}

截图

附件截图

修改之后的截图

程序功能

功能:每一个生产者都要把自己生产的产品放入缓冲池,每个消费者从缓冲池中取走产品消费。在这种情况下,生产者消费者进程同步,因为只有通过互通消息才知道是否能存入产品或者取走产品。他们之间也存在互斥,即生产者消费者必须互斥访问缓冲池,即不能有两个以上的进程同时进行。

标签:同步,thread,blank,number,NUM,测试,pthread,sem,NULL
From: https://www.cnblogs.com/marryj/p/16873442.html

相关文章

  • thread互斥测试
    thread互斥测试#include<stdio.h>#include<stdlib.h>#include<pthread.h>#include<ctype.h>structarg_set{ char*fname; intcount;};structarg......
  • Java中线程的基本操作以及Thread和Runnable两种实现的比较
    线程的定义:某一时间点执行的处理,是操作系统能够进行运算调度的最小单位。一条线程是某一进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务......
  • JPA中关于外键设计的测试和思考
    一、项目构建(Maven工程)Pom.xml文件<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/......
  • thread互斥测试
    题目要求编译运行附件中的代码,并说明程序的功能根据自己的理解,提交不少于3张图片代码#include<stdio.h>#include<stdlib.h>#include<pthread.h>#include<ct......
  • thread同步测试
    thread同步测试#include<stdio.h>#include<pthread.h>#include<stdlib.h>#include<semaphore.h>#defineNUM5intqueue[NUM];sem_tblank_number,product_nu......
  • 线程同步-读者写者问题(多线程)
    多线程通信之读者、写者问题读、写问题是另一个非常出名的同步问题,常常用来模拟数据库的数据查询和数据修改两种情况问题。也即,一个数据库允许有多个访问者同时对其进行......
  • thread同步测试
    任务说明1.编译运行附件中的代码,提交运行结果截图,并说明程序功能2.修改代码,把同步资源个数减少为3个,把使用资源的线程增加到(你的学号%3+4)个,编译代码,提交修改后的代码......
  • thread同步测试
    题目1编译运行附件中的代码,提交运行结果截图,并说明程序功能2修改代码,把同步资源个数减少为3个,把使用资源的线程增加到(你的学号%3+4)个,编译代码,提交修改后的代码和运......
  • thread互斥测试
    thread互斥编译运行附件中的代码,并说明程序的功能根据自己的理解,提交不少于3张图片代码#include<stdio.h>#include<stdlib.h>#include<pthread.h>#include<......
  • thread互斥测试
    一、实验截图二、实验代码#include<stdio.h>#include<stdlib.h>#include<pthread.h>#include<ctype.h>structarg_set{ char*fname; intcount;......