Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

linux - What is fd0 set to, when process is started in background?

Normally fd0 == stdin. But when process is started in background and fd0 is owned by terminal again, what is it set in that background process then? How can the background process accepts input? (if there was no redirection)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

FD 0 is defined to be standard input, no matter what it's connected to.

Whether a process is run in the foreground or background, it inherits all the descriptors from its parent. So FD 0 will be connected to the terminal when there's no redirection.

However, terminal devices have a foreground process group, and only processes in that group are permitted to read input. Shells that implement job control put each job into a different process group, and set the terminal's foreground process group to the group of the foreground process. The result of this is that background processes will be stopped if they try to read from the terminal.

You can also use stty tostop to stop background processes if they try to write to the terminal. This can be useful if you don't want output from background processes to interfere with what you're doing in the foreground.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...