An UNIX System Call used to create a new processes.
Let’s say we have the following code and output:
- First prints out hello world with a process identifier (PID)
- Process calls the
fork()
system call, which the OS provides as a way to create anew process - The process that is created is an (almost) exact copy of the calling process
- To the OS, there are two instances of the program
p1
running, and both are about to return from thefork()
system call
- To the OS, there are two instances of the program
- The newly created child process doesn’t start running in
main
(note that the hello world message was only printed once!) but comes to life as if it had called fork itself - While the child is identical in many ways, the value it returns to the caller of
fork()
is different – the parent receives the PID of the newly-created child but the child returns 0
It should also be noted that the output is not deterministic, as there is some randomness involved with how the CPU may choose to schedule which process runs first, the parent or the child. In some cases, the child may run first.