三类线程search、insert、delete共享(访问)单链表,利用P、V原语操作实现这三类线程。限定如下: (1)search可以与同类线程同时执行; (2)insert类线程之间互斥,但是可以与任意多search同时执行; (3)del

admin2012-06-21  77

问题 三类线程search、insert、delete共享(访问)单链表,利用P、V原语操作实现这三类线程。限定如下:
  (1)search可以与同类线程同时执行;
  (2)insert类线程之间互斥,但是可以与任意多search同时执行;
  (3)delete不但同类之间互斥,而且与其他类线程互斥。

选项

答案int searcher_num=0,inserter_num=0; semaphore search_switch=1; semaphore insert_switch=1; semaphore insert_mutex=1; semaphore delete_mutex=1; semaphore no_searcher=1; semaphore no_inserter=1; search_thread: { P(search_switch); if(++searcher_hum==1)P(no_searcher); V(search_switch); search_node_code(); //critical section P(search_switch); if(--searcher_num==0)V(no_searcher); V(search_switch); } insert_thread: { P(insert_switch); if(++inserter_num==1)P(no_inserter); V(insert_switch); P(insert_mutex); insert_node_code(); //critical section V(insert_mutex); P(insert_switch); if(--inserter_num==0)V(no inserter); V(insert_switch); } delete_thread: { P(no_searcher); P(no_inserter); delete_node_code(); //critical section V(no_inserter); V(no_searcher); }

解析
转载请注明原文地址:https://kaotiyun.com/show/X8xi777K
0

最新回复(0)