福州綜合網(wǎng)站建設(shè)慧生活798app下載
實驗名稱:經(jīng)典同步問題:生成者與消費者問題
相關(guān)知識
信號量
信號量是用來協(xié)調(diào)不同進程間的數(shù)據(jù)對象,可用來保護共享資源,也能用來實現(xiàn)進程間及同一進程不同線程間的進程同步。分為二值信號燈和計算信號燈兩種類型。
進程與線程原語的比較
線程創(chuàng)建
線程創(chuàng)建是通過函數(shù) pthread_create(thread,attr,start_routine,arg)
函數(shù)來實現(xiàn)的,而該函數(shù)是通過Linux特有的系統(tǒng)調(diào)用clone
來實現(xiàn)的。
格式:
#include<pthread.h>
int pthread_create(thread,attr,start_routine,arg);
其中參數(shù)thread
為線程標識符,attr
為線程屬性設(shè)置,start_routine
為線程函數(shù)起始地址,arg
為傳遞給start_routine
的參數(shù)。創(chuàng)建線程成功返回0,否則返回錯誤號。
獲得線程標識符
格式:
#include<pthread.h>
pthread_t pthread_self(void);
說明:返回調(diào)用的線程的標識符。每個線程都有自己的線程標識符,以便在進程內(nèi)區(qū)分,線程標識符在pthread_create
創(chuàng)建時產(chǎn)生。
線程等待
格式:
#include<pthread.h>
int pthread_join(thread,retval);
說明:該函數(shù)將調(diào)用它的線程阻塞,一直等到被等待的線程結(jié)束為止,當函數(shù)返回時,被等待線程的資源被收回。thread
為被等待的線程標識符,retval
為用戶定義的指針,存放被等待線程的返回值。
線程退出
格式:
#include<pthread.h>
void pthread_exit(retval); //終止調(diào)用線程,`retval`為線程的返回值。int pthread_cancel(thread); //終止由參數(shù)thread指定的線程
實驗內(nèi)容
使用多線程和信號量解決生產(chǎn)者/消費者問題:有一個長度為N的緩沖池被生產(chǎn)者和消費者共同使用。只要緩沖池未滿,生產(chǎn)者就可以將消息送入緩沖池;只要緩沖池不空,消費者便可從緩沖池中取走一個消息。生產(chǎn)者向緩沖池放入消息的同時,消費者不能操作緩沖池,反之亦然。
pthread_join()
將調(diào)用它的線程阻塞,一直等到被等待的線程結(jié)束為止,當函數(shù)返回時,被等待線程的資源被收回。本實驗中使用room_sem
信號量來表示緩沖區(qū)可用空間,product_sem
信號量表示緩沖區(qū)中有無可用產(chǎn)品,而mutex
代表線程互斥信號量。
編寫producer_consumer.c
:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<semaphore.h>
#include<sys/types.h>
#define PRODUCER_NUM 5
#define CONSUMER_NUM 5
#define POOL_SIZE 11
int pool[POOL_SIZE]; //buffer
int head=0; //read pointer
int rear=0; //write pointer
sem_t room_sem;//available room in buffer
sem_t product_sem;//available products in buffer
pthread_mutex_t mutex;
void producer_fun(void*arg)
{while(1){sleep(1);sem_wait(&room_sem);pthread_mutex_lock(&mutex);//producer write data to bufferpool[rear]=1;rear=(rear+1)%POOL_SIZE;printf("producer %d write to pool\n",(int)arg);printf("pool size is %d\n",(rear-head+POOL_SIZE)%POOL_SIZE);pthread_mutex_unlock(&mutex);sem_post(&product_sem);}
}
void consumer_fun(void *arg)
{while(1){int data;sleep(10);sem_wait(&product_sem);pthread_mutex_lock(&mutex);//consumer read data in bufferdata=pool[head];head=(head+1)%POOL_SIZE;printf("consumer %d read from pool\n",(int)arg);printf("pool size is %d\n",(rear-head+POOL_SIZE)%POOL_SIZE);pthread_mutex_unlock(&mutex);sem_post(&room_sem);}
}
int main()
{pthread_t producer_id[PRODUCER_NUM];pthread_t consumer_id[CONSUMER_NUM];pthread_mutex_init(&mutex,NULL);int ret=sem_init(&room_sem,0,POOL_SIZE-1);//initialize the signal room_semif(ret!=0){printf("sem_init error\n");exit(0);}ret=sem_init(&product_sem,0,0); //initialize the signal produc_semif(ret!=0){printf("sem_init error\n");exit(0);}for(int i=0;i<PRODUCER_NUM;i++){//create producer threadret=pthread_create(&producer_id[i],NULL,producer_fun,(void*)i);if(ret!=0){printf("producer_id error\n");exit(0);}//create consumer threadret=pthread_create(&consumer_id[i],NULL,consumer_fun,(void*)i);if(ret!=0){printf("consumer_id error\n");exit(0);}}for(int i=0;i<PRODUCER_NUM;i++){pthread_join(producer_id[i],NULL);pthread_join(consumer_id[i],NULL);}exit(0);
}
編譯時使用以下命令:
gcc -o producer_consumer producer_consumer.c -lpthread
注:編譯選項要加上
-lpthread
,因為pthread
不是Linux默認庫,鏈接時需要使用靜態(tài)庫libpthread.a
。