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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:23:05+00:00 2026-06-11T11:23:05+00:00

I am reading a book about Operating Systems and am fairly new to C

  • 0

I am reading a book about Operating Systems and am fairly new to C programming as well (tends to complicate things) but am curious as to why I cannot print the incremented i value when every child is created via the fork(). Does it lie with the child processes or something else? Thanks in advance.

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

int main ()
{
    pid_t pid;
    int i = 1;

    pid = fork();
    pid = fork();
    pid = fork();
    pid = fork();

    if (pid == 0) {
        printf("got child %d\n", i);
        i++;
    }
}
  • 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-11T11:23:06+00:00Added an answer on June 11, 2026 at 11:23 am

    You can’t print an incremented value because i is not being printed after being incremented. The processes spun off by fork() don’t share resources, they each get an independent copy of i. So you’ll see:

    got child 1
    

    over and over. Try this tweak and you’ll see that each of the child processes are getting in to the conditional with the same value:

    int main () {
      pid_t pid;
      int i = 1;
      pid = fork();
      pid = fork();
      pid = fork();
      pid = fork();
      if (pid == 0) {
         printf("the child %d, got %d\n", getpid(), i);
         i++;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading a book about Operating Systems and it gives some examples in C
im currently reading a book about programming Android and there is a nice little
I have been reading about bit operators in Objective-C in Kochan's book, Programming in
I'm reading a book about programming ASP.NET in C#. The book makes the following
I'm reading a book about iOS (it calls Programming iOS 4, by Matt Neuburg)
I'm reading Martin Fowler's Book about enterprise application architecture design patterns, but the German
Recently i'm reading the book Operating System Concepts Chapter VI about critical section problem,
I'm reading book about OO programming in JavaScript and get some strange behaviour: function
I'm reading a book about parallel programming an it says that it's not thread
I'm reading the Modern Operating System book. And I'm confused about the Page Size.

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.