lab4 Process
  • Introduction
  • Knowing about process
    • Checking System Process
    • Process Identification
  • Let's Fork! Process Creation
    • Background of Process Creation
    • fork system call
    • Distinguish Parent and Child Process
    • Exercises
  • Process Execution
    • First exec example
    • Environment Variables
    • exec Family
    • exec Family: execlp
    • exec Family: execle
    • exec Family: execv
    • exec Family: execvp
    • exec Family: execve
    • exec error handling
  • Wait!
    • Problem 1: Without Wait?
    • Sleep
    • Problem 2: Zombies!
    • Wait System Call
    • waitpid
Powered by GitBook
On this page

Was this helpful?

  1. Let's Fork! Process Creation

fork system call

PreviousBackground of Process CreationNextDistinguish Parent and Child Process

Last updated 5 years ago

Was this helpful?

Let's try the following code to see the effect of fork !

/* LetsFork/fork1.c */
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc,char *argv[])
{
 printf("Before Fork, My PID: [%d]\n",getpid());
 fork();
 printf("After Fork, My PID: [%d]\n",getpid());
 return 0;
}

Here is the sample output:

You will discover you have two lines of output! This can be explained by the behaviour of fork. In the example, Process [8265] is the original process, and process [8266] is a newly spawned process (we call it child process).

Behaviour of fork

  1. The child process will be spawned after fork.

  2. The child will continue executing the code after fork() is returned, not from the beginning.

  3. The parent still continues executing the same program.

Then How can we distinguish the parent and child process?