2024-08-12作业

作业1

没头绪,不值如何下手

作业2

作业3,

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#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/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;
//苹果或者橘子到达数量后,上锁生产者,然后
//生产者的上锁环节变成尝试上锁
int apple=0,orange=0;

pthread_cond_t c1;
pthread_mutex_t m1;

pthread_cond_t c2;
pthread_mutex_t m2;

pthread_mutex_t m;

void* task1 (void *arg)
{
    while(1)
    {
        pthread_mutex_lock(&m1);
        pthread_cond_wait(&c1,&m1);
        apple-=3;
        printf("消费后苹果数:%d\n",apple);

        if(apple<10)
        {
            pthread_mutex_unlock(&m1);
        }
        /*else//总不能接着apple接着减少吧
        {    
            //锁住一个生产者,第二个生产者也没法生产
            pthread_mutex_lock(&m2);
        
            //pthread_cond_signal(&c1);
        }*/
        sleep(1);
    }
}
void* task2 (void *arg)
{
    while(1)
    {
        pthread_mutex_lock(&m2);
        pthread_cond_wait(&c2,&m2);
        orange-=5;    
        printf("消费后橘子数:%d\n",orange);
        if(orange>=20)
        {
            pthread_mutex_unlock(&m2);
        }
        /*else//总不能接着apple接着减少吧
        {    
            //锁住一个生产者,第二个生产者也没法生产
            pthread_mutex_lock(&m2);
        
            //pthread_cond_signal(&c2);
        }*/

        sleep(2);
    }
}
void* task3 (void *arg)
{
    while(1)
    {
        pthread_mutex_lock(&m1);
        pthread_mutex_lock(&m2);
        printf("当前苹果数:%d\n",apple);
        if(apple>=10)
        {
            pthread_cond_signal(&c1);

        }
        

        else if(apple>=3)
        {    
            apple+=1;
            pthread_cond_signal(&c1);
        }
        else 
        {
            apple+=1;
        }
            /*else if(apple>=10)
        {
            pthread_cond_signal(&c1);
            pthread_mutex_unlock(&m1);
            pthread_mutex_unlock(&m2);
        }*/
        pthread_mutex_unlock(&m2);
        pthread_mutex_unlock(&m1);
        sleep(1);
    }
}
void* task4 (void *arg)//
{
    while(1)
    {
        pthread_mutex_lock(&m2);
        pthread_mutex_lock(&m1);
        printf("当前橘子数:%d\n",orange);
        if (orange>=10)
        {
            pthread_cond_signal(&c2);
        }
        else if(orange>=5)
        {
            orange+=2;
            pthread_cond_signal(&c2);
        }
        else 
        {
            orange+=2;
        }
        pthread_mutex_unlock(&m1);
        pthread_mutex_unlock(&m2);
        sleep(2);
    }
}

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);
    pthread_mutex_lock(&m1);

    pthread_mutex_lock(&m2);
    pthread_mutex_init(&m,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)
    {
        pthread_mutex_lock(&m);
        //printf("2p2v生产模型:\n");
        printf("\n");
        sleep(1);

        pthread_mutex_unlock(&m1);
        pthread_mutex_unlock(&m2);
        pthread_mutex_unlock(&m);

    }

    return 0;
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值