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 8253133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:46:19+00:00 2026-06-08T00:46:19+00:00

Consider the output of the below program: int main() { int ret; ret=fork(); ret=fork();

  • 0

Consider the output of the below program:

int main()
{
    int ret;
    ret=fork();
    ret=fork();
    ret=fork();
    ret=fork();

    if(!ret)
            printf("one\n");
    else
            printf("two\n");
    return 0;
}

I am getting the output as:

two
one
two
two    

http://ideone.com/omgKm

AFAIT, the output should be 8 times one & 8 times two.

Where are the rest one's & two's?

  • 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-08T00:46:20+00:00Added an answer on June 8, 2026 at 12:46 am

    Consider this alternative code which tracks what goes on somewhat better:

    #include <stdio.h>
    #include <unistd.h>
    
    int main(void)
    {
        int ret1 = fork();
        int ret2 = fork();
        int ret3 = fork();
        int ret4 = fork();
    
        if (ret4 == 0)
            printf("one: (%d: %d, %d, %d, %d)\n", (int)getpid(), ret1, ret2, ret3, ret4);
        else
            printf("two: (%d: %d, %d, %d, %d)\n", (int)getpid(), ret1, ret2, ret3, ret4);
        return 0;
    }
    

    Show us the output from this variation and we can see what worked and what failed.


    After seeing the alternative output, I got this on my Mac (where Isis JL: is my prompt and rmk is an alternative implementation of make):

    Isis JL: rmk fb && ./fb
        /usr/bin/gcc -O3 -g -std=c99 -Wall -Wextra fb.c -o fb  
    two: (38068: 38069, 38070, 38071, 38072)
    one: (38072: 38069, 38070, 38071, 0)
    two: (38071: 38069, 38070, 0, 38075)
    two: (38070: 38069, 0, 38074, 38077)
    two: (38073: 0, 0, 38078, 38079)
    two: (38069: 0, 38073, 38076, 38080)
    one: (38075: 38069, 38070, 0, 0)
    one: (38077: 38069, 0, 38074, 0)
    Isis JL: two: (38074: 38069, 0, 0, 38081)
    two: (38078: 0, 0, 0, 38082)
    one: (38079: 0, 0, 38078, 0)
    one: (38081: 38069, 0, 0, 0)
    two: (38076: 0, 38073, 0, 38083)
    one: (38080: 0, 38073, 38076, 0)
    one: (38083: 0, 38073, 0, 0)
    one: (38082: 0, 0, 0, 0)
    
    Isis JL:
    

    Note the interleaved prompt — the blank line at the end is where I hit return after the output completed.

    Hypothesis:

    The output on ideone is not captured after the initial process stops.

    Try this alternative, which waits for children to die before exiting:

    #include <stdio.h>
    #include <unistd.h>
    #include <sys/wait.h>
    
    int main(void)
    {
        int ret1 = fork();
        int ret2 = fork();
        int ret3 = fork();
        int ret4 = fork();
    
        if (ret4 == 0)
            printf("one: (%d: %d, %d, %d, %d)\n", (int)getpid(), ret1, ret2, ret3, ret4);
        else
            printf("two: (%d: %d, %d, %d, %d)\n", (int)getpid(), ret1, ret2, ret3, ret4);
        while (wait(0) > 0)
            ;
        return 0;
    }
    

    Output on Mac, once more:

    Isis JL: rmk fb && ./fb
        /usr/bin/gcc -O3 -g -std=c99 -Wall -Wextra fb.c -o fb  
    two: (38111: 38112, 38113, 38114, 38115)
    one: (38115: 38112, 38113, 38114, 0)
    two: (38114: 38112, 38113, 0, 38119)
    two: (38113: 38112, 0, 38117, 38121)
    two: (38117: 38112, 0, 0, 38123)
    one: (38119: 38112, 38113, 0, 0)
    two: (38118: 0, 38116, 0, 38124)
    one: (38121: 38112, 0, 38117, 0)
    one: (38125: 0, 0, 38122, 0)
    two: (38116: 0, 0, 38122, 38125)
    two: (38122: 0, 0, 0, 38126)
    two: (38112: 0, 38116, 38118, 38120)
    one: (38120: 0, 38116, 38118, 0)
    one: (38123: 38112, 0, 0, 0)
    one: (38124: 0, 38116, 0, 0)
    one: (38126: 0, 0, 0, 0)
    Isis JL:
    

    Hypothesis Proven

    To the extent you can prove anything… http://ideone.com/zFoLn …

    This shows all 16 lines of output. The problem must have been ‘premature termination’.

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

Sidebar

Related Questions

Consider the below program: #pragma startup foo1 #pragma exit foo2 void foo1() { printf(Called
Consider the below program: class A { public: A(int i) { cout<<Called<<endl; } };
Consider the below code snippet: int main() { const int i=3; int *ptr; ptr=const_cast<int*>(&i);
Consider the below String String names = Bharath-Vinayak-Harish-Punith I want to get output in
Consider the following output from a Tomcat server under Eclipse: INFO: Initializing Coyote HTTP/1.1
Consider the below Case 1: [Success] Input : X(P)~AK,X(MV)~AK Replace with: AP Output: X(P)~AP,X(MV)~AP
Consider the program below. It has been simplified from a complex case. It fails
Consider the sample code below : File1.cpp #include <iostream> static int x = 6;
Consider the following php code used for HTML output: echo <div id='logo2'> <a href='if(checkLanguage())
Consider: cd C:\BORLAND\BCC55\BIN bcc32 hello.cpp Output: Borland C++ 5.5.1 for Win32 Copyright (c) 1993,

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.