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

thread同步测试

时间:2022-11-09 13:34:31浏览次数:44  
标签:同步 thread number 信号量 互斥 测试 pthread sem NULL

题目

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

运行截图

修改后

原来的代码中NUM的值为5,可知以前资源数为5,而只有一个消费者线程。用20201221%3+4=5,这里我们需要创建5个消费者线程。故我们需要添加互斥信号量mutex,加上头文件#include<unistd.h>,另外PV操作都要相应地加上对mutex互斥信号量的操作。创建线程时pthread_create( &cid1, NULL, consumer, NULL);语句要扩充为5个。

点击查看代码
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <semaphore.h>
#include <unistd.h>

#define NUM 3//资源的数量为3
int queue[NUM];
sem_t blank_number, product_number,mutex;//设置缓冲区空白位置数量、缓冲区产品数量、互斥信号量

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

	for ( ;; ) {
		sem_wait( &blank_number );//是否对生产者阻塞
		sem_wait( &mutex);//占用互斥信号量,互斥信号量-1
		queue[p] = rand() % 1000;//等待队列
		printf("Product %d \n", queue[p]);
		p = (p+1) % NUM;//因为资源只有3个,计数后还需要mod 3
		sleep ( rand() % 5);//休眠
		sem_post(&mutex); //释放互斥信号量
		sem_post( &product_number );//是否唤醒消费者
	}
}
void *consumer ( void * arg )
{

	static int c = 0;
	for( ;; ) {
		sem_wait( &product_number );//是否对消费者进行阻塞
    	sem_wait(&mutex);//互斥信号量 
		printf("Consume %d\n", queue[c]);
		c = (c+1) % NUM;//同理,资源数只有3个
		sleep( rand() % 5 );
		sem_post(&mutex);//互斥信号量
		sem_post( &blank_number );//是否唤醒生产者
	}
}

int main(int argc, char *argv[] )
{
	pthread_t pid, cid1,cid2,cid3,cid4,cid5;//创建5个消费者线程
	sem_init( &blank_number, 0, NUM );//初始化空闲缓冲区信号量
	sem_init( &product_number, 0, 0);//初始化产品信号量
	sem_init( &mutex, 1, 1);//初始化互斥信号量
	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_create( &cid5, NULL, consumer, NULL);//这里创建了5个消费者线程
	pthread_join( pid, NULL );
	pthread_join( cid1, NULL );//等待消费者线程执行完毕
	pthread_join( cid2, NULL );
	pthread_join( cid3, NULL );
	pthread_join( cid4, NULL );
	pthread_join( cid5, NULL );//5个消费者线程执行完毕
	sem_destroy( &blank_number );//销毁信号量
	sem_destroy( &product_number );
	return 0;
}



标签:同步,thread,number,信号量,互斥,测试,pthread,sem,NULL
From: https://www.cnblogs.com/1482156703optimus/p/16873312.html

相关文章

  • 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;......
  • thread互斥测试
    截图编译结果说明互斥锁,是一种信号量,常用来防止两个进程或线程在同一时刻访问相同的共享资源。可以保证以下三点:原子性:把一个互斥量锁定为一个原子操作,这意味着操作......
  • thread互斥测试
    thread互斥测试编译运行附件中的代码,并说明程序的功能根据自己的理解,提交不少于3张图片代码#include<stdio.h>#include<stdlib.h>#include<pthread.h>#incl......
  • 测试左移与右移
    大家熟悉的测试工作(也是传统的瀑布式),是接到项目后参与需求评审,然后根据需求文档写写用例和准备脚本,等开发提测之后正式开始测试、提bug、回归,测试通过后就结束了,项目交给......
  • thread同步测试
    thread同步测试1编译运行附件中的代码,提交运行结果截图,并说明程序功能2修改代码,把同步资源个数减少为3个,把使用资源的线程增加到(你的学号%3+4)个,编译代码,提交修改后......
  • thread同步测试
    任务详情1编译运行附件中的代码,提交运行结果截图,并说明程序功能2修改代码,把同步资源个数减少为3个,把使用资源的线程增加到(你的学号%3+4)个,编译代码,提交修改后的代码......
  • thread互斥测试
    编译运行附件中的代码,并说明程序的功能根据自己的理解,提交不少于3张图片程序功能:通过thread互斥来查看两个文件中字符串的数量,一个空格分开算两个,第一个zx.txt文件先获得......
  • 线程同步-读者写者问题(多线程)
    任务描述:0推荐在openEuer上实现1描述操作系统中“读者-写者”问题,理解问题的本质,提交你理解或查找到的文本资料2利用多线程完成reader和writer3在main中测试若干个......
  • report 鼠标键盘报告描述符测试
     0x05,0x01,//USAGE_PAGE(GenericDesktop)0x09,0x06,//USAGE(Keyboard)0xa1,0x01,//COLLECTION(Application)0x85,0x01,//ReportID(1)......