資源簡介
(Linux C)利用多進程或多線程模擬實現生產者/消費者問題。
代碼片段和文件信息
#include??
#include??
#include?
#include?
#define?SIZE?15
?
static?sem_t?mutex;??
static?sem_t?fullempty;?
struct?product{?
????char?products[SIZE];?
????int?firstend;?
????int?num;?????????????????
};
struct?product?*p;
void?*producter()?
{
char?c;
while(1){?
sem_wait(&empty);?
??? sem_wait(&mutex);
c=‘a‘+rand()?%?26;
p->products[p->end]=c;
p->end=(p->end+1)%SIZE ;
p->num=p->num+1;
printf(“proceducer生產了%c.........產品總數為%d\n“cp->num);?????
sem_post(&mutex);
sem_post(&full);
sleep(rand()%3);
}???????????
}
void?*consumer()?
?{
char?c;
while(1){?
sem_wait(&fu
評論
共有 條評論