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

  • Home
  • SEARCH
  • 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 8202645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:15:31+00:00 2026-06-07T07:15:31+00:00

I just need to understand this statement: if (fork() && !fork()) shouldn’t it always

  • 0

I just need to understand this statement:

if (fork() && !fork())

shouldn’t it always be false? I mean, if I write:

if (a && !a)

It’s always false so the first should always be false too, am I wrong? Of course I am, but I’m hoping someone can explain this strange thing to me.

I’m studying C for an exam and I had to resolve this code:

int main(){
if(fork && !fork()){
   printf("a\n");
}
else printf("b\n");
}
  • 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-07T07:15:33+00:00Added an answer on June 7, 2026 at 7:15 am

    Every calls to the unix process creation system call fork() returns twice. First it returns with the PID of the child to the parent(the process which called fork()). Second it returns to 0 to the newly created child.

    from man pages:

    Return Value

    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.

    in your case

    if (fork() && !fork())
    

    The statement inside if , calls fork twice. So what will happen is following :

    A
    |----------------B
    |                |
    |---C            |
    |   |            |         
    

    Now first call to fork() will return in both A and B. In A it will be nonzero and in B it will be zero.

    Second call to fork() will be evoked only from A. because first fork returned 0 to B, it will not Evoke a second fork(). its because && short circuits the evaluation if first operand is found non zero. Thanks to Daniel for pointing this out.

    So we can make a table out of this:

    PID       fork()1      fork()2
    ------------------------------
    A           >0          >0
    B           =0          >0
    C           >0          =0
    

    So from the chart, Process C’s if will be evaluated to TRUE

    Its important to remember, fork()1 didn’t returned to C . it got the copy of Already evaluated expression from its parent.

    I hope this explains your question.

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

Sidebar

Related Questions

I'm curious, why does sed need 3 \ just to recognize one? I'd understand
Just need some quick clarification I have 2 Queries in my Access Database that
Just need a little bit of Javascript to warn people that press the back
Just need get some vals located in application.ini(main ini) in the Controller plugin I
I just need a help in configuring hiberclipse for my eclipse indigo version. Install
I just need to know if WCF is platform independent like Webservices? Can the
I just need to be pointed in the right direction - I can research
I just need some clarification on variables A normal variable has 2 parts to
I just need to rename JQgrid column dynamically as per user selection from a
I just need to know hot to create a CGLayer that has an image

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.