2011-04-07 150 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 
...