进程同步与互斥实验(生产流水线)

这篇实验报告详细介绍了如何在Linux环境下,基于内核信号量和用户态信号量解决扩展版的生产者-消费者问题。通过使用pthread库中的线程同步互斥API,如pthread_mutex_init()、pthread_mutex_lock()和pthread_mutex_unlock(),设计并实现了一个解决方案,以加深对进程线程同步互斥机制的理解。

操作系统实验报告


题目:生产流水线

在这里插入图片描述

一、实验目的

本题目是生产者-消费者问题的扩展。掌握 Linux 提供的内核信号量、用户态信号量的原理和方法,以及系统调用和 API 函数,采用这两类进程/线程同步互斥方式机制,针对生产者-消费者经典同步互斥问题的扩展问题,设计并编程实现解决方案,观察验证方案正确性,加深对复杂进程线程同步互斥问题的理解。

二、实验内容

在这里插入图片描述

三、实验设计原理

在这里插入图片描述在这里插入图片描述

四、实验步骤

1.在平台上创建一个.c文件并编译
2.利用 Pthread提供的 pthread_mutex_init()、pthread_mutex_lock()、pthread_mutex_unlock()等线程同步互斥 API,以线程方式编程实现该设计方案,观察分析程序运行结果。

五、实验结果及其分析

在这里插入图片描述

代码如下:

#include<stdio.h> 
#include<stdlib.h> 
#include<unistd.h> 
#include<pthread.h> 
#include<semaphore.h> 
 
#define STATION_SIZE 12 //缓冲池大小 
int mytime;
pthread_mutex_t mutexA,mutexB,mutexNumemepy;
int countA = 0,countB = 0;
int Numempty = STATION_SIZE;
void *worderA() 
{ 
    int temp = 0;
    while(1)
    {
        mytime = rand() % 2;
        temp = 0;
        sleep(mytime); 
        pthread_mutex_lock(&mutexNumemepy); 
        if(Numempty >= 2 && countA < 8)
        {
            Numempty -=2;
            pthread_mutex_lock(&mutexA);
            countA +=2;
            temp += 2;
            pthread_mutex_unlock(&mutexA);
            printf("workerA  produce %d part As.Station has %d A and %d B.\n",temp,countA,countB);
        }
        pthread_mutex_unlock(&mutexNumemepy);    
    }
} 
void *worderB() 
{ 
    int temp = 0;
    while(1)
    {
        mytime = rand() % 2;
        temp = 0;
        sleep(mytime); 
        pthread_mutex_lock(&mutexNumemepy); 
        if(Numempty >= 1 && countB < 8)
        {
            Numempty -=1;
            pthread_mutex_lock(&mutexB);
            countB +=1;
            temp += 1;
            pthread_mutex_unlock(&mutexB);
            printf("workerB  produce %d part Bs.Station has %d A and %d B.\n",temp,countA,countB);
        }
        pthread_mutex_unlock(&mutexNumemepy); 
        
    }
    
} 
void *worderC() 
{ 
    int temp = 0;
    while(1)
    {
        mytime = rand() % 2;
        temp = 0;
        sleep(mytime); 
        pthread_mutex_lock(&mutexA); 
        pthread_mutex_lock(&mutexB); 
        if(countA>=4 && countB>=3)
        {
            countA -=4;
            countB -=3;
            temp += 1;
            pthread_mutex_lock(&mutexNumemepy);
            Numempty +=7;
            pthread_mutex_unlock(&mutexNumemepy); 
            printf("workerC  produce %d part Cs.Station has %d A and %d B.\n",temp,countA,countB);
        }
        pthread_mutex_unlock(&mutexA);
        pthread_mutex_unlock(&mutexB);
        
    }
} 
 
int main() 
{ 
    srand((unsigned)time(NULL));
    pthread_t producer1;
    pthread_t producer2;
    pthread_t consumer;
    //初始化互斥量
    pthread_mutex_init(&mutexA, NULL);  
    pthread_mutex_init(&mutexB, NULL);
    pthread_mutex_init(&mutexNumemepy, NULL);
    //创建workerA线程
    int ret1,ret2,ret3; 
    ret1 =pthread_create(&producer1, NULL, worderA, NULL); 
    if (ret1 != 0) 
    { 
        printf("workerA creat error"); 
        exit(0); 
    } 
    //创建workerB线程 
    ret2 =pthread_create(&producer2, NULL, worderB, NULL); 
    if (ret2 != 0) 
    { 
        printf("workerB creat error"); 
        exit(0); 
    } 
    //创建workerC线程 
    ret3 =pthread_create(&consumer, NULL, worderC, NULL); 
    if (ret3 != 0) 
    { 
        printf("workerC creat error"); 
        exit(0); 
    } 
    pthread_join(producer1,NULL);
    pthread_join(producer2,NULL);
    pthread_join(consumer,NULL);
 
    exit(0)

总结

以上就是今天要讲的内容,本文仅仅简单介绍了进程同步和互斥的producer-consumer问题,其他相关内容还请继续关注!

评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BUPT_bo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值