資源簡介
ubuntu系統(tǒng)下C++程序,信號量實現(xiàn)互斥例子爸爸女兒兒子蘋果橘子
操作系統(tǒng)作業(yè)
代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?
using?namespace?std;
sem_t?empty;
sem_t?orange;
sem_t?apple;
pthread_mutex_t?mutex;
void*?dad(void?*arg)
{
while(1)
{
sem_wait(?&empty);
pthread_mutex_lock(?&mutex);
int?i=rand();
if(i%2==0)
{
printf(“dad:?put?orangeorange:%d.apple:%d\n“orangeapple);
pthread_mutex_unlock(?&mutex);
sem_post(?&orange);
}else
{
printf(“dad:?put?appleorange:%d.apple:%d\n“orangeapple);
pthread_mutex_unlock(?&mutex);
sem_post(?&apple);
}
}
}
void*?son(void?*arg)
{
while(1)
{
sem_wait(?&apple);
pthread_mutex_lock(?&mutex);
printf(“son:?got?appleorange:%d.apple:%d\n“orangeapple);
sem_post(?&empty);
評論
共有 條評論