What is the output of the following code? #include <stdlib.h> #include<sys/types.h> #include<sys/wait.h> #include<unistd.h> int main(int argc, char…

What is the output of the following code?

#include <stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
int main(int argc, char *argv[]){
    int i;
    for(i=0;i<4;i++){
        int pid = fork();
        if(pid==0){
            printf("%d\n",i);
            exit(0);
        }
    }
    for(i=0;i<4;i++){
        wait(NULL);
    }
    return 0;
}

From the BAERA Assistant Engineer (CSE) 2023 – Written (BUET) exam, question 1 of 1.

More from this paper