I am currently studying the fork () function in C. I understand what it does (I think). My question is why are we checking it in the next program?
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main() { int pid; pid=fork(); if(pid<0)/* why is this here? */ { fprintf(stderr, "Fork failed"); exit(-1); } else if (pid == 0) { printf("Printed from the child process\n"); } else { printf("Printed from the parent process\n"); wait(pid); } }
In this program, we check if PID <0 is returned, which indicates a failure. Why does fork () not work?
Juicy source share