Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...