if (fork()) fork()

11,213

Solution 1

Here's a hint: if (fork()) is just a short way of writing if (fork() != 0).

Solution 2

Perhaps you are best off just trying it, reading the documentation for fork, and then, if it still doesn't make sense, asking a more specific question about what part you don't understand.

Start by trying this:

#include <stdio.h>
#include <unistd.h>
int main(int argc,char **argv){
    int x,y=0;
    x = fork();
    if (x) y = fork();
    printf("x: %d, y: %d\n",x,y);
    return 0;
}

Solution 3

i had a same problem like you.

the meaning of ::::::> if (fork())

if (fork() !=0), and as you know:

  • fork(): can take 3 values!

  • fork() = 0 for child.

  • fork() < 0 error.

  • fork() > 0 parent.

So:

if ( 0 != O ) ==> false (in this case you dont have to do the 2 fork())

if (-123 != 0 ) ==> True (yes you have).

if (5 != 0 ) ==> true (yes you have).

good like

Share:
11,213
Admin
Author by

Admin

Updated on June 14, 2022

Comments

  • Admin
    Admin almost 2 years

    I am studying for an OS quiz and I did not understand what output

    if(fork())
        fork()
    

    will produce. Can someone explain?

    I didn't understand this line:

    if(fork())
    

    Edit:

    What I meant with "output" is how many processes will be there if this code was executed.

    Sorry I'm a bit dizzy after studying.