In the context of Unix or Linux operating systems, Fork is a system call for creating a new process from an existing process that is already running. This is separate from the same term, "fork," also used in programming, in which developers take source code from one project to start a new piece of software.
The fork() function creates a new process by duplicating the existing process from which it is called. The new process created by the Fork system call is called the "child process," running concurrently with the original or "parent process." When a new child process is created, both processes execute the next instruction following the fork() system call. System call fork() takes no arguments and returns a process ID (PID).
The concept of processes is fundamental to UNIX/Linux operating systems. Each process is identified through a PID, which is unique across the whole operating system. The child and parent processes run in separate memory spaces. At the time of the Fork system call, both memory spaces have the same content. Each process also has its own process address space where memory segments, such as code segment, data segment, stack segment, etc, are placed. The child process is a duplicate copy of the parent with some exceptions:
- The child process has its own unique PID
- The child process has a parent PID which is the same as the PID of the process that created it
- The child process's resource utilization and CPU time counters are reset to zero
- The set of pending signals in the child process is initially empty
The child process also does not inherit the following from the parent process:
- Timers
- Memory locks
- Semaphore adjustments
- Process-associated record locks
- Outstanding asynchronous I/) operations
If the fork() function is successful, it returns twice: once in the child process with the return value "0," and then in the parent process with the return value equal to the child’s PID. This is because after the fork system call, the child process created shares the text segment with the parent process and continues execution from the next statement, therefore returning twice (once in parent and once in child).
The different values returned by the fork() function are the following:
- Negative value—the creation of a child process was unsuccessful
- Zero—returns in the child process when successful
- Positive value—returns the PID of the child process in the parent process
If the function is unsuccessful, "errno" indicates the error.
Potential errors when running the fork() function include those below:
A limit on the number of threads imposed by the system was encountered. This can be caused by a number for a number of reasons:
- The RLIMIT_NPROC soft resource limit, which limits the number of processes and threads for a real user ID, was reached
- The kernel's system-wide limit on the number of processes and threads was reached
- The max number of PIDs was reached
- The PID limit imposed by the cgroup "process number" controller was reached
EAGAIN can also mean the caller is operating under the scheduling policy without the reset-on-fork-flag set.
The function failed to allocate the necessary kernel structures due to memory restrictions.
An attempt was made to create a child process in a PID namespace whose "init" process has terminated.
The function is not supported on this platform. An example could be hardware without a Memory-Management Unit).
The system call was interrupted by a signal and will be restarted.

