資源簡介
ubuntu系統下C++程序,信號量實現互斥例子爸爸女兒兒子蘋果橘子
操作系統作業
代碼片段和文件信息
#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);
- 上一篇:C語言實現局域網掃描器
- 下一篇:C++封裝的一個跨平臺的線程類和鎖類
評論
共有 條評論