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

  • SEARCH
  • Home
  • 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 9235353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:00:39+00:00 2026-06-18T07:00:39+00:00

If I run the following code : #include <stdio.h> #include <unistd.h> int main() {

  • 0

If I run the following code :

#include <stdio.h>
#include <unistd.h>

int main()
{
    pid_t pid, pid1;
    fflush(stdout);
    pid = fork();
    fflush(stdout); 
    pid1 = fork();
    if(pid==0)
    {
         printf("%d is the first child\n", getpid() );
    }
    else if(pid>0)
    {
         printf("%d is the first parent\n", pid);
        wait();
    }
    if(pid1==0)
    {
         printf("%d is the second child\n", getpid() );
    }
    else if(pid1>0)
    {
         printf("%d is the second child\n", pid1);
        wait();
    }

    return 0;
}

I get the output :

2896 is the first parent
2896 is the first child
2898 is the first child
2898 is the second child
2898 is the second child
2896 is the first parent
2897 is the second child
2897 is the second child

I cannot understand the output. Why are the same strings being printed multiple times ?

  • 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-18T07:00:40+00:00Added an answer on June 18, 2026 at 7:00 am

    You are doing total 3 forks: First, pid = fork() is done in original process, resulting in one more process, so total is now two, both continuing from the next line in your code.

    Then pid1 = fork() is done on both of these, resulting in two more child processes, so total is now 4, each again continuing to next line (first if).

    Then all three processes process the two if-else statements (assuming no fork errors), each process printing two lines. So four processes times 2 lines is 8 lines. Which you are getting.

    So the processes are:

    • original
    • first generation older child, from 1st fork statement
    • first generation younger child, from 2nd fork statement by the original
    • 2nd generation child, from 2nd fork statement by the older first generation child

    If you want to understand the output, step through the code in your mind for all of these four processes. It might also help if you print out current pid in each print statement, in addition to what you print now.

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

Sidebar

Related Questions

When I run the following code: #include <stdio.h> int main(int argc, char *argv[]) {
Consider the following code: #include <stdio.h> int main() { printf(%d, 300 * 300 /
It seems that when I run the following code: #include <stdio.h> #include <stdlib.h> int
Consider the following code: #include <stdio.h> #include <ctype.h> char* Mstrupr(char* szCad); int main() {
please take a look at the following code snippet: #include <stdio.h> int main ()
I have the following code: #include <stdio.h> #include <time.h> #include <stdlib.h> #include <unistd.h> int
Given the following code: #include <stdio.h> int main() { int *p; p = (int
I've got the following code: #include <stdio.h> int main(int argc, char **argv) { int
When run following code after compiling on gcc,I get segmentation fault. #include <stdio.h> #include
Using the following code: #include <stdio.h> struct my_struct { int a; int b; my_struct();

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.