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
retis true, then only the right hand side portion is evaluated, which eventually calls thefork ()system call. The return value of the call is ANDed with the current value ofret, and reassigned toret.Basically it works like
Read this:
Consider the fork tree below. Each tree “node” shows the sequence of executed statement in each individual statement. One work in one line.
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
retnow contains 1, which is printed in the second printf. asretis 1,ifis 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.ifbody is entered, andfork ()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.ifis entered,fork ()is executed, which makes (p5). Now p4 proceeds to print Hello.p4
starts from
ifbody, gets out and prints Hellop5
starts from
ifbody, gets out and prints HelloAbove 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.