如何在后台运行进程?

时间:2022-06-07 19:21:40

I've done a fork and and then an exec but I don't know how to start it in the background.

我做了一个fork然后是一个exec,但我不知道如何在后台启动它。

Should I use an argument after the exec? If so, which is it?

我应该在执行后使用参数吗?如果是这样,这是什么?

1 个解决方案

#1


2  

If you simply want to background a process use daemon().

如果您只是想要后台进程使用守护进程()。

If you want to spawn off a process that then backgrounds itself 1st use fork() and inside this 1st child call fork() again letting the 2nd child call exec*() for the process to be backgrounded. Let the initial parent wait() for the 1st child.

如果你想生成一个进程,然后背景本身第一次使用fork()并在第一个子调用fork()中再次让第二个子进程调用exec *()来进行后台处理。让第一个孩子的初始父等待()。

As the parent of the second child (the process fork()ed 1st) dies, the exec*()ed process will be reaped by init.

当第二个子节点的父节点(进程fork()ed 1st)消失时,exec *()ed进程将由init获取。


Note: The pattern above is sometimes referred to as "double-forking". See also here: Why fork() twice (and links from there)

注意:上面的模式有时被称为“双重分叉”。另见:这里为什么fork()两次(和那里的链接)

Another interesting set of answers on this is here: Linux: Difference between forking twice and daemon(ise)

另一个有趣的答案就在这里:Linux:分叉两次和守护进程(ise)之间的区别

#1


2  

If you simply want to background a process use daemon().

如果您只是想要后台进程使用守护进程()。

If you want to spawn off a process that then backgrounds itself 1st use fork() and inside this 1st child call fork() again letting the 2nd child call exec*() for the process to be backgrounded. Let the initial parent wait() for the 1st child.

如果你想生成一个进程,然后背景本身第一次使用fork()并在第一个子调用fork()中再次让第二个子进程调用exec *()来进行后台处理。让第一个孩子的初始父等待()。

As the parent of the second child (the process fork()ed 1st) dies, the exec*()ed process will be reaped by init.

当第二个子节点的父节点(进程fork()ed 1st)消失时,exec *()ed进程将由init获取。


Note: The pattern above is sometimes referred to as "double-forking". See also here: Why fork() twice (and links from there)

注意:上面的模式有时被称为“双重分叉”。另见:这里为什么fork()两次(和那里的链接)

Another interesting set of answers on this is here: Linux: Difference between forking twice and daemon(ise)

另一个有趣的答案就在这里:Linux:分叉两次和守护进程(ise)之间的区别