Unix has a number of system calls that manage processes, A system call is like a function call to the operating system, except the privilege level switches from the user to the superuser and execution continues in the operating system kernel code.
The main system calls for managing processes are fork
, exec
, wait
, and kill
.
You can manage and view current processes using Bash (more on Bash Process Management).
The fork system call
fork
creates a new process (the child) where the memory image and the metadata are an exact copy of the process that called fork (the parent).
- the child process has a new process ID
- the child process has the parent process ID of the process that called fork
- initially, the child process is an identical replica of the parent.
Shared files
One important point to note is that both the parent and child processes initially share the same open file descriptors. This means that if a file is open in the parent process before the fork
call, it will also be open in the child process. Changes made to the file in one process will affect the other.
Return value
After the fork
call, both the child and the parent continue execution as if the fork
function had returned, except that in the child fork returns 0 and in the parent fork returns the PID of the child.
Consider the following example to distinguish the child process and the parent process.
import os
def main():
# Use os.fork() to create a new process
child_pid = os.fork()
if child_pid == 0:
# This code block runs in the child process
print("Child process is running. PID:", os.getpid())
# The child process can do its own tasks here
else:
# This code block runs in the parent process
print("Parent process is running. PID:", os.getpid())
print("Child process PID:", child_pid)
# The parent process can interact with the child or perform other tasks
if __name__ == "__main__":
main()
A return value of -1 suggests a failed fork.
The exec system call
The exec
call is used to replace the current process image with a new program, it transfers the control from the current process to the new program, discarding the original process image.
The exec
comes in various versions like execl
, execv
, execle
, and others, each with different ways of specifying the program to execute and passing arguments to it.
The wait system call
The wait
system call suspends the execution of the calling process until one of its child process exits. When a child process exits, it becomes a “zombie” process until the parent calls the wait
to collect its exit status.
Return value
The wait
system call returns the PID of the terminated child process to the parent, allowing the parent to identify which child has exited.
The kill system call
Processes can exit voluntarily or they can be terminated by another process using the kill
system call. The kill
system call is a mechanism in Unix-like operating systems that allows one process (the sender) to send a signal to another process (the receiver).
Kill signal
By default, when the kill
system call is used without specifying a signal, it sends the TERM signal to the receiver process. The TERM signal is often used to request the graceful termination of a process, allowing it to perform cleanup operations before exiting.
The signal 9
is used to force quit a process without giving it a chance to clean up.
Signal authentication
Signals can only be sent to another process if either of the following is true:
- Both the sender process and the receiver process have the same UID OR
- The sender is the superuser
Operating_system INFO1112 System_call Bash Process_management