後臺進程讀寫終端的限制,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

參考資料

相關文章
相關標籤/搜索