The os.killpg()
method signals the process group.
Note:os.killpg()
is only available for UNIX based systems.
os.killpg(pgid, sig)
It takes the following argument values:
pgid
: The current process group id.sig
: The signal type to the processor.It does not return any value.
In this code snippet, we'll discuss os.killpg()
of Python OS module. The os.killpg()
will help to stop the execution of the current process group.
# Program to check os.killpg() methodimport osimport signal# Get current process group idpgid = os.getpgid(os.getpid())if pgid == 1: # if parent process completedos.kill(os.getpid(), signal.SIGTERM)else: # otherwise stop group executionprint("Terminating whole program execution")os.killpg(os.getpgid(os.getpid()), signal.SIGCHLD)
os
and signal
module in this program.os.getpgid()
method to get the current process group ID. The os.getpgid()
takes the current process id as an argument. For this, we use os.getpid().os.kill()
to stop. The os.kill()
takes two argument values:signal.SIGTERM
generates a termination signal for the current process.os.killpg()
. Here, signal.SIGCHLD
terminates child process execution.