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

thread同步测试

时间:2022-11-09 13:35:35浏览次数:70  
标签:同步 thread number 原语 NUM 测试 pthread sem NULL

任务说明

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

原理理解

PV原语

PV原语通过操作信号量来处理进程间的同步与互斥的问题。其核心就是一段不可分割不可中断的程序。 其基本思路是用一种新的变量类型(semaphore)来记录当前可用资源的数量。
semaphore有两种实现方式:

  • semaphore的取值必须大于或等于0。0表示当前已没有空闲资源,而正数表示当前空闲资源的数量;
  • semaphore的取值可正可负,负数的绝对值表示正在等待进入临界区的进程个数。

信号量是由操作系统来维护的,用户进程只能通过初始化和两个标准原语(P、V原语)来访问。初始化可指定一个非负整数,即空闲资源总数。

  • P原语:阻塞原语,负责把当前进程由运行状态转换为阻塞状态,直到另外一个进程唤醒它。操作为:申请一个空闲资源(把信号量减1),若成功,则退出;若失败,则该进程被阻塞;
  • V原语:唤醒原语,负责把一个被阻塞的进程唤醒,它有一个参数表,存放着等待被唤醒的进程信息。操作为:释放一个被占用的资源(把信号量加1),如果发现有被阻塞的进程,则选择一个唤醒之。

P原语操作的动作是:

sem减1;
若sem减1后仍大于或等于零,则进程继续执行;
若sem减1后小于零,则该进程被阻塞后进入与该信号相对应的队列中,然后转进程调度。
V原语操作的动作是:

V原语操作的动作是:

sem加1;
若相加结果大于零,则进程继续执行;
若相加结果小于或等于零,则从该信号的等待队列中唤醒一等待进程,然后再返回原进程继续执行或转进程调度。

原文链接:https://blog.csdn.net/s2152637/article/details/102466060

编译原代码

原始代码如下:

#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;
}

运行结果截图如下:

修改代码

原来的代码中NUM的值为5,可知以前资源数为5,而只有一个消费者线程。用20201213%3+4=6,这里我们需要创建4个消费者线程。故我们需要添加互斥信号量mutex,加上头文件#include<unistd.h>,另外PV操作都要相应地加上对mutex互斥信号量的操作。创建线程时pthread_create( &cid1, NULL, consumer, NULL);语句要扩充为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_create( &cid5, NULL, consumer, NULL);
	pthread_create( &cid6, NULL, consumer, NULL);
	pthread_join( pid, NULL );
	pthread_join( cid1, NULL );
	pthread_join( cid2, NULL );
	pthread_join( cid3, NULL );
	pthread_join( cid4, NULL );
	pthread_join( cid5, NULL );
	pthread_join( cid6, NULL );
	sem_destroy( &blank_number );
	sem_destroy( &product_number );
	return 0;
}

运行结果如下:

标签:同步,thread,number,原语,NUM,测试,pthread,sem,NULL
From: https://www.cnblogs.com/kenneth2012/p/16873299.html

相关文章

  • 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;......
  • 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中测试若干个......