fork() in C


In this section we will see what is the fork system call in C. This fork system call is used to create a new process. This newly created process is known as child process. The current process which is creating another child process is called the parent process.

A child process uses the same program counter, CPU register, same files that are used by the parent process.

The fork() does not take any parameter, it returns integer values. It may return three types of integer values.

  • Negative Number: It returns negative number when child process creation is failed

  • Zero Value: It returns Zero for the newly created child process

  • Positive Value: The positive value is returned to the parent process.

Example Code

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main() {
   fork(); //make a child process of same type
   printf("Fork testing code
");    return 0; }

Output

soumyadeep@soumyadeep-VirtualBox:~$ ./a.out
Fork testing code
soumyadeep@soumyadeep-VirtualBox:~$ Fork testing code
soumyadeep@soumyadeep-VirtualBox:~$

Updated on: 30-Jul-2019

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements