读者-写者问题
1 typedef int semaphore; 2 semaphore count_mutex = 1; 3 semaphore data_mutex = 1; 4 int count = 0; 5 6 void reader(){ 7 while(TRUE){ 8 down(&count_mutex); 9 count++; 10 if(count == 1) down(&data_mutex); 11 up(&count_mutex); 12 reader(); 13 down(&count_mutex); 14 count--; 15 if(count == 0) up(&data_mutex); 16 up(&count_mutex); 17 } 18 } 19 20 21 void writer(){ 22 while(TRUE){ 23 down(&data_mutex); 24 writer(); 25 up(&data_mutex); 26 } 27 }

浙公网安备 33010602011771号