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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:24:10+00:00 2026-05-29T11:24:10+00:00

I have this example of code, but I don’t understand why this code creates

  • 0

I have this example of code, but I don’t understand why this code creates 5 processes plus the original. (6 process total)

#include <unistd.h>

int main(void) {
    int i;
    for (i = 0; i < 3; i++) {
        if (fork() && (i == 1)) {
            break;
        }
    }
}

Process graph

  • 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-29T11:24:11+00:00Added an answer on May 29, 2026 at 11:24 am

    fork() splits a process in two, and returns either 0 (if this process is the child), or the PID of the child (if this process is the parent). So, this line:

    if (fork() && (i == 1)) break;
    

    Says “if this is the parent process, and this is the second time through the loop, break out of the loop”. This means the loop runs like this:

    • i == 0: The first time through the loop, i is 0, we create two processes, both entering the loop at i == 1. Total now two processes

    • i == 1: Both of those processes fork, but two of them do not continue to iterate because of the if (fork() && (i == 1)) break; line (the two that don’t continue are both of the parents in the fork calls). Total now four processes, but only two of those are continuing to loop.

    • i == 2: Now, the two that continue the loop both fork, resulting in 6 processes.

    • i == 3: All 6 processes exit the loop (since i < 3 == false , there is no more looping)

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

Sidebar

Related Questions

I have this code example, but I don't understand why changing the values in
I have this code, but I don't see where I went wrong here. It
Has anyone done this / have some example code?
I have this piece of code (from the Nokia PC connectivity 3.2 example code,
I have this C# code for example DateTime.Now.ToString(MMMM dd, yyyy); Now the current thread
i have this problem, look this code of example: src := ''; for iindex
Let's say for example I have this code: <input type=file id=file name=> <input class=uploadarea>
I have this simple example I can't seems to get working : MERGE INTO
I have tried this example http://www.bennadel.com/blog/1056-ColdFusion-CFPOP-My-First-Look.htm , but it retrieve emails from POP server.
I have this very simple example that I am using to learn structs in

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.