Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

forks and its functionality?

Tags:

c

fork

I have the following code and am asked to how many times "A", "B", "C", "D", "E" will be printed

fun() {
  printf("A");
  fork();
  printf("B");
  if (fork() != 0) {
     printf("C");
     fork();
     printf("D");
  }
  printf("E");
}

so it should be:

A
A 
B
E

im not sure if my answer above is correct? and what the line if(fork() !=0 ) do?

like image 718
PhoonOne Avatar asked May 19 '26 19:05

PhoonOne


1 Answers

From the documentation:

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.

like image 134
koopajah Avatar answered May 22 '26 10:05

koopajah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!