Hi. I'm a product of the "new" generation of computer science students. Meaning, I was taught Java up until college, then I learned a bit of Assembly, and now that I'm in my senior year, I'm getting the crap kicked out of me because a) Java doesn't cover many bases such as addresses, garbage collection, and all the other things that seem to be essential to the fundamentals of programming languages and b) Java seems to be very complex when it comes to such machine-close operations such as using threads and processes.
Anyway, the good thing about this year is that I'm learning about 7 languages between my classes -- but there is a lot of stuff I don't know how to do.
Just like fork().
All the examples (in my textbook and online) seem to just fork() the parent process into having one child.
I have an assignment due that requires me to create 8 processes. 3 of the processes are supposed to have 2 children each.
What I can't seem to wrap my head around is how to keep tabs of which process you are currently using! Logically, my mind tells me to create the parents first, and have their children create children. But the code for processes is bizarre!
Code:
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
int pid = fork();
if (pid > 0)
{
printf ("I'm the parent! I just created child pid (%d)!\n", pid);
}
else if (pid == 0)
{
printf ("I'm the child! My pid is (%d)\n", getpid ());
}
else if (pid < 0)
{
perror ("Ouch! Unable to fork() child process!\n");
exit (1);
}
return 0;
}
While I get this, I don't understand which process is currently being looked at. I also don't know how to make grandchildren. I don't know where to place the fork() calls, or even how to do it. I can't find any good examples online (I've been searching for 3 hours!).
I don't want my homework done for me, I just need to learn how to determine which process I'm currently on, and how to work from there. It's a very abstract concept compared to the rest of the OOP stuff I've done before. I'm extremely stuck....
Thanks for any and all help.
Edit: Looking at the code I'm understanding how pid changes, but I'm still confused as to how I work it into those if statements, and how to make grandchildren with those if statements.