Skip to main content

/docs/images/banner.jpg

Operating System

fork command

Fork

fork - create a new process

fork

We use C program to create a new process. The program is as follows:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main() {
fork();
printf("Hello world!\n");
return 0;
}

To compile and run the program, use the following commands(dont copy the $ sign, it represents the terminal prompt):

$ gcc fork.c -o fork
$ ./fork

This will create a new process and print "Hello world!" twice. The fork system call is used to create a new process.

Questions

What is the use of fork system call?
fork system call is used to create a new process.
What is the output of the program?
The program will print "Hello world!" twice.
What is the return value of fork system call?

The return value of fork system call is the process ID of the child process.