7.22作业

1:使用互斥锁或者信号量,实现一个简单的生产者消费者模型 一个线程每秒生产3个苹果,另一个线程每秒消费8个苹果

#include "/home/a/Desktop/test/gencls/GenFunc.h"
pthread_mutex_t mt1,mt2;

int apple = 0;
void* usrapp(void* data)
{
	while(1)
	{
		pthread_mutex_lock(&mt2);
		pthread_mutex_lock(&mt1);
		apple-=8;
		pthread_mutex_unlock(&mt1);
		sleep(1);
	}
}
void* dataprint(void* data)
{
	while(1)
	{
		printf("目前有%d个苹果\n",apple);
		sleep(1);
		printf("\033[A\033[K");
	}
}
int main(int argc, const char *argv[])
{
	pthread_mutex_init(&mt1,0);
	pthread_mutex_init(&mt2,0);
	//pthread_mutex_lock(&mt1);
	pthread_mutex_lock(&mt2);

	pthread_t pid1,pid2;
	pthread_create(&pid1,0,usrapp,0);
	pthread_create(&pid2,0,dataprint,0);

	pthread_detach(pid1);
	pthread_detach(pid2);

	srand(time(0));
	while(1)
	{
		pthread_mutex_lock(&mt1);
		apple+=3;
		pthread_mutex_unlock(&mt1);
		if(apple >=8) pthread_mutex_unlock(&mt2);
		else pthread_mutex_trylock(&mt2);
		sleep(1);
	}

	pthread_mutex_destroy(&mt1);
	pthread_mutex_destroy(&mt2);
	return 0;
}

2:第二题: 有一个盘子,盘子里面最多放3个苹果,5个橘子 2个生产者线程,一个每秒放1个苹果,另一个每秒2个橘子 放了苹果就不能放橘子,放了橘子就不能放苹果 2个消费者线程,1号消费者线程每秒消费2个苹果,2号消费者线程,每秒消费3个橘子 解题思路: 苹果生产者和消费者之间,需要2组条件变量 橘子生产者和消费者之间,需要2组条件变量 苹果生产者和橘子生产者之间,需要一组互斥锁或者信号量

#include "/home/a/Desktop/test/gencls/GenFunc.h"

pthread_mutex_t mtu1,mtu2,mtu3;
pthread_cond_t cond1,cond2,cond3;

int apple= 0;
int orange = 0;
int countd1 = 0;
int countd2 = 0;

void* user1(void* data)
{
	while(1)
	{
		pthread_mutex_lock(&mtu1);
		pthread_cond_wait(&cond1,&mtu1);
		apple-=2;
		countd1+=2;
		pthread_mutex_unlock(&mtu1);
		sleep(1);
	}
}
void* user2(void* data)
{
	while(1)
	{
		pthread_mutex_lock(&mtu2);
		pthread_cond_wait(&cond2,&mtu2);
		orange-=3;
		countd2+=3;
		pthread_mutex_unlock(&mtu2);
		sleep(1);
	}
}
void* creater(void* data)
{
	while(1)
	{
		pthread_mutex_lock(&mtu3);
		pthread_cond_wait(&cond3,&mtu3);
		int ctype = rand()%2;
		//if(apple >= 3) ctype = 1;
		//if(orange >= 4) ctype = 0;
		if(apple > 0) ctype = 0;
		if(orange > 0) ctype = 1;
		if(0 == ctype) 
		{
				pthread_mutex_lock(&mtu1);
				apple+=1;
				pthread_mutex_unlock(&mtu1);
		}
		else if(1 == ctype){
				pthread_mutex_lock(&mtu2);
				orange+=2;
				pthread_mutex_unlock(&mtu2);
		}
		pthread_mutex_unlock(&mtu3);
		sleep(1);
	}
}
void* printdata(void* data)
{
	while(1)
	{
		printf("盘子里有苹果%d:橘子%d\n",apple,orange);
		//printf("消费者2消费了%d个苹果\n消费者3消费了%d个橘子\n消费者4消费了%d个橘子\n",countd2,countd3,countd4);
		sleep(1);
		printf("\033[A\033[K");
		//printf("\033[A\033[K");
		//printf("\033[A\033[K");
		//printf("\033[A\033[K");
		//printf("\033[A\033[K");
	}
}
int main(int argc, const char *argv[])
{
	pthread_mutex_init(&mtu1,0);
	pthread_mutex_init(&mtu2,0);
	pthread_mutex_init(&mtu3,0);

	pthread_cond_init(&cond1,0);
	pthread_cond_init(&cond2,0);
	pthread_cond_init(&cond3,0);

	pthread_t pid1,pid2,pid3,pid4;
	pthread_create(&pid1,0,user1,0);
	pthread_create(&pid2,0,user2,0);
	pthread_create(&pid3,0,creater,0);
	pthread_create(&pid4,0,printdata,0);

	pthread_detach(pid1);
	pthread_detach(pid2);
	pthread_detach(pid3);
	pthread_detach(pid4);

	srand(time(0));
	while(1)
	{
		if(apple < 3 && orange < 4) pthread_cond_signal(&cond3);
		if(apple >= 2) pthread_cond_signal(&cond1);
		if(orange >=3) pthread_cond_signal(&cond2);
	}

	
	pthread_mutex_destroy(&mtu1);
	pthread_mutex_destroy(&mtu2);
	pthread_mutex_destroy(&mtu3);
	
	pthread_cond_destroy(&cond1);
	pthread_cond_destroy(&cond2);
	pthread_cond_destroy(&cond3);
	return 0;
}

3: 将上述函数封装成静态库,并测试,描述该函数的功能 

键盘输入一个字符   然后清楚显示  并输出输入字符或组合功能键对应的ascII号

#ifndef __GenFunc_H__
#define __GenFunc_H__
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
#include<dirent.h>
#include <termios.h> 
#include <unistd.h> 
#include <assert.h>

#include "GenFunc.c"

typedef struct sockaddr_in addr_in_t; 
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
//功能描述:从终端获取整行输入
//输入:buff接收用的字符串,size最大读取长度
//输出:无
//返回值:获取到的字符串
char* GetLineFunc(char* buff,int size);
//功能描述:获取键盘输入 并转化为对应ascII编号
//输入:无
//输出:无
//返回值:ascII编号
int getch();

#endif

思维导图

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值