条件编程和互斥锁的学习

第一题: 编写代码验证递归锁和错误检查锁的功能

第二题: 2p2v模型 2个生产者 1#每秒生产1个苹果 2#每秒生产2个橘子 2个消费者 3#没秒消费3个苹果 4#每2秒消费5个橘子

#include <stdio.h>
#include <string.h>
#include <stdlib.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>

typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
pthread_mutex_t m;
pthread_mutex_t n;
pthread_cond_t a;
pthread_cond_t o;
int apples =0;
int oranges=0;
void* task2(void* arg)
{
	while(1)
	{
		pthread_mutex_lock(&n);
		oranges+=2;
		printf("生产者2生产橘子的个数为: %d\n",oranges);
		pthread_cond_signal(&o);
		pthread_mutex_unlock(&n);
		sleep(1);
	}
}
void* task3(void* arg)
{
	while(1)
	{
		pthread_mutex_lock(&m);
		while(apples<3)
		{
			pthread_cond_wait(&a,&m);
		}
		if(apples>=3)
		{
			apples-=3;
			printf("消费者1消费三个苹果后的个数为:%d\n",apples);
		}
		pthread_mutex_unlock(&m);
		sleep(1);
	}
}

void* task4(void* arg)
{
	while(1)
	{
		pthread_mutex_lock(&n);
		while(oranges<5)
		{
			pthread_cond_wait(&o,&n);
		}
		if(oranges>=5)
		{
			oranges-=5;
			printf("消费者2买过5个橘子后的个数为: %d\n",oranges);
		}
		pthread_mutex_unlock(&n);
		sleep(2);
	}
}
int main(int argc, const char *argv[])
{
	pthread_mutex_init(&m, 0);
    pthread_mutex_init(&n, 0);
    pthread_cond_init(&a, 0);
    pthread_cond_init(&o, NULL);
	pthread_t id1,id2,id3,id4;

	pthread_create(&id2,0,task2,0);
	pthread_create(&id3,0,task3,0);
	pthread_create(&id4,0,task4,0);
	pthread_detach(id1);
	pthread_detach(id2);
	pthread_detach(id3);
	pthread_detach(id4);
	while(1)
	{
		pthread_mutex_lock(&m);
		apples++;
		printf("生产者1生产的苹果数为:%d\n",apples);
		pthread_cond_signal(&a);
		pthread_mutex_unlock(&m);
		sleep(1);
	}
	return 0;
}

第三题: 2p2v模型 2个生产者 1#每秒生产1个苹果 2#每秒生产2个橘子 2个消费者 3#没秒消费3个苹果 4#每2秒消费5个橘子 由于仓库有限:生产了橘子之后,就不能生产苹果,反之同理 同样由于仓库有限,仓库里面最多存放10个苹果或者20个橘子

#include <stdio.h>
#include <string.h>
#include <stdlib.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>

typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
pthread_mutex_t m;
pthread_mutex_t n;
pthread_mutex_t p;
pthread_cond_t a;
pthread_cond_t o;
int apples =0;
int oranges=0;
void* task2(void* arg)
{
	while(1)
	{
		pthread_mutex_lock(&p);
		pthread_mutex_lock(&n);
		if(oranges<20)
		{
		oranges+=2;
		printf("生产者2生产橘子的个数为: %d\n",oranges);
		pthread_cond_signal(&o);
		}
		pthread_mutex_unlock(&n);
		pthread_mutex_unlock(&p);
		sleep(1);
	}

}
void* task3(void* arg)
{
	while(1)
	{
		pthread_mutex_lock(&m);
		while(apples<3)
		{
			pthread_cond_wait(&a,&m);
		}
		if(apples>=3)
		{
			apples-=3;
			printf("消费者1消费三个苹果后的个数为:%d\n",apples);
		}
		pthread_mutex_unlock(&m);
		sleep(1);
	}
}

void* task4(void* arg)
{
	while(1)
	{
		pthread_mutex_lock(&n);
		while(oranges<5)
		{
			pthread_cond_wait(&o,&n);
		}
		if(oranges>=5)
		{
			oranges-=5;
			printf("消费者2买过5个橘子后的个数为: %d\n",oranges);
		}
		pthread_mutex_unlock(&n);
		sleep(2);
	}
}
int main(int argc, const char *argv[])
{
	pthread_mutex_init(&m, 0);
    pthread_mutex_init(&n, 0);
	pthread_mutex_init(&p,0);
    pthread_cond_init(&a, 0);
    pthread_cond_init(&o, NULL);
	pthread_t id1,id2,id3,id4;
	pthread_create(&id2,0,task2,0);
	pthread_create(&id3,0,task3,0);
	pthread_create(&id4,0,task4,0);
	pthread_detach(id1);
	pthread_detach(id2);
	pthread_detach(id3);
	pthread_detach(id4);

	 pthread_mutex_destroy(&m);
    pthread_mutex_destroy(&n);
    pthread_mutex_destroy(&p);
    pthread_cond_destroy(&a);
    pthread_cond_destroy(&o);
	while(1)
	{
		pthread_mutex_lock(&p);
		pthread_mutex_lock(&m);
		if(apples<10)
		{
		apples++;
		printf("生产者1生产的苹果数为:%d\n",apples);
		pthread_cond_signal(&a);
		}
		pthread_mutex_unlock(&m);
		pthread_mutex_unlock(&p);
		sleep(1);

	}

	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值