开发者

Executing UNIX commands using fork,execvp

I am trying to create a program that takes in an input file containing a list of UNIX commands and executes these commands in a particular order. I am learning fork(), wait(), execvp() system calls and had some questions about the wait and forking pattern. This is the structure I am using for executing processes. Processes can execute in parallel or sequentially. I will be deciding this in the ordering. Say I have to execute processes in the order A, B, C D, E.

Here is the structure I came up with for this. Please let me know if this is correct.

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 execut开发者_Go百科ion.
              run a loop for the number of concurrent processes for that node .
              pid = fork()
              if(pid == 0)
              execvp(); 
         }
}

for loop {all nodes}
{
    wait()
} 

Is this structure correct ? Please let me know your suggestions/comments.


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


The structure you propose does not allow for sequential execution, as you do not call wait until all nodes have been executed. You can use one of the variants of wait() which allow the WNOHANG option to check for termination of a child without blocking.

When you call fork() you need to check for -1 which indicates an error, as well as checking for 0 indicating the call has returned in the child process.

It is difficult to know exactly what structure to suggest as I am not sure what sequential constraints you require.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜