日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > windows >内容正文

windows

操作系统上机作业--使用条件变量解决生产者、计算者、消费者问题(多线程)

發(fā)布時間:2023/12/1 windows 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 操作系统上机作业--使用条件变量解决生产者、计算者、消费者问题(多线程) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

pc1.c: 使用條件變量解決生產(chǎn)者、計算者、消費(fèi)者問題

/* ? 系統(tǒng)中有3個線程:生產(chǎn)者、計算者、消費(fèi)者 ? 系統(tǒng)中有2個容量為4的緩沖區(qū):buffer1、buffer2 ? 生產(chǎn)者生產(chǎn)'a'、'b'、'c'、‘d'、'e'、'f'、'g'、'h'八個字符,放入到buffer1 ? 計算者從buffer1取出字符,將小寫字符轉(zhuǎn)換為大寫字符,放入到buffer2 ? 消費(fèi)者從buffer2取出字符,將其打印到屏幕上

實(shí)現(xiàn)思路:設(shè)置兩個大小為4的緩沖區(qū),第一個為生產(chǎn)者和計算著共享,第二個為計算者和消費(fèi)者共享。

1、生產(chǎn)者線程產(chǎn)生字符,將字符寫入緩沖區(qū)buffer1中,并將指針后移,當(dāng)緩沖區(qū)被寫滿是,將生產(chǎn)者線程掛起,喚醒計算者線程,等待緩沖區(qū)buffer1有空閑存儲空間的時候再喚醒生產(chǎn)者線程。

2、計算者線程從buffer1中讀取數(shù)據(jù),并移動指針,并將讀取的數(shù)據(jù)寫入buffer2中,當(dāng)buffer1有空閑時,喚醒生產(chǎn)者進(jìn)程,當(dāng)buffer2被寫滿時喚醒消費(fèi)者進(jìn)程。

3、消費(fèi)者進(jìn)程從buffer2中讀取字符并輸出,并移動指針,當(dāng)buffer2有空閑的時候,喚醒計算者進(jìn)程。
實(shí)現(xiàn)代碼:

#include<stdio.h> #include<pthread.h> #include<unistd.h>#define CAPACITY 4char buffer1[4]; char buffer2[4];int crea; //生產(chǎn)者 int comp1;//計算者 int comp2;//計算者 int cons; //消費(fèi)者 int buffer1_is_empty(){return crea==comp1; }int buffer1_is_full(){return (crea+1)%CAPACITY==comp1; }int buffer2_is_empty(){return comp2==cons; }int buffer2_is_full(){return (cons+1)%CAPACITY==comp2; }int get_item1(){int item;item=buffer1[comp1];comp1=(comp1+1)%CAPACITY;return item; }int get_item2(){int item;item=buffer2[cons];cons=(cons+1)%CAPACITY;return item; }int put_item1(int item){buffer1[crea]=item;crea=(crea+1)%CAPACITY; }int put_item2(int item){buffer2[comp2]=item;comp2=(comp2+1)%CAPACITY; }pthread_mutex_t mutex1; pthread_cond_t wait_empty_buffer1; pthread_cond_t wait_full_buffer1;pthread_mutex_t mutex2; pthread_cond_t wait_empty_buffer2; pthread_cond_t wait_full_buffer2;#define ITEM_COUNT (CAPACITY *2)void *consumer(void *arg){int i;int item;for(i=0;i<ITEM_COUNT;i++){pthread_mutex_lock(&mutex2);while(buffer2_is_empty())pthread_cond_wait(&wait_full_buffer2,&mutex2);item=get_item2();printf(" consume item:%c\n",item);pthread_cond_signal(&wait_empty_buffer2);pthread_mutex_unlock(&mutex2);}return NULL; }void *computer(void *arg){int i;int item;for(i=0;i<ITEM_COUNT;i++){pthread_mutex_lock(&mutex1);while(buffer1_is_empty())pthread_cond_wait(&wait_full_buffer1,&mutex1);item=get_item1();printf(" computer get item:%c\n",item);item-=32;pthread_cond_signal(&wait_empty_buffer1);pthread_mutex_unlock(&mutex1);pthread_mutex_lock(&mutex2);while(buffer2_is_full())pthread_cond_wait(&wait_empty_buffer2,&mutex2);put_item2(item);printf(" computer put item:%c\n",item);pthread_cond_signal(&wait_full_buffer2);pthread_mutex_unlock(&mutex2);}return NULL; }void *create(void *arg){int i;int item;for(i=0;i<ITEM_COUNT;i++){pthread_mutex_lock(&mutex1);while(buffer1_is_full())pthread_cond_wait(&wait_empty_buffer1,&mutex1);item='a'+i;put_item1(item);printf("create item:%c\n",item);pthread_cond_signal(&wait_full_buffer1);pthread_mutex_unlock(&mutex1);}return NULL; }int main(){pthread_t consumer_tid;pthread_t computer_tid;pthread_mutex_init(&mutex1,NULL);pthread_mutex_init(&mutex2,NULL);pthread_cond_init(&wait_empty_buffer1,NULL);pthread_cond_init(&wait_full_buffer1,NULL);pthread_cond_init(&wait_empty_buffer2,NULL);pthread_cond_init(&wait_full_buffer2,NULL);pthread_create(&consumer_tid,NULL,consumer,NULL);pthread_create(&computer_tid,NULL,computer,NULL);create(NULL);pthread_join(consumer_tid,NULL);pthread_join(computer_tid,NULL);pthread_mutex_destroy(&mutex1);pthread_mutex_destroy(&mutex2);return 0; }


歡迎留言交流。。。

總結(jié)

以上是生活随笔為你收集整理的操作系统上机作业--使用条件变量解决生产者、计算者、消费者问题(多线程)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。