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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:39:39+00:00 2026-05-29T23:39:39+00:00

I am learning how to create processes by using fork, and I am confused

  • 0

I am learning how to create processes by using fork, and I am confused in the following. This is the code:

int main() {
    int ret = fork();
    // printf("%d\n", ret);
    ret = ret && fork(); /* Here is where I am confused*/
    // print("ret: %d\n", ret);
    if(ret == 0) {
        fork();
    }
    printf("Hello world\n");
    return 1;
}

So, what is the use of double ampersand exactly doing?
I ran the program with a “printf” to know what was exactly the values, but it became more confusing because the output in the first “printf” is 0 and the second “printf” is “1”. So I am not quite sure what is double ampersand doing.

I appreciate the help!

  • 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-29T23:39:40+00:00Added an answer on May 29, 2026 at 11:39 pm

    It is called the logical AND operator. This will evaluate the logical ANDing result of the two operands. The property of this operator is:

    First the left hand side operand is evaluated, if it is TRUE (non zero), then the right hand side operand is evaluated. If it is also true then the whole expression is true, or false otherwise. On the other hand, if the left hand side operand is FALSE, then the right hand side operand is not evaluated at all. This can be done because, as one of the operands is false, whatever be the other operand, the expression becomes false. This is known as short circuiting

    In your code, if the left hand if ret is true, then only the right hand side portion is evaluated, which eventually calls the fork () system call. The return value of the call is ANDed with the current value of ret, and reassigned to ret.

    Basically it works like

    if (ret == TRUE)
    {
      retval = fork ();
      ret = ret && retval;
    }
    

    Read this:

    On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created, and errno is set appropriately.

    Consider the fork tree below. Each tree “node” shows the sequence of executed statement in each individual statement. One work in one line.

        (p1)
    +--+ret = fork ();
    |   printf 1 shows pid
    |   && allows         fork (), ret = 1 = pid1 && pid2 
    |   printf 2 shows 1    +
    |   `if' not entered    |
    |   show hello          |
    |                       |    (p3)
    |                       +--+ ret = 0 = ret && fork () (this is 0 here)
    +-----+                      printf 2 shows 0
          |                      `if' is entered
          |                      fork ()
          |                      show hello
          |                            +
          |                            |
          +                            |
         (p2)                          |
        level 1                        +-------+
        print 0 in 1st printf                  |
        && DOES NOT allow fork ()            (p5)
        print 0 in 2st printf             show hello
       `if' entered
        fork () +-----------+
        show hello          |
                            |
                            +
                          (p4)
                        show hello
    

    Here what goes in each process.

    p1
    executes fork () once, and has a pid (non-zero) in ret.
    prints the pid
    short circuit allows to execute fork (). As this is the parent, it returns another pid, which is anded with the previous child pid, which evaluates to 1. Therefore ret now contains 1, which is printed in the second printf. as ret is 1, if is not executes. Hello is printed.

    p2
    Child of p1, so ret has 0. prints 0 in the first printf. Short circuit does not allow fork () call. if body is entered, and fork () is called, which makes (p4). Now (p2) proceeds to print Hello.

    p3
    Child of p1, so fork () return is 0, which is ANDed with ret, and makes it 0 after the assignment. This is spawned after the first printf, so only the second printf shows 0.
    if is entered, fork () is executed, which makes (p5). Now p4 proceeds to print Hello.

    p4
    starts from if body, gets out and prints Hello

    p5
    starts from if body, gets out and prints Hello

    Above I have tried to express the process spawn tree, and the sequence of works in each process is expressed in each line of the process “node” on the tree. The edges denote spawn, and the edge start at the corresponding fork.

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

Sidebar

Related Questions

I'm learning Objective-C using GNUstep(because I use Linux). I was thinking in create a
I am learning F# and very interested in this language I try to create
I'm using the following script to create a toggle effect for opening and closing
#include<iostream> #include<fstream> #include<cstdlib> #include<string> using namespace std; **int main() { double write(); double read();
Just wondering if anyone has some good resources for learning how to create new
I learning Perl and I want to create a simple application that gets all
I am learning Cocoa and trying to create an application for Mac that displays
I'm learning MVC and am having trouble deciding when I should create a new
I am learning PyQt and wonder if one can create custom/owner draw control like
I'm learning about table design in SQL and I'm wonder how to create a

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.