首页 > 其他分享 >thread同步

thread同步

时间:2022-11-09 14:26:16浏览次数:33  
标签:同步 thread number 信号量 互斥 pthread sem NULL

一、实验截图

  • 修改前
  • 修改后

二、实验代码

//更改前
#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,而只有一个消费者线程。用20201209%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/daijun123/p/16873437.html

相关文章

  • thread同步测试
    thread同步测试任务1编译运行附件中的代码,提交运行结果截图,并说明程序功能2修改代码,把同步资源个数减少为3个,把使用资源的线程增加到(你的学号%3+4)个,编译代码,提交修......
  • thread互斥测试
    thread互斥测试#include<stdio.h>#include<stdlib.h>#include<pthread.h>#include<ctype.h>structarg_set{ char*fname; intcount;};structarg......
  • Java中线程的基本操作以及Thread和Runnable两种实现的比较
    线程的定义:某一时间点执行的处理,是操作系统能够进行运算调度的最小单位。一条线程是某一进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务......
  • 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;......