![](https://i-blog.csdnimg.cn/direct/756b5d523a944e4faa7e41f5b005c64e.png)
#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/ipc.h>
#include <sys/sem.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 c1;
pthread_cond_t c2;
int apple = 0;
int orange = 0;
void *task1(void *arg)
{
while (1)
{
pthread_mutex_lock(&m);
pthread_cond_wait(&c1, &m);
apple -= 3;
printf("消费了3个苹果,还剩%d个苹果\n", apple);
pthread_mutex_unlock(&m);
sleep(1);
}
}
void *task2(void *arg)
{
while (1)
{
pthread_mutex_lock(&n);
pthread_cond_wait(&c2, &n);
orange -= 5;
printf("消费了5个橘子,还剩%d个橘子\n", orange);
pthread_mutex_unlock(&n);
sleep(2);
}
}
void *task3(void *arg)
{
while (1)
{
orange += 2;
printf("生产了%d个橘子\n", orange);
if (orange >= 5)
{
pthread_cond_signal(&c2);
}
sleep(1);
}
}
void *task4(void *arg)
{
while (1)
{
apple++;
printf("生产了%d个苹果\n", apple);
if(apple >= 3)
{
pthread_cond_signal(&c1);
}
sleep(1);
}
}
int main(int argc, const char *argv[])
{
pthread_cond_init(&c1, 0);
pthread_mutex_init(&m, 0);
pthread_cond_init(&c2, 0);
pthread_mutex_init(&n, 0);
pthread_t id1, id2, id3, id4;
pthread_create(&id1, 0, task1, 0);
pthread_detach(id1);
pthread_create(&id2, 0, task2, 0);
pthread_detach(id2);
pthread_create(&id3, 0, task3, 0);
pthread_detach(id3);
pthread_create(&id4, 0, task4, 0);
pthread_detach(id4);
while(1);
return 0;
}
#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/ipc.h>
#include <sys/sem.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 c1;
pthread_cond_t c2;
int apple = 0;
int orange = 0;
void *task1(void *arg)
{
while (1)
{
pthread_mutex_lock(&m);
pthread_cond_wait(&c1, &m);
apple -= 3;
printf("消费了3个苹果,还剩%d个苹果\n", apple);
pthread_mutex_unlock(&m);
sleep(11);
}
}
void *task2(void *arg)
{
while (1)
{
pthread_mutex_lock(&n);
pthread_cond_wait(&c2, &n);
orange -= 5;
printf("消费了5个橘子,还剩%d个橘子\n", orange);
pthread_mutex_unlock(&n);
sleep(11);
}
}
void *task3(void *arg)
{
srand(time(0));
int num = rand() % 2;
while (1)
{
pthread_mutex_lock(&n);
if (num == 0 && orange < 18)
{
orange += 2;
printf("生产了%d个橘子\n", orange);
}
if (orange >= 5)
{
pthread_cond_signal(&c2);
}
pthread_mutex_unlock(&n);
sleep(1);
}
}
void *task4(void *arg)
{
srand(time(0));
int num = rand() % 2;
while (1)
{
pthread_mutex_lock(&m);
if (num == 1 && apple < 10)
{
apple++;
printf("生产了%d个苹果\n", apple);
}
if (apple >= 3)
{
pthread_cond_signal(&c1);
}
pthread_mutex_unlock(&m);
sleep(1);
}
}
int main(int argc, const char *argv[])
{
pthread_cond_init(&c1, 0);
pthread_mutex_init(&m, 0);
pthread_cond_init(&c2, 0);
pthread_mutex_init(&n, 0);
pthread_t id1, id2, id3, id4;
pthread_create(&id1, 0, task1, 0);
pthread_detach(id1);
pthread_create(&id2, 0, task2, 0);
pthread_detach(id2);
pthread_create(&id3, 0, task3, 0);
pthread_detach(id3);
pthread_create(&id4, 0, task4, 0);
pthread_detach(id4);
while (1)
;
return 0;
}