Creating a process using fork() system call

Introduction to fork() system call:

“fork()” system call is used to create a new process.
The child process returns zero and the parent process returns a number greater then zero.
The new process created by fork() is a copy of the current process except for the returned value.

So to summarize fork() will return:

Greater than 0 to parent process.

Equal to 0 to child process

Less than 0 in case of error.

Function prototype of fork()

#include

pid_t fork(void);
pid_t vfork(void);

If fork() is successful,
It will return a number of type “pid_t” which is greater than 0 and represents the PID of the newly created child process.
The child process, fork() returns 0.
If fork() fails then its return value will be less than 0.
vfork() is a more efficient version of fork(), which does not duplicate the entire parent context.

Let us understand fork() with number of examples:

Example 1:

#include <stdio.h> 
#include <sys/types.h>
//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com

#include <unistd.h> 
int main() 
{ 
  
    fork(); 
  
    printf("Hello from www.ProDeveloperTutorial.com!\n"); 
    return 0; 
} 

Output:

Hello from www.ProDeveloperTutorial.com!
Hello from www.ProDeveloperTutorial.com!

Example 2:

#include <stdio.h> 
#include <sys/types.h> 
//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com

#include <unistd.h> 

void myFork() 
{ 
    // child process because return value zero 
    if (fork() == 0) 
        printf("Hello from Child!\n"); 
  
    // parent process because return value non-zero. 
    else
        printf("Hello from Parent!\n"); 
} 
int main() 
{ 
    myFork(); 
    return 0; 
}

Output:

Hello from Parent!
Hello from Child!

Example 3: fork() tree

#include <stdio.h> 
#include <sys/types.h> 
//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com

int main() 
{ 
    fork(); 
    fork(); 
    fork(); 
    printf("hello\n"); 
    return 0; 
} 

Output:

hello
hello
hello
hello
hello
hello
hello
hello

fork ();   // f1
fork ();   // f2
fork ();   // f3

       f1       // There will be 1 child process 
    /     \     // created by  fork 1.
  f2      f2    // There will be 2 child processes
 /  \    /  \   //  created by fork 2
f3  f3  f3  f3  // There will be 4 child processes 
                // created by fork 3

wait() system call.

wait() system call will force a parent process to wait for a child process to stop or terminate.

wait() system call return the pid of the child or -1 for an error.

Syntax of wait system call:

int wait (int *status_location)

waitpid() system call is used to wait on particular child process instead of waiting for all the child process.

Syntax for waitpid()

pid_t waitpid(pid_t pid, int *statloc, int options);

Example for wait() system call:


#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com

 
int main(void) {
  pid_t pid = fork();
 
  if(pid == 0) 
  {
    printf("Child => PPID: %d PID: %d\n", getppid(), getpid());
    sleep(6); // sleep for 6 seconds
    exit(EXIT_SUCCESS);
  } 
  else if(pid > 0) 
  {
    printf("Parent => PID: %d\n", getpid());
    printf("Waiting for child process to finish.\n");
    wait(NULL);
    printf("Child process finished.\n");
  } 
  else 
  {
    printf("Unable to create child process.\n");
  }
 
  return EXIT_SUCCESS;
}

Output:

Parent => PID: 1510
Waiting for child process to finish.
Child => PPID: 1510 PID: 1511 <------ it will wait for 6 seconds for child process to finish.
Child process finished.

exit() system call

exit() system call will terminates the process which calls this function and returns the exit status value.

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
//for more tutorials on C, C++, STL, DS, Linux visit www.ProDeveloperTutorial.com

#include <unistd.h>

int main () 
{
   printf("Start of the program.\n");
   
   printf("Exiting the program.\n");
   exit(0);

   printf("End of the program.\n");

   return(0);
}

Output:

Start of the program.
Exiting the program.

Child life cycle Process

 

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *