后台进程读写终端的限制,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.html

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.shell

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.ubuntu

Most other terminal operations that a program can do are treated as reading or as writing. (The description of each operation should say which.)app

TOSTOP 控制后台任务是否能写终端,less

stty tostop; (sleep 5; echo hello, world) &

tostop 被设置,任务试图写终端时,SIGTTOU 信号发送给该进程,进程中止。需 kill -9.post

stty -tostop; (sleep 5; echo hello, world) &

取消 tostop,后台任务能够发送 hello, world 到终端。ui

$ 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.this

参考资料

相关文章
相关标签/搜索