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!
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.