20240812

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

//定义2个条件变量和2个互斥锁
pthread_cond_t c1,c2;
pthread_mutex_t m1;
pthread_mutex_t m2;

//定义苹果橘子变量
int apple = 0;
int orange = 0;

//线程函数,每秒消费3个苹果
void* task1(void* arg)
{
    while(1)
    {
        pthread_mutex_lock(&m1);
        pthread_cond_wait(&c1,&m1);
        apple -= 3;
        printf("消费了3个苹果,还剩%d个\n",apple);
        pthread_mutex_unlock(&m1);
        sleep(1);
    }
}

//线程函数,每2秒消费5个橘子
void* task2(void* arg)
{
    while(1)
    {
        pthread_mutex_lock(&m1);
        pthread_cond_wait(&c2,&m1);
        orange -= 5;
        printf("消费了5个橘子,还剩%d个\n",orange);
        pthread_mutex_unlock(&m1);
        sleep(2);
    }
}


//线程函数,生产苹果
void* task3(void* arg)
{
    while(1)
    {
        pthread_mutex_lock(&m1);
        apple++;
        printf("apple = %d\n",apple);
        if(apple >= 3)
        {
            pthread_cond_signal(&c1);
        }
        pthread_mutex_unlock(&m1);
        sleep(1);
    }
}


//线程函数,生产橘子
void* task4(void* arg)
{
    while(1)
    {
        pthread_mutex_lock(&m2);

        orange += 2;

        printf("orange = %d\n",orange);

        if(orange >= 5)
        {
            pthread_cond_signal(&c2);
        }
        pthread_mutex_unlock(&m2);
        sleep(1);
    }
}

int main(int argc, const char* argv[]) 
{
    //初始化条件变量
    pthread_cond_init(&c1,0);
    pthread_cond_init(&c2,0);
    //初始化互斥锁
    pthread_mutex_init(&m1,0);
    pthread_mutex_init(&m2,0);

    //创建2个线程
    pthread_t id1,id2,id3,id4;
    pthread_create(&id1,0,task1,0);
    pthread_create(&id2,0,task2,0);
    pthread_create(&id3,0,task3,0);
    pthread_create(&id4,0,task4,0);
    pthread_detach(id1);
    pthread_detach(id2);
    pthread_detach(id3);
    pthread_detach(id4);

    while(1);

    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 m1, m2;
pthread_cond_t c1, c2;

// 定义苹果橘子变量
int apple = 0;
int orange = 0;


// 线程函数,消费苹果
void *task1(void *arg)
{
    while (1)
    {
        pthread_mutex_lock(&m1);
        pthread_cond_wait(&c1, &m1);
        apple -= 3;
        printf("消费了3个苹果,还剩%d个\n", apple);
        pthread_mutex_unlock(&m1);
        sleep(11);
    }
}

// 线程函数,消费橘子
void *task2(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);
    }
}

// 线程函数,生产苹果
void *task3(void *arg)
{
    srand(time(0));
    int index = rand() % 2;
    while (1)
    {
        pthread_mutex_lock(&m1);
        // 只生产苹果
        if (index == 0)
        {
            if (apple < 10) // 小于10,每秒生产一个苹果
            {
                apple++;
            }
            printf("apple = %d\n", apple);
            if (apple >= 3)
            {
                pthread_cond_signal(&c1);
            }
        }
        else if(index == 1)
        {
            break;
        }
        pthread_mutex_unlock(&m1);
        sleep(1); // 休眠1秒
    }
    return 0;
}

// 线程函数,生产橘子
void *task4(void *arg)
{
    srand(time(0));
    int index = rand() % 2;
    while (1)
    {
        pthread_mutex_lock(&m2);
        if (index == 1)
        {
            if (orange < 20) // 小于20,每秒生产2个橘子
            {
                orange += 2;
                if (orange > 20) // 原本为19个,只能生产一个,最多20个
                {
                    orange = 20;
                }
            }
            printf("orange = %d\n", orange);
            if (orange >= 5)
            {
                pthread_cond_signal(&c2);
            }
        }
        else if(index == 0)
        {
            break;
        }

        pthread_mutex_unlock(&m2);
        sleep(1); // 休眠1秒
    }
    return 0;
}

int main(int argc, const char *argv[])
{
    // 使用当前时间作为随机数生成器的种子
  //  srand(time(0));

    // 初始化
    pthread_mutex_init(&m1, 0);
    pthread_mutex_init(&m2, 0);
    pthread_cond_init(&c1, 0);
    pthread_cond_init(&c2, 0);

    // 创建线程
    pthread_t id1, id2, id3, id4;
    pthread_create(&id1, 0, task1, 0);
    pthread_create(&id2, 0, task2, 0);
    pthread_create(&id3, 0, task3, 0);
    pthread_create(&id4, 0, task4, 0);
    pthread_detach(id1);
    pthread_detach(id2);
    pthread_detach(id3);
    pthread_detach(id4);

    while(1);
   
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值