#include <stdio.h>
#include <string.h>
#include <stdlib.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 m2;
pthread_cond_t c;
pthread_cond_t c2;
pthread_cond_t c3;
pthread_mutex_t m3;
int apple = 0;
int orange = 0;
void * orangepro(void* arg)
{
while(1)
{
pthread_mutex_lock(&m2);
pthread_cond_wait(&c2,&m2);
orange += 2;
printf("橘子被生产,有%d个橘子\n",orange);
if(orange>=5)
{
pthread_cond_signal(&c3);
}
pthread_mutex_unlock(&m2);
sleep(1);
}
}
void* task(void* arg)
{
while(1)
{
pthread_mutex_lock(&m3);
pthread_cond_wait(&c,&m3);
apple-=3;
printf("3个苹果被消费,还剩%d个\n",apple);
pthread_mutex_unlock(&m3);
sleep(1);
}
}
void* task1(void* arg)
{
while(1)
{
pthread_mutex_lock(&m3);
pthread_cond_wait(&c3,&m3);
orange-=5;
printf("5个橘子被消费,还剩%d个橘子\n",orange);
pthread_mutex_unlock(&m3);
sleep(2);
}
}
int main(int argc, const char* argv[])
{
pthread_mutex_init(&m,0);
pthread_mutex_init(&m2,0);
pthread_cond_init(&c,0);
pthread_cond_init(&c2,0);
pthread_cond_init(&c3,0);
pthread_mutex_init(&m3,0);
pthread_t id,id2,id3;
pthread_create(&id,0,task,0);
pthread_create(&id2,0,task1,0);
pthread_create(&id3,0,orangepro,0);
while(1)
{
pthread_mutex_lock(&m);
apple++;
printf("苹果被生产,数量为%d\n",apple);
pthread_cond_signal(&c2);
if(apple>=3)
{
pthread_cond_signal(&c);
}
sleep(1);
pthread_mutex_unlock(&m);
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.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 m2; // 橘子
pthread_cond_t c; // 苹果
pthread_cond_t c2; // 橘子
int apple = 0;
int orange = 0;
void *orangepro(void *arg) // 橘子生产商
{
srand(time(0));
int idex = rand() % 2;
while (1)
{
pthread_mutex_lock(&m2);
if (idex == 0)
{
if (orange < 20&&orange!=19)
{
orange += 2;
printf("橘子被生产,有%d个橘子\n", orange);
if (orange >= 5)
{
pthread_cond_signal(&c2);
}
}
else
{
pthread_cond_signal(&c2);
pthread_mutex_unlock(&m2);
sleep(1);
continue;
}
}
else if (idex == 1)
{
break;
}
pthread_mutex_unlock(&m2);
sleep(1);
}
return 0;
}
void *applepro(void *arg) // 苹果生产商店
{
srand(time(0));
int idex = rand() % 2;
while (1)
{
pthread_mutex_lock(&m);
if (idex == 1)
{
if (apple < 10)
{
apple++;
printf("苹果被生产,数量为%d\n", apple);
if (apple >= 3)
{
pthread_cond_signal(&c);
}
}
else
{
pthread_cond_signal(&c);
pthread_mutex_unlock(&m);
sleep(1);
continue;
}
}
else if (idex == 0)
{
break;
}
pthread_mutex_unlock(&m);
sleep(1);
}
return 0;
}
void *task(void *arg) // 苹果消费商店
{
while (1)
{
pthread_mutex_lock(&m);
pthread_cond_wait(&c, &m);
apple -= 3;
printf("3个苹果被消费,还剩%d个\n", apple);
pthread_mutex_unlock(&m);
sleep(11);
}
}
void *task1(void *arg) // 橘子消费商店
{
while (1)
{
pthread_mutex_lock(&m2);
pthread_cond_wait(&c2, &m2);
orange -= 5;
printf("5个橘子被消费,还剩%d个橘子\n", orange);
pthread_mutex_unlock(&m2);
sleep(11);
}
}
int main(int argc, const char *argv[])
{
pthread_mutex_init(&m, 0);
pthread_mutex_init(&m2, 0);
pthread_cond_init(&c, 0);
pthread_cond_init(&c2, 0);
pthread_t id, id2, id3, id4;
pthread_create(&id, 0, task, 0);
pthread_create(&id2, 0, task1, 0);
pthread_create(&id3, 0, orangepro, 0);
pthread_create(&id4, 0, applepro, 0);
pthread_detach(id);
pthread_detach(id2);
pthread_detach(id3);
pthread_detach(id4);
while (1)
;
return 0;
}
2024.8.12
于 2024-08-13 08:37:24 首次发布