简体   繁体   English

在后台C linux中运行新的子进程

[英]Running a new child process in background C linux

I am trying to run a new process in background so it will be possible to continue working with parent process. 我正在尝试在后台运行新流程,因此可以继续使用父流程。

I used fork then execl. 我先用fork然后用execl。 I tried to add to the execl command the argument & but it doesn't work: 我试图将参数&添加到execl命令,但它不起作用:

execl("newproc","newproc","arg1","&",NULL);

Is there any solution? 有什么解决办法吗?

The child will run in the background since you used fork . 自从您使用fork以来,孩子将在后台运行。 The child will keep running in parallel with the parent (if exec succeeded). 子级将继续与父级并行运行(如果exec成功)。 If you care about whether the child process succeeded or not (and your code should) you should eventually call waitpid to collect its exit status. 如果您关心子进程是否成功(您的代码应该成功),则最终应调用waitpid来收集其退出状态。 Otherwise you should call fork twice and have the intermediate process exit without wait ing for the child, so that init adopts the grandchild process. 否则,您应该调用fork 两次 ,并在不wait子进程的情况下退出中间进程,以便init采用孙进程。

As @mah said, the & is unnecessary. 正如@mah所说, &是不必要的。 But another change is needed to that line; 但是,这条线还需要改变。 execl is a variadic function, and function prototypes therefore don't take care of converting arguments to the correct type. execl是一个可变参数函数,因此函数原型不需要考虑将参数转换为正确的类型。 Therefore the final argument should be passed as the correct type - just change it to (char*)NULL . 因此,最后一个参数应该以正确的类型传递-只需将其更改为(char*)NULL

You mention that your code didn't work. 您提到您的代码无效。 While that could just be because of the spurious & , it may also be because of the first argument. 虽然这可能只是因为虚假的& ,也可能是因为第一个参数。 The execl function does not search $PATH for the named program, so unless newproc is actually in the current directory, this execl() invocation will return. execl函数不会在$PATH搜索指定的程序,因此,除非newproc实际上在当前目录中,否则此execl()调用将返回。 When execl returns, that always indicates that there is a problem. execl返回时,始终表示存在问题。 The simplest way to solve this is to use execlp() instead of execl() . 解决此问题的最简单方法是使用execlp()而不是execl() The alternative approach is to specify an absolute path as the first argument. 另一种方法是将绝对路径指定为第一个参数。 You can even specify a relative path as the first argument, but this is rarely useful. 您甚至可以将相对路径指定为第一个参数,但这很少有用。

& is not a command argument, its a flag that the shell uses to know to run the command in the background. &不是命令参数,它是shell用来在后台运行命令的标志。 In this case, you're performing the work of the shell... remove the & . 在这种情况下,您要执行外壳的工作...删除& Since you state you've called fork() , as long as you're only performing execl() in the child process after fork returns, you're already running in the background. 由于您声明了自己的调用fork() ,只要在fork返回后仅在子进程中执行execl() ,您就已经在后台运行了。

The problem is that & is not a command line option to programs. 问题在于&不是程序的命令行选项。 Instead, it is merely special shell syntax which puts a command in the background. 相反,它只是一种特殊的shell语法,它在后台放置了一个命令。 The distinguishing feature of background programs is that they are not connected to a terminal, and the terminal is not waiting for the process to complete. 后台程序的显着特征是它们没有连接到终端,并且终端不等待该过程完成。 The proper function is daemon() . 正确的函数是daemon() Do a man daemon to read up on how it is used. 做一个man daemon来阅读如何使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM