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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:59:40+00:00 2026-05-13T18:59:40+00:00

I have a doubt in the following piece of code and its behaviour: #include

  • 0

I have a doubt in the following piece of code and its behaviour:

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

#define N 5
#define nt 1

int pm[N][2],pid[N];
int i,j,size;
char s[100];
int count=0;

int main()
{
 for(i=1;i<N;i++)
    {
      printf("\n i=%d",i);

      if(pid[i]=fork() == -1)
        {
          printf("\n fork not wrking");
          exit(1);
        }
      else if(pid[i]>0)
        {
          printf(" \n pid:%3d\n",pid[i]);
        break;
        }

    }
return 0;
}

I initially thought that the code will spawn a process and skip out of the loop.
Thereby,
1 spawns 2 and skips out.
2 spawns 3 and skips out
3 spawns 4 and skips out
4 spawns 5 and skips out.

I tried executing this code and was surprised by the answer i got ( for the printf of i in the code). Here is the output

 i=1
 i=2
 i=3
 i=2
 i=4
 i=3
 i=3
 i=3
 i=4
 i=4
 i=4
 i=4
 i=4
 i=4
 i=4

Can Anyone please explain to me what is going on here.
NOTE: I am working on a Solaris machine.

  • 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-05-13T18:59:41+00:00Added an answer on May 13, 2026 at 6:59 pm

    Update

    You’re missing a set of parentheses in your updated code. That should be if ((pid[i] = fork()) == -1) not if (pid[i] = fork() == -1)!

    The latter is equivalent to if (pid[i] = (fork() == -1)) due to precedence rules. It ends up assigning 0 to pid[i] each time through the loop, so the parents think they are child processes and don’t break out of the loop.


    I agree with your analysis of what should happen. The parent should spawn a child and then quit, so each i=n printout should only show up once.

    Are you sure you typed it in exactly as specified in your question? I ran your program (with minor modifications) and got the output you predicted:

    $ cat fork.c
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define N 5
    
    int main() {
        int pid[N], i;
    
        for(i=1;i<N;i++) /* What’s the total number of processes? */
        {
        printf("i=%d\n",i); // Output of all the is
        if((pid[i]=fork())==-1) exit(1);
        else if(pid[i] > 0) break;
        }
    
        return 0;
    }
    $ gcc -o fork fork.c
    $ ./fork
    i=1
    i=2
    $ i=3
    i=4
    

    Note that I moved the \n in the printout to the end. When you put it at the beginning of the line you end up with stdout not being flushed, so you get multiple printouts when the parent and child processes both flush their output buffers. That might be the cause of your problem. FWIW on my Mac I got each printout twice.

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

Sidebar

Related Questions

I have a doubt i use the following piece of code get data from
I have a small doubt I have following code bool res= false; if(cond1) {
I have the following code, could anyone please clarify my doubt below. public static
I have doubt in the following scenario Scenario: A process or program starts with
Whenever i hear about interfaces i have the following doubt. i have the following
In following program . I have one doubt. I have declared one global variable
recently I am learning C++ and have some doubt on the following case. void
I am reviewing C++ casts operator and I have the following doubt: for polymorphic
I am newbie to iPhone programming. I have the following doubt which is stopping
Let's say I have the following method: public static int CountNonNullMembers<T>(this IEnumerable<T> enumerable) {

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.