利用线程的同步和互斥解决两个消费者两个生产者一个临界区问题
2017-10-26 22:44
405 查看
//没长期测试 ,发出来让大家看看
#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>
#include<stdlib.h>
pthread_t fa,ma,boy,gr;
sem_t sem_fa,sem_ma,sem_boy,sem_gr;
pthread_mutex_t mutex;
void *father(void *arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
printf("父亲买一个苹果\n");
sem_wait(&sem_fa);
sleep(2);
printf("父亲把苹果放在盘子里\n");
sem_post(&sem_gr);
}
pthread_exit(NULL);
}
void *girl(void *arg)
{
while(1)
{
pthread_mutex_unlock(&mutex);
sem_wait(&sem_gr);
printf("女儿从盘子里拿走苹果\n");
sleep(1);
sem_post(&sem_fa);
printf("女儿吃苹果\n");
sleep(5);
}
pthread_exit(NULL);
}
void *mather(void *arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
printf("母亲买一个橘子\n");
sem_wait(&sem_ma);
sleep(2);
printf("母亲把橘子放在盘子里\n");
sem_post(&sem_boy);
}
pthread_exit(NULL);
}
void *boy1(void *arg)
{
while(1)
{
pthread_mutex_unlock(&mutex);
sem_wait(&sem_boy);
printf("儿子从盘子里拿走橘子\n");
sleep(1);
sem_post(&sem_ma);
printf("儿子吃掉橘子\n");
sleep(5);
}
pthread_exit(NULL);
}
int main()
{
sem_init(&sem_fa,0,1);
sem_init(&sem_gr,0,0);
sem_init(&sem_ma,0,1);
sem_init(&sem_boy,0,0);
pthread_mutex_init(&mutex,NULL);
pthread_mutex_lock(&mutex);
if(pthread_create(&fa,NULL,father,NULL)!=0)
{
perror("thread fa create fail\n");
exit(0);
}
if(pthread_create(&gr,NULL,girl,NULL)!=0)
{
perror("thread girl create fail\n");
exit(0);
}
pthread_mutex_unlock;
pthread_mutex_lock(&mutex);
if(pthread_create(&ma,NULL,mather,NULL)!=0)
{
perror("thread mather create fail\n");
exit(0);
}
if(pthread_create(&boy,NULL,boy1,NULL)!=0)
{
perror("thread boy1 create fail\n");
exit(0);
}
pthread_mutex_unlock;
pthread_join(fa,NULL);
pthread_join(gr,NULL);
pthread_join(ma,NULL);
pthread_join(boy,NULL);
sem_destroy(&sem_fa);
sem_destroy(&sem_gr);
sem_destroy(&sem_ma);
sem_destroy(&sem_boy);
pthread_mutex_destroy(&mutex);
}
#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>
#include<stdlib.h>
pthread_t fa,ma,boy,gr;
sem_t sem_fa,sem_ma,sem_boy,sem_gr;
pthread_mutex_t mutex;
void *father(void *arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
printf("父亲买一个苹果\n");
sem_wait(&sem_fa);
sleep(2);
printf("父亲把苹果放在盘子里\n");
sem_post(&sem_gr);
}
pthread_exit(NULL);
}
void *girl(void *arg)
{
while(1)
{
pthread_mutex_unlock(&mutex);
sem_wait(&sem_gr);
printf("女儿从盘子里拿走苹果\n");
sleep(1);
sem_post(&sem_fa);
printf("女儿吃苹果\n");
sleep(5);
}
pthread_exit(NULL);
}
void *mather(void *arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
printf("母亲买一个橘子\n");
sem_wait(&sem_ma);
sleep(2);
printf("母亲把橘子放在盘子里\n");
sem_post(&sem_boy);
}
pthread_exit(NULL);
}
void *boy1(void *arg)
{
while(1)
{
pthread_mutex_unlock(&mutex);
sem_wait(&sem_boy);
printf("儿子从盘子里拿走橘子\n");
sleep(1);
sem_post(&sem_ma);
printf("儿子吃掉橘子\n");
sleep(5);
}
pthread_exit(NULL);
}
int main()
{
sem_init(&sem_fa,0,1);
sem_init(&sem_gr,0,0);
sem_init(&sem_ma,0,1);
sem_init(&sem_boy,0,0);
pthread_mutex_init(&mutex,NULL);
pthread_mutex_lock(&mutex);
if(pthread_create(&fa,NULL,father,NULL)!=0)
{
perror("thread fa create fail\n");
exit(0);
}
if(pthread_create(&gr,NULL,girl,NULL)!=0)
{
perror("thread girl create fail\n");
exit(0);
}
pthread_mutex_unlock;
pthread_mutex_lock(&mutex);
if(pthread_create(&ma,NULL,mather,NULL)!=0)
{
perror("thread mather create fail\n");
exit(0);
}
if(pthread_create(&boy,NULL,boy1,NULL)!=0)
{
perror("thread boy1 create fail\n");
exit(0);
}
pthread_mutex_unlock;
pthread_join(fa,NULL);
pthread_join(gr,NULL);
pthread_join(ma,NULL);
pthread_join(boy,NULL);
sem_destroy(&sem_fa);
sem_destroy(&sem_gr);
sem_destroy(&sem_ma);
sem_destroy(&sem_boy);
pthread_mutex_destroy(&mutex);
}
相关文章推荐
- 两个线程之间的同步(生产者与消费者问题)性能优化
- 进程、线程知识点总结和同步(消费者生产者,读者写者三类问题)、互斥、异步、并发、并行、死锁、活锁的总结
- 利用线程解决生产者——消费者问题
- 进程、线程知识点总结和同步(消费者生产者,读者写者三类问题)、互斥、异步、并发、并行、死锁、活锁的总结
- 经典生产者与消费者问题(线程的同步与互斥)
- [java线程同步]生产者消费者问题demo
- 【线程】多线程同步互斥-互斥锁,生产者消费者问题
- 线程经典问题 生产者消费者 jdk 1.5后解决办法 lock 和condition
- 【线程】多线程同步互斥-条件变量与信号量,生产者与消费者问题
- 进程间同步互斥经典问题(一)生产者-消费者问题
- python 生产者,消费者的同步互斥问题
- C++ 用critical_section 代码临界区模拟信号量,解决生产者消费者的问题
- 杂项: 线程的同步于互斥,生产者问题,读写者问题实现
- 三个同步与互斥问题之生产者与消费者
- 资源同步与互斥问题(生产者与消费者)
- 用线程解决生产者消费者问题
- unp2 14 使用线程和互斥锁解决消费者生产者问题
- 操作系统经典同步互斥问题——生产者消费者问题
- linux中的生产者和消费者问题--信号量 互斥 同步
- 生产者-消费者问题实现 (linux下C同步信号量和互斥信号量的应用)