使用信号量实现生产者-消费者问题
1 # define N 100 2 typedef int semaphore; 3 semaphore mutex = 1; 4 semaphore empty = N; 5 semaphore full = 0; 6 7 void produce(){ 8 while(TRUE){ 9 int item = produce_item(); 10 down(&empty); 11 down(&mutex); 12 insert_item(item); 13 up(&mutex); 14 up(&full); 15 } 16 } 17 18 void consumer(){ 19 while(TRUE){ 20 down(&full); 21 down(&mutex); 22 int item = remove_item(); 23 up(&mutex); 24 up(&empty); 25 consumer_item(item); 26 } 27 }

浙公网安备 33010602011771号