简体   繁体   中英

execution of if/else if/else with a fork()

I have tried implementing an os program. Here is the code:

#include<sys/types.h>
#include<stdio.h>
#include<unistd.h>

int main()
{
    pid_t pid, pid1;

    pid = fork();

    if(pid<0)
    {
            fprintf(stderr,"Fork Failed");
            return 1;
    }

    else if(pid == 0) /* child process */
    {
            pid1 = getpid();
            printf("child: pid = %d\n",pid);
            printf("child: pid1 = %d\n",pid1);

    }

    else   /* parent process */
    {
            pid1 = getpid();
            printf("parent: pid = %d\n",pid);
            printf("parent: pid1 = %d\n",pid1);

    }

    return 0;
}

and its o/p:

parent: pid = 1836
parent: pid1 = 1835
child: pid = 0
child: pid1 = 1836

can somebody explain me how is it working , ie the sequence of the execution for the if / else-if / else statements written in the code. I would think once the else if condition becomes true then else part is not executed, however here it has executed the parent process part ie else part and then the child part ..... how come?

You should read up on fork() . Once you hit a fork() statement a second process is started, it has a copy of everything the parent process has but it can run a separate execution, and the return it sees from the fork is different than what the parent sees.

 int main()
 {
   pid_t pid, pid1;
                   <--- up to here you have one process running
   pid = fork();   <--- when this returns you have two processes:
                          parent has pid = child's pid               child has pid = 0


   if(pid<0)       <--- child and parent both check this, it's not true so they move on
   {
     ....
   }
   else if(pid == 0)<--- this is true for the child, not the parent
   {
     ....           <--- child will now execute this code
   }
   else             <-- nothing else was true for the parent so it sees this
     ....           <-- and executes this code

So yes, you are correct, once you hit the if , or the else if or the else you're not going to get into another branch of the code, in a single process' execution . You're seeing the else if and the else because you have two processes running.

note how the pid1 's are different, because getpid() is returning which process is running that code, and you can see you have two different processes, one picks the else if the other picks the else .

Do man fork

fork actually returns zero for the child process id and non-zero for the parent means the actual process id of child is returned to parent

after else if(pid ==0 ) {...} is child process

and else {...} is parent process.

Basic meaning to fork is to create new process .

If main calls one fork() then only child is created with it's own address space. just below the fork call all the statements are same for the parent and child process.

creating a process using fork means these two parent and child are independent process and don't share any data between them by default

But in your case after fork call these two are independent so the order of execution of these two processes are unspecified . You might get these things:

1. parent executed and program terminates
parent: pid = 1235
parent: pid1 = 1234


2. child executed then parent and then program terminates :
child : pid = 0
child :pid1 = 1235
parent : pid = 1235
parent :pid1 = 1234

If you want to make sure that child must execute first and then parent shloud exit. put one line in parent code wait(NULL); as the first statement in else{..} It means parent will wait until child terminates.

fork() creates a new process, which have independent execution from the original process.

Your new child process runs the else if part.

Your existing parent process runs the else part.

The order of execution between your child and parent process is not determistic, they are now different processes and the OS will schedule them to run however the kernel feels like. It can be more or less random whether the parent or the child process runs its code first.

You could even have the printfs from the child and parent interleaved.

There are two different processes which are running your program. After the call of fork , there are two executions of the following instructions, and they have different values of pid . So both else if and else parts are executed, each by one of the two processes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM