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

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

问题描述

我试图产生一个执行系统命令的进程,而我自己的程序仍然继续,两个进程将并行运行。我在linux上工作。



我在网上查找,听起来像我应该使用exec()家族。但它不工作,正如我所料。例如,在下面的代码中,我只看到之前被打印,但不是完成。



我很好奇,如果我发行什么?

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

[UPDATE]



p>谢谢你们的评论。现在我的程序看起来像这样。一切正常,除非在结束,我必须按回车完成程序。我不知道为什么我要按最后一次进入?

  #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






ve进行了更改,只有出现,您需要按Enter键程序才能完成。实际发生的是这样的:父进程分叉并执行子进程。两个进程都运行,两个进程同时打印到stdout。它们的输出是乱码。父进程比子进程少,所以它首先终止。当它终止时,你的shell,等待它,苏醒和打印通常的提示。同时,子进程仍在运行。它打印更多的文件条目。最后,它终止。 shell没有注意子进程(它的孙子),所以shell没有理由重新打印提示。仔细看看你得到的输出,你应该可以找到你常用的命令提示符埋在 ls 输出上面。



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



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


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.

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

[UPDATE]

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

解决方案

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.


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.

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.

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天全站免登陆