使用 exec 在新进程中执行系统命令 [英] using exec to execute a system command in a new process

查看:19
本文介绍了使用 exec 在新进程中执行系统命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to spawn a process that executes a system command, while my own program still proceeds and two processes will run in parallel. I am working on linux.

我在网上查了一下,听起来我应该使用 exec() 系列.但它并没有像我预期的那样工作.例如,在下面的代码中,我只看到before"被打印,而不是done".

I looked up online and sounds like I should use exec() family. But it doesn't work quite as what I expected. For example, in the following code, I only see "before" being printed, ,but not "done".

我很好奇我有什么事情吗?

I am curious if I am issing anything?

#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;
}

[更新]

感谢您的评论.现在我的程序看起来像这样.一切正常,除了最后,我必须按 Enter 才能完成程序.我不知道为什么我必须按最后一个输入?

Thank you for your guys comments. Now my program looks like this. Everything works fine except at the end, I have to press enter to finish the program. I am not sure why I have to press the last enter?

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

using namespace std;

main()
{
   cout << "before" << endl;
   int pid = fork();
   cout << pid << endl;
   if (pid==0) {
      execl("/bin/ls", "ls", "-r", "-t", "-l", (char *) 0);
   }
   cout << "done" << endl;
}

推荐答案

您错过了对 fork 的调用.exec 所做的就是用新程序的进程映像替换当前进程映像.使用 fork 生成当前进程的副本.它的返回值会告诉您是正在运行的子进程还是原始父进程.如果是孩子,则调用 exec.

You're missing a call to fork. All exec does is replace the current process image with that of the new program. Use fork to spawn a copy of your current process. Its return value will tell you whether it's the child or the original parent that's running. If it's the child, call exec.

进行更改后,看起来您只需要按 Enter 键即可完成程序.实际发生的事情是这样的:父进程派生并执行子进程.两个进程都运行,并且两个进程同时打印到标准输出.他们的输出是乱码.父进程比子进程要做的事情少,所以它首先终止.当它终止时,等待它的 shell 会唤醒并打印通常的提示.同时,子进程仍在运行.它打印更多的文件条目.最后,它终止.shell 没有关注子进程(它的孙子进程),所以 shell 没有理由重新打印提示.仔细查看您得到的输出,您应该能够找到隐藏在上面 ls 输出中的常用命令提示符.

Once you've made that change, it only appears that you need to press Enter for the programs to finish. What's actually happening is this: The parent process forks and executes the child process. Both processes run, and both processes print to stdout at the same time. Their output is garbled. The parent process has less to do than the child, so it terminates first. When it terminates, your shell, which was waiting for it, wakes and prints the usual prompt. Meanwhile, the child process is still running. It prints more file entries. Finally, it terminates. The shell isn't paying attention to the child process (its grandchild), so the shell has no reason to re-print the prompt. Look more carefully at the output you get, and you should be able to find your usual command prompt buried in the ls output above.

光标出现正在等待您按下一个键.当你这样做时,shell 会打印一个提示,一切看起来都很正常.但就外壳而言,一切都已经正常了.您之前可以输入另一个命令.它看起来有点奇怪,但 shell 会正常执行它,因为它只接收来自键盘的输入,而不是来自将附加字符打印到屏幕的子进程.

The cursor appears to be waiting for you to press a key. When you do, the shell prints a prompt, and all looks normal. But as far as the shell was concerned, all was already normal. You could have typed another command before. It would have looked a little strange, but the shell would have executed it normally because it only receives input from the keyboard, not from the child process printing additional characters to the screen.

如果您在单独的控制台窗口中使用像 top 这样的程序,您可以在按下 Enter 键之前观察并确认两个程序都已完成运行.

If you use a program like top in a separate console window, you can watch and confirm that both programs have already finished running before you have to press Enter.

这篇关于使用 exec 在新进程中执行系统命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆