The kernel implements a programming interface for users of the system called the ‘system call’ interface. Users and programs interact with the OS through its system call interface.
fork - Used to create a process. At the time of the fork, the child process inherits it’s execution state from the parent. This execution state includes the parent’s address space contents, CPU register values, and any system resources it has allocated. The OS also creates a new process control struct (task struct), an OS data structure for managing the child process, and it assigns the child process a PID.
When the child process is first scheduled to run on the CPU, it starts executing where the parent process left off, at the return from the fork call.
From a programmer’s point of view, a call to fork returns twice. Once in the context of the running parent process, and once in the context of the running child process. In order to different the return values, a call to fork returns different values to the parent and child. The value returned to the parent is the PID of the child (or -1 if the fork fails), and the value returned to the child is always 0.
Example fork code:
#include <stdio.h>
#include <sys/types.h>;
#include <unistd.h>;
int main()
{
// make two process which run same
// program after this instruction
fork();
printf("Hello world!\n");
return 0;
}`
exec - Unix provides a family of exec system calls that trigger the OS to overlay the calling process’s image with a new image from a binary executable file.
exit - to terminate, a process calls the exit syscall, which triggers the OS to clean up most of the processes execution state. After running the exit code, a process notifies it’s parent that it has exited. The parent is responsible for cleaning up the child’s remaining state from the system.
After executing the exit syscall, the OS delivers a SIGCHLD signal to the process’s parent process to notify it that its child has exited. The child then becomes a zombie process; it moves to the Exited state and can no longer run on the CPU. The execution state of a zombie process is partially cleaned up but the OS still maintains a little information about it, including about how it terminated. A parent process reaps its zombie child by calling the wait syscall.