hi , i need help please, i have the following code but im not sure about how many childs are created and if the parent executes the first if statement : if ((i & 1) && fork() == 0) , or not , since im confused about the term (i & 1) && fork() == 0.
if someone can draw the parent - child process tree and tell me please how that if statement is to be understood i would be very thankfull , i didnot add sleep() so you can check it as you want. please help.

#include <stdio.h>
#include <stdlib.h>


int main(int argc, char** argv) {

int i = 2;
for (i=2; i<=4; i++){

 printf("Hello %d\n", i);
 if ((i & 1) && fork() == 0){

 printf("OS %d\n", i);
 fork();
 if (fork() > 0)
 exit(1);
 printf("Students\n");
 }

 else
 printf("World\n");

}
return 0;
}

many thanks in advance

The expression i & 1 is true only if i is odd. This will happen only if i equals 3 in the loop. In that case fork() is executed and creates a child process. Let A be the main process and B the child process created at this moment. The if statement's body is only executed in process B (because that's where fork() returned 0). Then, the fork() at line 14 is executed, spawning a grandchild process say C. Line 15 is executed in both B and C, spawning another grandchild D and a grandgrandchild E. B and C exit at line 16 with status 1 while D and E both print Students and go to next loop with i == 4.

thanks , and the loop with i == 4 will print out Hello 4 and jump to line 21 , because 4 is an even number . thanks allot 10000 thanks.

Here is a python program with almost the same statements

from os import fork, getpid
from sys import exit

def main():
    i = 2
    while i <= 4:
        print('PID', getpid(), ': Hello', i)
        if (i & 1) and (fork() == 0):
            print('PID', getpid(), ': OS', i)
            fork()
            if fork() > 0:
                exit(1)
            print('PID', getpid(), ': Students', i)
        else:
            print('PID', getpid(), ': World', i)
        i += 1
    return 0

if __name__ == '__main__':
    exit(main())

""" my output -->
('PID', 7621, ': Hello', 2)
('PID', 7621, ': World', 2)
('PID', 7621, ': Hello', 3)
('PID', 7621, ': World', 3)
('PID', 7621, ': Hello', 4)
('PID', 7621, ': World', 4)
('PID', 7622, ': OS', 3)
('PID', 7625, ': Students', 3)
('PID', 7625, ': Hello', 4)
('PID', 7625, ': World', 4)
('PID', 7624, ': Students', 3)
('PID', 7624, ': Hello', 4)
('PID', 7624, ': World', 4)
"""

A is 7621, the child B is 7622, the grandchild and grandgrandchild are 7624 and 7625 (which is which ?)

thanks allot :) .
yesterday i tried ps -aux | grep myfork to see the processes and i entered some sleep() function calls inside my code and ran it . everything is ok and i could see A->B->C B->D C->E , im confused only at one line regarding the creation of the child B , will it be created at loop i=2 and the next loop it will execute the code inside the for loop? or it will be created at loop i=3 and will execute the code right after that?
becuase when the Parent A is trying to access the loop at i=2 i see already the Child B in the linux termial.
at i=2 --> (i&1)--> False // will fork() at the right side now execute?
and if yes the child B sould then work further with i=2 ? but if so it would printout OS 2 but it dont...

line : 11. if ((i & 1) && fork() == 0)

No in principle B is created only at loop i=3. Can you post the whole code ?

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv){
int i = 2;
for (i=2; i<=4; i++)
{
printf("Hello %d\n", i);
sleep(5);
if ((i & 1) && fork() == 0)
{
sleep(5);
printf("OS %d\n", i);
fork();
sleep(5);
if (fork() > 0) {

sleep(5);
exit(1); }

sleep(5);
printf("Students\n");
}
else
printf("World\n");
}
return 0;
}

You only need to add prints to see what's going on. Here is my version, with pid numbers. Everything seems to run the way we described above

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

#define SLEEP 1

int main(int argc, char** argv){
int i = 2;
for (i=2; i<=4; i++)
{
printf("Hello %d (pid %d, ppid %d)\n", i, getpid(), getppid());
sleep(SLEEP);
if ((i & 1) && fork() == 0)
{
sleep(SLEEP);
printf("OS %d (pid %d, ppid %d)\n", i, getpid(), getppid());
fork();
sleep(SLEEP);
printf("ready to fork %d (pid %d, ppid %d)\n", i, getpid(), getppid());
if (fork() > 0) {

sleep(SLEEP);
exit(1); }

sleep(SLEEP);
printf("Students %d (pid %d, ppid %d)\n", i, getpid(), getppid());
}
else
printf("World %d (pid %d, ppid %d)\n", i, getpid(), getppid());
}
return 0;
}

/*
 * Example output with comments
 * 
Hello 2 (pid 4870, ppid 4742)           4870 is A
World 2 (pid 4870, ppid 4742)
Hello 3 (pid 4870, ppid 4742)
World 3 (pid 4870, ppid 4742)
Hello 4 (pid 4870, ppid 4742)
World 4 (pid 4870, ppid 4742)
OS 3 (pid 4871, ppid 4870)              4871 is B
12:15 494489] ready to fork 3 (pid 4871, ppid 1804)   B is about to fork D
ready to fork 3 (pid 4872, ppid 4871)   4872 is C, about to fork E
Students 3 (pid 4874, ppid 4872)        4874 is E (parent C)
Students 3 (pid 4873, ppid 4871)        4873 is D (parent B)
Hello 4 (pid 4874, ppid 4872)
Hello 4 (pid 4873, ppid 4871)
World 4 (pid 4873, ppid 1804)
World 4 (pid 4874, ppid 1804)
 */

Edit: notice that B (4871) is adopted by init (1804) after A has died.

aha ok , understood :) .
should i also include : #include <unistd.h> ?

for what exactly are : #include <unistd.h> and #include <stdlib.h> ?

thanks allot , i will mark the Question as solved :) .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.