As I fork() 2 childs from the same parent, I would like to keep my first child's pid and pass it to the 2nd child. Question is how do I do it? I've tried global variables or maybe I did it incorrectly.
From the start, I was told by my lecturer that global variables are bad so I didn't read much about it. Here's a piece of the code...
Code:
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <signal.h>
#include <errno.h>
int firstID;
int main(){
int id;
if(fork() == 0){
id = (int)getpid();
printf("I am child : %d\n", id);
firstID = id;
}
else{
if(fork() == 0){
printf("My First child's ID : %d\n", firstID);
}
}
return 0;
}
When I print the pid on the 2nd fork, it shows me '0'.