操做系統經典的同步問題,面試寶典,

一、測量控制系統中的數據採集任務把所採集的數據送一單緩衝區;計算任務則 從該緩衝區中取出數據並進行計算。試寫出利用信號量機制實現二者共享單緩衝區的同步算法。 php

       Var Sempty,Sfull: semaphore:= 1,0 算法

Begin 併發

              Parbegin app

              Collection:begin 進程

              repeat ip

                     採集一個數據; 同步

                     wait(Sempty); it

                     數據放入緩衝區; io

                     signal(Sfull); map

              untill false;

              end;

              Compute:begin

              repeat

                     wait(Sfull);

                     從緩衝區取出數據;

                     signal(Sempty);

                     計算;

`                    until false;

                     end;

              Parend

       End

二、有一閱覽室,共有100個座位。讀者進入時必須先在一種登記表上登記,該表爲每一座位列一個表目,包括座號和讀者姓名。讀者離開時要註銷掉登記內容。試用wait和signal原語描述讀者進程的同步問題。

       var mutex, readcount :semaphore := 1,100;

Begin

              Parbegin

              Process Reader:begin

              repeat

              wait(readcount);

              wait(mutex);

              <填入座號和姓名完成登記>;

              signal(mutex);

<閱讀>

              wait(mutex)

       <刪除登記表中的相關表項,完成註銷>

              signal(mutex);

              signal(readcount);

              until false;

              end;

              parend;

       End;

1)、桌上有一空盤,只容許放一個水果,爸爸專向盤中放蘋果,媽媽專向盤中放桔子;女兒專吃盤中的蘋果,兒子專吃盤中的桔子;試用wait和signal原語實現爸爸、媽媽、女兒、兒子之間的同步問題。

var Sempty, Sapple, Sorange,: semaphore:= 1,0,0;

 begin

       parbegin

              Father: begin

                            repeat

                                   wait(Sempty);                                <put apple in tray>;

                                   signal(Sapple);                         until false;

                       end;

              Mother: begin

                            repeat

                                   wait(Sempty);                                <put orange in tray>;

                                   signal(Sorange);                       until false;

                        end;

              Son: begin

                            repeat

                                   wait(Sorange);

                                   <take orange>;

                                   signal(Sempty);

                            until false;

                      end;

              Daughter: begin

                            repeat

                                   wait(Sapple);

                                   <take apple>;

                                   signal(Sempty);                       until false;

                       end;

              parend;

end;

一、在4×100米接力賽中,4個運動員之間存在以下關係,運動員1跑到終點把接力棒交給運動員2;運動員2一開始處於等待狀態,在接到運動員1傳來的接力棒後才能往前跑,他跑完100米後交給運動員3,運動員3也只有在接到運動員2傳來的棒後才能跑,他跑完100米後交給運動員4,運動員4接到棒後跑徹底程。請試用信號量機制對其上過程進行分析。

        

       var s1,s2,s3:semaphpre:=0,0,0;

       begin

              parbegin

                     Athlete1: begin

                                   Run100m;

                                   signal(s1);

                                end;

                     Athlete2: begin

                                   wait(s1);

                                   Run100m;

                                   signal(s2);

                                end;

                     Athlete3: begin

                                   wait(s2);

                                   Run100m;

                                   signal(s3);

                                end;

                     Athlete4: begin

                                   wait(s3);

                                   Run100m;

                                end;

              parend;

       end

 

二、在公共汽車上,司機和售票員各行其職,司機負責開車和到站停車;售票員負責售票和開、關車門;當售票員關好車門後駕駛員才能開車行駛。試用wait和signal操做實現司機和售票員的同步。

var s1,s2:semaphore:=0,0;

begin

       parbegin

              Process Driver

              begin

              repeat

                     <go right>;

                     <stop bus>;

                     signal(s2);

                     wait(s1);

              until false;

              end;

              Process BookingClerk;

              begin

              repeat

                     <ticketing>;

                     wait(s2);

                     <open the door>;

                     <close the door>;

                     signal(s1);

              until false

              end;

       parend;

end;

一、假設有3個併發進程P,Q,R,其中P負責從輸入設備上讀入信息,並傳送給Q,Q將信息加工後傳送給R,R負責打印輸出。進程P,Q共享一個有m個緩衝區組成的緩衝池;進程Q,R共享一個有n個緩衝區組成的緩衝池(假設緩衝池足夠大,進程間每次傳輸信息的單位均小於等於緩衝區長度),請寫出知足上述條件的併發程序。(12分)

var mutex1,mutex2,Sip,Siq,Soq,Sor:semaphore:=1,1,m,0,n,0;

begin

       parbegin

              Process P

              begin

              repeat

                     <讀入信息>

                     wait(Sip);

                     wait(mutex1);

                     <數據放入緩衝區>

                     signal(mutex1);

                     signal(Siq);

              until false

              end;

              Process Q

              begin

              repeat

                     wait(Siq);

                     wait(mutex1);

                     <從緩衝區中取出數據>

                     signal(mutex1);

                     signal(Sip);

                     <數據處理〉

                     wait(Soq);

                     wait(mutex2);

                     <處理後的數據放入緩衝區>

                     signal(mutex2);

                     signal(Sor);

              until false

              end;

              Process R

              repeat

                     wait(Sor);

                     wait(mutex2);

              <把數據送入打印機完成打印>;

                     signal(mutex2);

                     signal(Soq);

              until false

              end

       parend

end

 

二、有一隻鐵籠子,每次只能放入一隻動物,獵手向籠子裏放入老虎,農民向籠子裏放入豬;動物園等待取籠子裏的老虎,飯店等待取籠子裏的豬。現請用wait和signal操做寫出能同步執行的程序。

var Sempty, Stiger, Spig,: semaphore:= 1,0,0;  

begin

       parbegin

              Hunter: begin

                            repeat

                                   wait(Sempty);

                                   <put tiger in cage>;

                                   signal(Stiger);

                            until false;

                       end;

              Farmer: begin

                            repeat

                                   wait(Sempty);

                                   <put pig in cage>;

                                   signal(Spig);

                            until false;

                        end;

              Zoo: begin

                            repeat

                                   wait(Stiger);

                                   <take tiger>;

                                   signal(Sempty);

                            until false;

                      end;

              Hotel: begin

                            repeat

                                   wait(Spig);

                                   <take pig>;

                                   signal(Sempty);         until false;

                       end;

              parend;

end;

相關文章
相關標籤/搜索