IO作业5

设置双信号实现交替生产者线程和消费者线程

cpp 复制代码
#include <myhead.h>
int n=0;
pthread_mutex_t fastmutex;//定义互斥锁
pthread_cond_t cond;//定义条件变量
pthread_cond_t cond2;
void *product()
{
  int i;
  for(i=0;i<10;i++)
  {
	  n++;
	  printf("我生产了一台特斯拉n=%d\n",n);
	  
	  pthread_cond_signal(&cond);
	  pthread_cond_wait(&cond2,&fastmutex);
	  pthread_mutex_unlock(&fastmutex);
	  
	  //每次循环都唤醒一个消费者
  }
  pthread_exit(NULL);
}	
void *custom()
{
    pthread_cond_wait(&cond,&fastmutex);
	//让消费者处于等待状态
	n--;
	printf("我消费了一台特斯拉n=%d\n",n);
	pthread_mutex_unlock(&fastmutex);
    pthread_cond_signal(&cond2);
    pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
	pthread_mutex_init(&fastmutex,NULL);//初始化互斥锁

	pthread_cond_init(&cond,NULL);//初始化条件变量
	pthread_cond_init(&cond2,NULL);//初始化条件变量

	pthread_t tid1,tid2[10];
	if(pthread_create(&tid1,NULL,product,NULL)!=0)
	{
		perror("pthread_create");
		return -1;
	}
	int i;
	for(i=0;i<10;i++)
	{
		if(pthread_create(&tid2[i],NULL,custom,NULL)!=0)
		{
			perror("pthread_create");
			return -1;
		}
	}
	pthread_join(tid1,NULL);
	for(i=0;i<10;i++)
	{
		pthread_join(tid2[i],NULL);
	}
	return 0;
}
cpp 复制代码
#include <myhead.h>
void *fun1()
{
	printf("子线程1\n");
	int fp3=open("./3.txt",O_CREAT|O_RDWR|O_TRUNC,0664);
	if(fp3==-1)
	{
		perror("打开文件3失败\n");
		return NULL;
	}
	int fp4=open("./4.txt",O_RDONLY);
	if(fp4==-1)
	{
		perror("打开文件4失败\n");
		return NULL;
	}
	char buf[100];
	int sum=0;
int len=lseek(fp4,0,SEEK_END);
lseek(fp4,0,SEEK_SET);
while(1)
{
int a=read(fp4,buf,len/2);
sum+=a;
int k=a-(sum-len/2);
  if(sum>=len/2)
  {
write(fp3,buf,k);
	break;
  }
write(fp3,buf,a);  
}
close(fp3);
close(fp4);
}
void *fun2()
{
	sleep(2);
	printf("子线程2\n");
	int fp3=open("./3.txt",O_RDWR|O_APPEND,0664);
	if(fp3==-1)
	{
		perror("打开文件3失败\n");
		return NULL;
	}
	int fp4=open("./4.txt",O_RDONLY);
	if(fp4==-1)
	{
		perror("打开文件4失败\n");
		return NULL;
	}
	char buf[100];
	int len=lseek(fp4,0,SEEK_END);
	lseek(fp4,len/2,SEEK_SET);
	while(1)
	{
		int a=read(fp4,buf,len-len/2);
		if(a<0)
		{
			break;
		}
		write(fp3,buf,a);
	}
	close(fp3);
	close(fp4);

}
int main(int argc, const char *argv[])
{
pthread_t tid1;
pthread_t tid2;
int k1=pthread_create(&tid1,NULL,fun1,NULL);
	if(k1!=0)
	{
		printf("创建线程1失败\n");
		exit(1);
	}
int k2=pthread_create(&tid2,NULL,fun2,NULL);
	if(k2!=0)
	{
		printf("创建线程2失败\n");
		exit(1);
	}
	sleep(3);
	return 0;
}
cpp 复制代码
#include <myhead.h>
#define MAX 5
#define SIZE 1000000
int n=0;
pthread_mutex_t fastmutex;//定义互斥锁
//pthread_mutex_t fastmutex=PTHREAD_MUTEX_INITIALIZER;
//静态初始化互斥锁
void *fun1()
{
	pthread_mutex_lock(&fastmutex);
	//上锁
	int i;
	for(i=0;i<SIZE;i++)
	{
		n+=1;
	}
pthread_mutex_unlock(&fastmutex);
//解锁
	pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
	//没有互斥锁时多线程产生竞态的现象
	pthread_t tid[MAX];
	pthread_mutex_init(&fastmutex,NULL);
	//动态初始化互斥锁,默认属性
	int i;
	for(i=0;i<MAX;i++)
	{
	int k=pthread_create(&tid[i],NULL,fun1,NULL);
	if(k!=0)
	  {
		perror("pthread_create");
		return -1;
	  } 
	}
for(i=0;i<MAX;i++)
{
	pthread_join(tid[i],NULL);//循环回收线程资源
}
printf("预期的结果:5000000\n");
printf("实际的结果:%d\n",n);
pthread_mutex_destroy(&fastmutex);
	return 0;
}
cpp 复制代码
#include <myhead.h>
//创建一个生产者生产10台特斯拉
//创建10个消费者线程,使用信号量每次只允许一个消费者区消费特斯拉
int n1=0;
sem_t sem;//定义无名信号量
void *product()
{
  int i;
  for(i=0;i<10;i++)
  {
	  n1+=1;
	  printf("n=%d生产者生产一台特斯拉\n",n1);
  }
  sem_post(&sem);//生产者完成后,释放无名信号量
  //sem=sem+1;
  pthread_exit(NULL);//生产者线程退出
}
void *custom()
{
	sem_wait(&sem);//消费者线程申请无名信号量
	//sem=sem-1;
	printf("n=%d消费者消费了一台特斯拉\n",n1);
	n1--;
	sem_post(&sem);//消费者线程释放无名信号量
	//sem=sem+1
	pthread_exit(NULL);//消费者线程退出
}
int main(int argc, const char *argv[])
{
pthread_t tid1,tid2[10];
sem_init(&sem,0,0);//初始化无名信号量0
//表示所有线程没有申请权限
int k1=pthread_create(&tid1,NULL,product,NULL);
if(k1!=0)
{
	perror("pthread_create");
	return -1;
}
int i;
for(i=0;i<10;i++)
{
	int k2=pthread_create(&tid2[i],NULL,custom,NULL);
	//消费者线程函数
	if(k2!=0)
	{
		perror("pthread_create");
		return -1;
	}
}
pthread_join(tid1,NULL);//回收生产者线程
for(i=0;i<10;i++)
{
	pthread_join(tid2[i],NULL);
}
sem_destroy(&sem);//销毁无名信号量
	return 0;
}
相关推荐
重生之小比特1 小时前
【算法拾光】递归、搜索与回溯算法简介
算法·深度优先
夏日听雨眠2 小时前
数据结构( Floyd)
数据结构·算法
金榜题名萱2 小时前
蓝桥杯 - 妮妮的月饼工厂(二分)
c语言·数据结构·c++·算法·蓝桥杯·动态规划
小王C语言2 小时前
优选算法:队列+宽选(bfs)
数据结构·算法·宽度优先
AI殉道师3 小时前
合因科技三大临床试验数字化模块:EDC + RTSM(IWRS)+ ePRO,从入组到随访如何协同?
科技·算法·临床试验数字化
Shaoshing4 小时前
LeetCode100 76.最小覆盖字串
java·算法·leetcode
不知棋者4 小时前
蓝桥杯 2020 省赛 B 组既约分数:从暴力枚举到欧拉函数前缀和
c++·算法·职场和发展·蓝桥杯
电科_银尘4 小时前
【大模型算法与工程 63】视觉语言模型:视觉编码器如何接入LLM
人工智能·算法·语言模型
小蒋学算法4 小时前
算法-三个无重叠子数组的最大和-前缀和+前缀和最值维护
数据结构·算法
揽星河..5 小时前
【C++】模板进阶
c++·算法