我是 linux 线程信号量的新手。
没有错误,没有警告,结果输出。
发生这种情况的原因是什么?
我怀疑信号量被阻塞或无限循环。
任何人都可以在某些地方看到逻辑不正确吗?
初始化 sem 值:bin_sem = 0,pile_sem = 0,fuel_sem = 0
sem_t bin_sem, pile_sem, fuel_sem;
int pile=0, fuel=0, wantedMineralVal=3, foundMineral=-1;
int isFound(void){
srand(time(NULL));
return (rand()%8+1);
}
void checkResource(){
if(fuel!=0 && pile!=0)
sem_post(&bin_sem);
}
void *deliverFuel(void *args){
while(foundMineral!=wantedMineralVal){
sem_wait(&fuel_sem);
fuel+=24;
checkResource();
}
}
void *deliverPile(void *args){
while(foundMineral!=wantedMineralVal){
sem_wait(&pile_sem);
pile+=12;
checkResource();
}
}
void *drilling(void *args){
do{
foundMineral = isFound();
if(pile == 0 || fuel==0){
sem_post(&pile_sem);
sem_post(&fuel_sem);
sem_wait(&bin_sem);
}
pile-=2;
fuel-=4;
}while(foundMineral!=wantedMineralVal);
}