Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用exec在新进程中执行系统命令_C++_Linux_Process_Parallel Processing_Exec - Fatal编程技术网

C++ 使用exec在新进程中执行系统命令

C++ 使用exec在新进程中执行系统命令,c++,linux,process,parallel-processing,exec,C++,Linux,Process,Parallel Processing,Exec,我正在尝试生成一个执行系统命令的进程,而我自己的程序仍在运行,两个进程将并行运行。我在linux上工作 我在网上查了一下,听起来我应该使用exec()家族。但它并不像我预期的那样有效。例如,在下面的代码中,我只看到正在打印的“before”,而没有看到“done” 我很好奇我是否遗漏了什么 #include <unistd.h> #include <iostream> using namespace std; main() { cout << "be

我正在尝试生成一个执行系统命令的进程,而我自己的程序仍在运行,两个进程将并行运行。我在linux上工作

我在网上查了一下,听起来我应该使用exec()家族。但它并不像我预期的那样有效。例如,在下面的代码中,我只看到正在打印的“before”,而没有看到“done”

我很好奇我是否遗漏了什么

#include <unistd.h>
#include <iostream>

using namespace std;

main()
{
   cout << "before" << endl;
   execl("/bin/ls", "/bin/ls", "-r", "-t", "-l", (char *) 0);
   cout << "done" << endl;
}
#包括
#包括
使用名称空间std;
main()
{

cout函数的
Exec
系列用新的可执行文件替换当前进程

要执行所需操作,请使用
fork()
函数之一,并让子进程
exec
执行新图像

[回应最新情况]

它正按照您告诉它的那样执行:您不必按“enter”键来完成程序:它已退出。shell已给出提示:

[wally@zenetfedora ~]$ ./z
before
22397
done
[wally@zenetfedora ~]$ 0               << here is the prompt (as well as the pid)
total 5102364
drwxr-xr-x.  2 wally wally       4096 2011-01-31 16:22 Templates
...
[wally@zenetfedora~]$/z
之前
22397
完成

[wally@zenetfedora ~]$0您缺少对
fork
的调用。
exec
所做的只是将当前进程映像替换为新程序的映像。使用
fork
生成当前进程的副本。其返回值将告诉您运行的是子进程还是原始父进程。如果是子进程,则调用
exec


一旦进行了更改,似乎只需按Enter键即可完成程序。实际发生的情况是:父进程分叉并执行子进程。两个进程都运行,并且两个进程同时打印到标准输出。它们的输出是乱码。父进程比子进程要少,因此它首先终止。当它终止时,等待它的shell将唤醒并打印通常的提示。同时,子进程仍在运行。它打印更多的文件条目。最后,它终止。shell没有注意到子进程(它的孙子进程),因此shell没有理由重新打印提示符。请仔细查看您得到的输出,您应该能够在上面的
ls
输出中找到常用的命令提示符

光标似乎在等待您按键。当您按键时,shell会打印一个提示,所有提示看起来都正常。但就shell而言,所有提示都已经正常。您可以在之前键入另一个命令。这看起来有点奇怪,但shell会正常执行,因为它只接收来自m键盘,而不是从子进程向屏幕打印附加字符


如果在单独的控制台窗口中使用类似于
top
的程序,则可以在按Enter键之前查看并确认两个程序都已完成运行。

您缺少execl()将内存中的当前程序替换为
/bin/ls


我建议看一下
popen()

应该为孩子添加一个
wait
,而不是
sleep
really你的main应该是
int main
,在结尾你应该添加一个
return 0;
按照你的方式,你的编译器可能会在结尾填充,并在结尾添加“按任意按钮继续”
[wally@zenetfedora ~]$ ./z
before
22397
done
[wally@zenetfedora ~]$ 0               << here is the prompt (as well as the pid)
total 5102364
drwxr-xr-x.  2 wally wally       4096 2011-01-31 16:22 Templates
...