Logo 
Search:

C Programming Articles

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

Program to create user defined fork function and create 2 child process, print their process ids

Posted By: Oscar Evans     Category: C Programming     Views: 6858

Write a C Program to create user defined fork function and create 2 child process, print their process ids.

Code for Program to create user defined fork function and create 2 child process, print their process ids in C Programming

int main()
{

        int i;
        int pfork(int);
        printf("Hello gud morning \n");

        printf(" i : %d ",i);
        i=pfork(3);

        if(i==0)
        {
                printf(")Parent \n");
                printf("i when 0 \n ");
                printf("Getpid : %d getppid : %d\n",getpid(),getppid());
                printf("\n");
        }

        elseif(i==1)
        {
                printf("Child \n");
                printf("i when 1 \n");
                printf("Getpid : %d getppid : %d\n",getpid(),getppid());
                printf("\n");

        }

        elseif(i==2)
        {
                printf("Child \n");
                printf("i when 2 \n");
                printf("Getpid : %d getppid : %d\n",getpid(),getppid());
                printf("\n");
        }

        else
        {
        }
}






int pfork(int x)
{
        int j;
        for(j=1;j<x;j++)
        {
                if(fork()==0)
                {
                        //sleep(3);return j;
                }
        }
        return 0;
}


Output:

[04mca8@LINTEL pp]$ ./a.out
Hello gud morning
 i : 134514128 Child
i when 1
Getpid : 28864 getppid : 28863

 i : 134514128 Child
i when 2
Getpid : 28865 getppid : 28863

 i : 134514128 )Parent
i when 0
 Getpid : 28863 getppid : 5656
  
Share: 



Oscar Evans
Oscar Evans author of Program to create user defined fork function and create 2 child process, print their process ids is from London, United Kingdom.
 
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!