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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:19:25+00:00 2026-06-06T01:19:25+00:00

Running this program is printing forked! 7 times. Can someone explain how forked! is

  • 0

Running this program is printing “forked!” 7 times. Can someone explain how “forked!” is being printed 7 times?

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

int main(){

  fork() && fork() || fork() && fork();

  printf("forked!\n");

  return 0;
}
  • 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-06T01:19:27+00:00Added an answer on June 6, 2026 at 1:19 am

    There’re several concepts being used here, first one is knowing what fork does and what it returns in certain circumstances. Shortly, when it gets called, it creates a duplicate process of the caller and returns 0 (false for logical expressions) in child process and non-zero (true for logical expressions) for parent process.
    Actually, it could return a negative (non-zero) value in case of an error, but here we assume that it always succeeds.

    The second concept is short-circuit computation of logical expressions, such as && and ||, specifically, 0 && fork() will not call fork(), because if the first operand is false (zero), then there’s no need to compute the second one. Similarly, 1 || fork() will not call fork() neither.

    Also note that in child processes the computation of the expression continues at the same point as in the parent process.

    Also, note that the expression is computed in the following order due to precedence:

    (fork() && fork()) || (fork() && fork())
    

    These observations should lead you to the correct answer.

    Consider the simplified example of fork() && fork()

       fork()        
      /     \
    false   true && fork()
                    /   \
                 false  true
    

    So here we have three processes created, two of which return false as the result and one returning true. Then for || we have all the processes returning false trying to run the same statement again, so we have 2 * 3 + 1 = 7 as the answer.

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

Sidebar

Related Questions

This is the code #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h>
Core dumped while running this program: int main(void) { sqlite3 *conn; int error =
When I am running this program I am getting warning array subscript has type
I'm running this small C# test program launched from a pre-commit batch file private
Say suppose I am running a java program through command line. And this program
When running this powershell command Get-ChildItem -Recurse -Include *.txt You get multiple tables in
I am developing a GUI to launch an external long-term running background program. This
Im running this query on the same server as the web application, so SPQuery.ExpandRecurrence
Try running this in a .VBS file MsgBox(545.14-544.94) You get a neat little answer
In running this on index.html, I get the following error: Uncaught SyntaxError: Unexpected token

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.