20240812 作业

#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;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值