当前位置: 技术问答>linux和unix
如此一程序,何以将机器跑死。。。
来源: 互联网 发布时间:2016-09-06
本文导语: 此程序解决的是阅览室问题,十个读者,五把椅子。 为何第五个读者进到阅览室的时候立马死机(Windows XP Cygwin) 此程序在Ubuntu9.04下没一点问题。同样的方式使用线程,信号灯解决读写者、哲学家就餐、生产者消费...
此程序解决的是阅览室问题,十个读者,五把椅子。
为何第五个读者进到阅览室的时候立马死机(Windows XP Cygwin)
此程序在Ubuntu9.04下没一点问题。同样的方式使用线程,信号灯解决读写者、哲学家就餐、生产者消费者等问题时,在Windows XP Cygwin也不会出错。
在下眼拙,实在看不出此程序有什么太大的问题,请指教。。。
为何第五个读者进到阅览室的时候立马死机(Windows XP Cygwin)
此程序在Ubuntu9.04下没一点问题。同样的方式使用线程,信号灯解决读写者、哲学家就餐、生产者消费者等问题时,在Windows XP Cygwin也不会出错。
在下眼拙,实在看不出此程序有什么太大的问题,请指教。。。
#include
#include
#include
#include
#include
#include
#include
#include
#define NUM_THREADS_READER 10 /*define the number of reader*/
#define CHAIR_NUM 5
int chairflg[CHAIR_NUM][2],counter=0;
sem_t chair,mutex;
pthread_t threads_reader[NUM_THREADS_READER];
FILE* fd;
void* reader_thread(void *tid);
void display(void);
int main(){
int i;
fd=fopen("output.txt","w"); /*open a file to save the result*/
sem_init(&chair,0,CHAIR_NUM); /*set the value of semaphores*/
sem_init(&mutex,0,1);
for(i=0;i