【问题标题】:Executing UNIX commands using fork,execvp使用 fork、execvp 执行 UNIX 命令
【发布时间】:2011-04-07 09:08:07
【问题描述】:

我正在尝试创建一个程序,该程序接收一个包含 UNIX 命令列表的输入文件,并以特定顺序执行这些命令。 我正在学习fork()wait()execvp() 系统调用,并对等待和分叉模式有一些疑问。 这是我用于执行进程的结构。进程可以并行或顺序执行。我将在订购时决定这一点。 假设我必须按照 A、B、C、D、E 的顺序执行流程。

这是我为此提出的结构。请让我知道这是否正确。

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()
} 

这个结构正确吗?请让我知道您的建议/cmets。

【问题讨论】:

    标签: c++


    【解决方案1】:
    ...
    if( pid == 0 )
      execvp();
    else if ( pid == -1 )
      // handle errors
    ...
    

    【讨论】:

      【解决方案2】:

      您建议的结构不允许顺序执行,因为您不会调用等待,直到所有节点都已执行。您可以使用 wait() 的一种变体,它允许 WNOHANG 选项在不阻塞的情况下检查子节点的终止。

      当您调用 fork() 时,您需要检查 -1 表示错误,以及检查 0 表示调用已在子进程中返回。

      很难确切知道要建议什么结构,因为我不确定您需要什么顺序约束。

      【讨论】:

        猜你喜欢
        • 2015-02-13
        • 1970-01-01
        • 1970-01-01
        • 2023-03-07
        • 1970-01-01
        • 2016-06-15
        • 2021-03-16
        • 1970-01-01
        • 2011-08-03
        相关资源
        最近更新 更多