Hi guys and girls,
I am working on a program that uses forking.
I understand that when u fork, the current process is cloned and the parent and child process start at the next line after the fork process.
So i want to make 2 child processes. I mean I want to spawn one child that will generate a random number and will return with that exit code and another child that does the same.
But i cant fork from inside the first fork rite cuz that would make the second child a child of the first child which is the child of the parent.
So i have to fork AFTER the first child exits rite?
so that the second is a child of the parent as well.
cant paste my code cuz its on a unix console but i will write it out.
Code:
// proper includes...
int main{
int result1 =-1, int result2=-2;
pid_t pid;
// first child spawned
pid = fork()
if pid == 0
result1 = player();
exit(result1);
pid = fork()
if pid == 0
result2 = player();
exit(result2);
}
int player{
srand(10);
// returns 1 or 0
int i= rand()%1
return i;
}
Am getting wierd numbers, i dont want a solution just guide me on how to properly use fork, exit, and wait
thank you very much