You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We must not create a process group for a built-in in a single-command pipeline, so we need to handle single-command and multi-command pipelines separately.
Questions:
When should the shell move itself back to the foreground?
For consistency and stability, the shell should move itself back to the foreground after running each job rather than only before performing I/O.
Does the parent process need to call setpgid after forking? → Yes, at least for the background job
The fg and bg built-ins have to send a SIGCONT to the target job, which must have been created. The parent process has to make sure the child is in its own process group when starting the job.
Does the parent process need to call tcsetpgrp after forking? → No
The shell have to call setpgid and tcsetpgrp to turn a child process into a foreground job. Since the two function cannot be called atomically, the job is in the background between the two calls. The child process has to call tcsetpgrp to bring itself to the foreground before starting its task.
The child blocks SIGTSTP until it finishes tcsetpgrp. Therefore, if the child stops, it has called tcsetpgrp, so it won't call tcsetpgrp after it is resumed (either in the foreground or background).
When the parent finishes waiting for the child to terminate or stop, the child must have called tcsetpgrp, so it is surely in the foreground.
The shell should suspend itself if it is in the background when it is manipulating jobs. Exactly when?
An interactive shell ignores SIGTTIN, SIGTTOU, and SIGTSTP by default, so it should not rely on automatically sent signals to stop it. Instead, it should "kill" itself or call tcsetpgrp to suspend itself.
When the shell starts a foreground job, the shell should be in the foreground.
When the shell starts a background job, the shell does not have to be in the foreground.
When the shell runs a built-in, the shell should be in the foreground because the (non-special) built-in should behave the same as the corresponding external utility.
To sum up, it will suffice for the shell to check the foreground-ness before running each pipeline.
System::setpgid
8e8bc23System::tcsetpgrp
/dev/tty
inEnv
Env::start_subshell
Env
5f0a3f9Env::wait_for_subshell_to_finish
We must not create a process group for a built-in in a single-command pipeline, so we need to handle single-command and multi-command pipelines separately.
Questions:
setpgid
after forking? → Yes, at least for the background jobfg
andbg
built-ins have to send a SIGCONT to the target job, which must have been created. The parent process has to make sure the child is in its own process group when starting the job.tcsetpgrp
after forking? → Nosetpgid
andtcsetpgrp
to turn a child process into a foreground job. Since the two function cannot be called atomically, the job is in the background between the two calls. The child process has to calltcsetpgrp
to bring itself to the foreground before starting its task.tcsetpgrp
. Therefore, if the child stops, it has calledtcsetpgrp
, so it won't calltcsetpgrp
after it is resumed (either in the foreground or background).tcsetpgrp
, so it is surely in the foreground.The text was updated successfully, but these errors were encountered: