使用fork、execvp执行UNIX命令
Executing UNIX commands using fork,execvp
我正在尝试创建一个程序,该程序接受包含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()
}
这个结构正确吗?请告诉我您的建议/意见。
...
if( pid == 0 )
execvp();
else if ( pid == -1 )
// handle errors
...
您建议的结构不允许顺序执行,因为您没有调用wait直到所有节点执行完毕。你可以使用wait()的一个变体,它允许WNOHANG选项在不阻塞的情况下检查子进程的终止。
当你调用fork()时,你需要检查-1是否表示有错误,以及检查0是否表示调用已经在子进程中返回。
很难确切地知道应该建议什么结构,因为我不确定您需要什么顺序约束。
相关文章:
- 从命令行c++发送文本文件名
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 在UNIX系统中使用DIR查找文件的字节大小
- 当套接字连接断开时检测C/C++Unix
- 外壳包装器句柄/执行交互式命令管道C++ UNIX
- unix命令带有C
- 具有读写功能的 UNIX 管道系统可执行"ls -la"命令
- 如何在Unix Shell中的后台执行命令
- qmake为win32-g++平台生成的Makefile包含unix命令
- Execlp()不接受参数列表,只接受命令.UNIX/LINUX编程
- Unix 命令行在编译后无法运行程序,没有错误消息
- 函数的多个声明使命令 UNIX
- 在C++中执行unix命令
- 在 C++ 文件上的 Windows 环境中运行 Unix 类似命令"./a.out <data.txt"
- 如何从Unix命令行安装c和c++编译器,然后与Eclipse一起使用
- 在unix平台上使用curses检测c++中的箭头键输入在调用系统命令后不起作用
- 如何创建任何定制的Unix命令
- 使用fork、execvp执行UNIX命令