#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<semaphore.h>
//定义信号量
//盘子空信号量
sem_t empty;//初始值为1,表示当前盘子为空
//苹果信号量
sem_t apple;//初始值为0,表示当前盘子没有苹果
//橘子信号量
sem_t orange;//初始值为0,表示当前盘子没有橘子
//函数声明
void* father(void* arg);//父亲线程执行函数
void* mother(void* arg);//母亲线程执行函数
void* son(void* arg);//儿子线程执行函数
void* daughter(void* arg);//女儿线程执行函数
void* father(void* arg) {
while(1) {
sem_wait(&empty);
// 放入一个苹果
printf("father --> apple\n");
sem_post(&apple);
sleep(rand() % 10); // 随机休眠一段时间
}
}
void* mother(void* arg)
{
while(1)
{
sem_wait(&empty);
//放入一个橘子
printf("mother --> orange\n");
sem_post(&orange);
sleep(r