Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A program where parent process creates a child process and both parent and child run same program different code

Tags:

c

fork

unix

process

//same program different code
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>

int main()
{
    int pid;
    pid=fork();
    if(pid<0)
    {
        printf("\n Error ");
        exit(1);
    }
    else if(pid==0)
    {
        printf("\n Hello I am the child process ");
        printf("\n My pid is %d ",getpid());
        exit(0);
    }
    else
    {
        printf("\n Hello I am the parent process ");
        printf("\n My actual pid is %d \n ",getpid());
        exit(1);
    }

}

I tried this , I hope its correct .
But I am not satisfied with the output .

The output is :

 Hello I am the parent process 
 My actual pid is 4287 
 ashu@ashu-VirtualWorld:~/Desktop/4thSemester/testprep$ 
 Hello I am the child process 
 My pid is 4288

Please help me I cant understand its output , I want the child process to occur first and then parent process . Also , when the execution ends the control is transferred to the program , so to return to terminal I have to use ctrl+c , I want that after the execution of the program ends the control transfers to the terminal .

like image 535
Ashutosh Anand Avatar asked Dec 04 '22 11:12

Ashutosh Anand


2 Answers

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/wait.h>
int main()
{
    int status;
    int pid;
    pid=fork();
    if(pid<0)
    {
        printf("\n Error ");
        exit(1);
    }
    else if(pid==0)
    {
        printf("\n Hello I am the child process ");
        printf("\n My pid is %d ",getpid());
        exit(0);
    }
    else
    {
       wait(&status);
        printf("\n Hello I am the parent process ");
        printf("\n My actual pid is %d \n ",getpid());
        exit(1);
    }

}

In this program the if (pid == 0) means child and pid > 0 means parent so the parent and child are running in the same time access the same resource so the problem of the race condition occur. The which one is first access the resource its executed first and another one is executed at later time.

The wait function avoid the race condition and when the child execution complete until the parent wait and after executed the parent

The Default vfork avoid the race condition

        pid=vfork();

Because the vfork use the parent wait for until the child complete. 

Also if you want to get the process ID of parent process. Use int ppid = getppid() function.

The output of the program is:

     Hello I am the child process 
     My pid is 7483 
     Hello I am the parent process 
     My actual pid is 7482 
like image 155
loganaayahee Avatar answered Dec 27 '22 08:12

loganaayahee


The thing with parallel processes is that they don't map well to the idea of "happening first"; they're running in parallel.

Of course, you can change the odds by having a delay in the code for the parent, before the print-outs.

Not at all sure what you mean by "the control is transferred to the program"; since both processes will hit exit() very quickly after having printed their messages, there should be no program left to transfer control to.

Note that you don't need to use exit() from main(), it should end with a plain old return since its return type is int.

like image 30
unwind Avatar answered Dec 27 '22 06:12

unwind