Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Parallel Processing ProgramsRSS Feeds

Program to fork a child and print the process id of parent and child process

Posted By: Bogart Fischer     Category: C Programming     Views: 11524

Write a program to fork a child and print the process id of parent and child process.

Code for Program to fork a child and print the process id of parent and child process in C Programming

int main()
{

        int i;
        printf("hello before fork \n");
        printf("i : %d\n",i);

        i=fork();
        printf("\n");

        if(i==0)
        {

                printf("Child has started\n\n");
                printf("child printing first time \n");

                printf("getpid : %d getppid : %d \n",getpid(),getppid());
                sleep(5);
                printf("\nchild printing second time \n");
                printf("getpid : %d getppid : %d \n",getpid(),getppid());
        }
        else
        {
                printf("parent has started\n");
                printf("getpid : %d  getppid : %d \n",getpid(),getppid());
                printf("\n");

        }

        printf("Hi after fork i : %d\n",i);

        return 0;


}
















Output:

[04mca8@LINTEL pp]$ ./a.out
hello before fork
i : 134514088

Child has started

child printing first time
getpid : 8354 getppid : 8353

parent has started
getpid : 8353  getppid : 5656

Hi after fork i : 8354
[04mca8@LINTEL pp]$
child printing second time
getpid : 8354 getppid : 1
Hi after fork i : 0
    
  
Share: 



Bogart Fischer
Bogart Fischer author of Program to fork a child and print the process id of parent and child process is from Frankfurt, Germany.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!