简体   繁体   English

是子进程在C的fork的前台还是后台

[英]Is the child process in foreground or background on fork in C

When I perform a fork() in C on Linux , is the newly created child a foreground or a background process? 当我在Linux上的C中执行fork()时,新创建的子进程是前台进程还是后台进程?

If it is foreground by default, is there any way to force it to be created as a background process? 如果默认情况下为前台,是否有任何方法可以强制将其创建为后台进程?

I read through quite a few links about fork , but whether it's foreground or background is not mentioned anywhere. 我通读了很多关于fork链接,但是在任何地方都没有提及它的前景还是背景。

http://en.wikipedia.org/wiki/Fork_(system_call) http://en.wikipedia.org/wiki/Fork_(系统调用)

http://linux.die.net/man/2/fork http://linux.die.net/man/2/fork

http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/create.html http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/create.html

Simply set the sequence as background or foreground using the commands I detailed below. 只需使用下面详述的命令,将序列设置为背景或前景即可。 Use the pid = fork() to ensure you execute in the parent or child whichever you want to set first. 使用pid = fork()确保在父项或子项中执行任何要首先设置的操作。 In the parent, pid will be the PID of the child and in the child it will be 0. Use it like this : 在父级中,pid将是子级的PID,在子级中它将是0。

if(pid)
// Parent
else
// Child

Process 处理

A process pid is put into the process group pgid by 进程pid通过以下方式放入进程组pgid中:

setpgid(pid, pgid);

If pgid == pid or pgid == 0 then this creates a new process group with process group leader pid. 如果pgid == pid或pgid == 0,则这将创建一个具有流程组领导者pid的新流程组。 Otherwise, this puts pid into the already existing process group pgid. 否则,这会将pid放入已经存在的进程组pgid中。 A zero pid refers to the current process. pid为零表示当前进程。 The call setpgrp() is equivalent to setpgid(0,0). 调用setpgrp()等效于setpgid(0,0)。

Foreground 前景

Among the process groups in a session at most one can be the foreground process group of that session. 一个会话中的进程组最多可以是该会话的前台进程组。 The tty input and tty signals (signals generated by ^C, ^Z, etc.) go to processes in this foreground process group. tty输入和tty信号(由^ C,^ Z等生成的信号)进入此前台进程组中的进程。

A process can determine the foreground process group in its session using tcgetpgrp(fd), where fd refers to its controlling tty. 进程可以使用tcgetpgrp(fd)在其会话中确定前台进程组,其中fd表示其控制tty。 If there is none, this returns a random value larger than 1 that is not a process group ID. 如果没有,则返回大于1的随机值,该值不是进程组ID。

A process can set the foreground process group in its session using tcsetpgrp(fd,pgrp), where fd refers to its controlling tty, and pgrp is a process group in the its session, and this session still is associated to the controlling tty of the calling process. 进程可以使用tcsetpgrp(fd,pgrp)在其会话中设置前台进程组,其中fd指其控制tty,而pgrp是其会话中的进程组,并且该会话仍与该进程的控制tty相关联。调用过程。

How does one get fd? 一个人怎么得到FD? By definition, /dev/tty refers to the controlling tty, entirely independent of redirects of standard input and output. 根据定义,/ dev / tty是指控制tty,它完全独立于标准输入和输出的重定向。 (There is also the function ctermid() to get the name of the controlling terminal. On a POSIX standard system it will return /dev/tty.) Opening the name of the controlling tty gives a file descriptor fd. (还有函数ctermid()来获取控制终端的名称。在POSIX标准系统上,它将返回/ dev / tty。)打开控制tty的名称将得到文件描述符fd。

Background 背景

All process groups in a session that are not foreground process group are background process groups. 会话中不是前台进程组的所有进程组都是后台进程组。 Since the user at the keyboard is interacting with foreground processes, background processes should stay away from it. 由于键盘上的用户正在与前台进程进行交互,因此后台进程应远离它。 When a background process reads from the terminal it gets a SIGTTIN signal. 当后台进程从终端读取时,它会收到SIGTTIN信号。 Normally, that will stop it, the job control shell notices and tells the user, who can say fg to continue this background process as a foreground process, and then this process can read from the terminal. 通常,它将停止它,作业控制外壳会通知并告诉用户,谁可以说fg继续该后台进程作为前台进程,然后可以从终端读取该进程。 But if the background process ignores or blocks the SIGTTIN signal, or if its process group is orphaned (see below), then the read() returns an EIO error, and no signal is sent. 但是,如果后台进程忽略或阻止了SIGTTIN信号,或者其进程组被孤立(请参见下文),则read()返回EIO错误,并且不发送信号。 (Indeed, the idea is to tell the process that reading from the terminal is not allowed right now. If it wouldn't see the signal, then it will see the error return.) (实际上,这个想法是要告诉该过程,现在不允许从终端读取数据。如果看不到信号,那么它将看到错误返回。)

When a background process writes to the terminal, it may get a SIGTTOU signal. 当后台进程写入终端时,它可能会收到SIGTTOU信号。 May: namely, when the flag that this must happen is set (it is off by default). 五月:即,设置必须发生这种情况的标志时(默认情况下处于关闭状态)。 One can set the flag by 一个人可以设置标志

% stty tostop

and clear it again by 并再次清除它

% stty -tostop

and inspect it by 并通过检查

% stty -a

On Linux and many Unix like systems, you may want to use daemon(3) to create a background, daemonized process. 在Linux和许多类似Unix的系统上,您可能需要使用daemon(3)创建后台守护进程。

You may also want to close(2) or redirect all of stdin , stdout , stderr (perhaps to /dev/null , see null(4) ) using dup2(2) . 您可能还想使用dup2(2) 关闭(2)或重定向所有stdinstdoutstderr (也许是/dev/null ,请参见null(4)

Otherwise, take care about controlling terminals , process groups , job control , session ids. 否则,请注意控制终端进程组作业控制 ,会话ID。 See setpgrp(2) (and setpgid ) and the NOTES section. 请参阅setpgrp(2) (和setpgid )和“ 注意”部分。 Use tcsetpgrp(3) , setsid(2) 使用tcsetpgrp(3)setsid(2)

Read also the tty demystified page && tty(4) 另请阅读tty神秘化页面&& tty(4)

Are you trying to change the nice value in code? 你们是不是要改变代码nice值? Whenever you fork a process it is in the "background." 每当您分叉一个进程时,它都在“后台”。 Depending on what you mean, and what you're trying to do. 取决于您的意思以及您要执行的操作。

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

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