14 Matching Annotations
- Nov 2022
-
en.wikipedia.org en.wikipedia.org
-
The process group mechanism in most Unix-like operating systems can be used to help protect against accidental orphaning, where in coordination with the user's shell will try to terminate all the child processes with the "hangup" signal (SIGHUP), rather than letting them continue to run as orphans.
-
its jobs (internal representation of process groups)
-
- Jun 2021
-
stackoverflow.com stackoverflow.com
-
There is one very important reason for enabling job control to be useful inside scripts: the side-effect it has of placing background processes in their own process groups. This makes it much, much easier to send signels to them and their children with one simple command: kill -<signal> -$pgid. All other ways of dealing with signaling entire trees of processes either involve elaborate (sometimes even recursive) functions, which are often bugnests, or risk killing the parent in the process (no pun intended).
-
- Apr 2021
-
linusakesson.net linusakesson.net
-
By default, fork(2) places a newly created child process in the same process group as its parent, so that e.g. a ^C from the keyboard will affect both parent and child.
-
But the shell, as part of its session leader duties, creates a new process group every time it launches a pipeline.
-
Job control is what happens when you press ^Z to suspend a program, or when you start a program in the background using &
-
A job is the same as a process group.
-
- Feb 2021
-
stackoverflow.com stackoverflow.com
-
Typically, a process associated with a controlling terminal is foreground process and its process group is called foreground process group. When you start a process from the command line, it's a foreground process:
-
-
stackoverflow.com stackoverflow.com
-
The shell process itself is in yet another process group all of its own and so doesn't receive the signal when one of those process groups is in the foreground. It's that simple.
-
Switching "jobs" between foreground and background is (some details aside) a matter of the shell telling the terminal which process group is now the foreground one.
-
-
stackoverflow.com stackoverflow.com
-
PGID=$(ps opgid= "$PID") # get the Process Group ID
-
-
unix.stackexchange.com unix.stackexchange.com
-
Also, this code will fail if $$ is not the process group leader, such as when the script is run under strace. Since a call to setsid(2) is probably tricky from a shell script, one approach might be to ps and obtain the process group ID from that.
-
When your script starts a process, that child becomes a member of a process group with PGID equal to the PID of the parent process which is $$ in the parent shell.
-
To accomplish this, after starting the children (loop.sh) in the background, call wait, and upon receipt of the INT signal, kill the process group whose PGID equals your PID.
-