Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8833031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:40:50+00:00 2026-06-14T08:40:50+00:00

I have tried implementing an os program. Here is the code: #include<sys/types.h> #include<stdio.h> #include<unistd.h>

  • 0

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 , i.e. 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 i.e. else part and then the child part ….. how come?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:40:51+00:00Added an answer on June 14, 2026 at 8:40 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried implementing a sorting program in mapreduce such that I have just the
I have tried this: #define format(f, ...) \ int size = strlen(f) + (sizeof((int[]){__VA_ARGS__})/sizeof(int))
I have read the following 2 articles and have tried implementing the same. My
I have 2 applications communicating using named pipes. Main program calls client program and
I have tried many ways implementing the below example, but I could not make
I'm implementing an email forwarder PHP program. Here is my requirement: Read the inbox
I have just tried implementing a class where numerous length/count properties, etc. are uint
I have tried implementing this , but the extension methods are not working when
I'm new to WPF....I have tried implementing controltemplate and datatriggers both for textbox..I want
I was wondering if any one of you have tried implementing Google Calendar API

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.