2011-04-07 155 views
0

我正在嘗試創建一個程序,它接收包含UNIX命令列表的輸入文件並以特定順序執行這些命令。 我正在學習fork()wait()execvp()系統調用,並且對等待和分岔模式有一些疑問。 這是我用於執行進程的結構。進程可以並行或按順序執行。我將在排序中決定這一點。 假設我必須按順序執行過程A,B,C D,E.使用fork執行UNIX命令,執行execvp

這裏是我爲此提出的結構。請讓我知道這是否正確。

ExecuteNodes function() 

For loop {}from 0 to vector size // vector - this is the data structure that will have all the input file details 
{ 
     For loop {}// this is for my ordering logic. For all nodes I calculate the number of  nodes  that can execute paralley . Also using this loop to set the nodes ready for execution 
     For loop { 
      if that node is ready for execution. 
       run a loop for the number of concurrent processes for that node . 
       pid = fork() 
       if(pid == 0) 
       execvp(); 
     } 
} 

for loop {all nodes} 
{ 
    wait() 
} 

這個結構是否正確?請讓我知道你的建議/意見。

回答

0

您建議的結構不允許順序執行,因爲您不會調用等待,直到所有節點都已執行。您可以使用wait()中的一個變體,它允許WNOHANG選項檢查是否阻止孩子終止。

當您調用fork()時,您需要檢查指示錯誤的-1,以及檢查0是否指示調用已經在子進程中返回。

由於我不確定需要什麼順序約束,因此很難確切知道要建議什麼結構。

1
... 
if(pid == 0) 
    execvp(); 
else if (pid == -1) 
    // handle errors 
...