后台进程读写终端的限制,SIGTTIN 信号, SIGTTOU 信号, tostop 属性
Posted armme
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了后台进程读写终端的限制,SIGTTIN 信号, SIGTTOU 信号, tostop 属性相关的知识,希望对你有一定的参考价值。
Processes in the foreground job of a controlling terminal have unrestricted access to that terminal; background proesses do not. This section describes in more detail what happens when a process in a background job tries to access its controlling terminal.
When a process in a background job tries to read from its controlling terminal, the process group is usually sent a SIGTTIN signal. This normally causes all of the processes in that group to stop (unless they handle the signal and don‘t stop themselves). However, if the reading process is ignoring or blocking this signal, then read fails with an EIO error instead.
Similarly, when a process in a background job tries to write to its controlling terminal, the default behavior is to send a SIGTTOU signal to the process group. However, the behavior is modified by the TOSTOP bit of the local modes flags (see section Local Modes). If this bit is not set (which is the default), then writing to the controlling terminal is always permitted without sending a signal. Writing is also permitted if the SIGTTOU signal is being ignored or blocked by the writing process.
Most other terminal operations that a program can do are treated as reading or as writing. (The description of each operation should say which.)
TOSTOP 控制后台任务是否能写终端,
stty tostop; (sleep 5; echo hello, world) &
tostop 被设置,任务试图写终端时,SIGTTOU 信号发送给该进程,进程停止。需手动 kill.
stty -tostop; (sleep 5; echo hello, world) &
取消 tostop,后台任务可以发送 hello, world 到终端。
$ stty -a
speed 38400 baud; rows 51; columns 151; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; discard = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke -flusho -extproc
可以看到 ubuntu 16.04 的默认设置是 -tostop.
参考资料
以上是关于后台进程读写终端的限制,SIGTTIN 信号, SIGTTOU 信号, tostop 属性的主要内容,如果未能解决你的问题,请参考以下文章
shell调用beeline导致nohup后台进程stopped