I'm just learning multithreading for the first time, and I'm using fork() to spin off new threads as child processes in UNIX environments (I know there are other ways, but the professor seems to like fork()...). My problem is that fork() gives the child a pid of zero, but I need to be able to differentiate between children (for example, if I want to kill one of them). Is there a way to give a child a nonzero pid after the fork? Here's my code:
As you can see, it just outputs
DAMN
YOU!
a few times, followed by
Watch your mouths, kids!
...Rinse & repeat. If, in main() (the parent), I want to, say, kill Child #1, how can I set it up to do that? Currently, the children do not in fact have unique identifiers...
Any and all help/suggestions/comments would be greatly appreciated...
code:<HR>Code:int main(){<P>pid_t CurrentID = getpid();<P>int ParentID = CurrentID;<P>//create first child with a fork if(CurrentID = fork() == 0){ while(1){ sleep(1); cout << "DAMN\n"; } }<P>//create second child if(CurrentID = fork() == 0){ while(1){ sleep(1); cout << "YOU!\n"; } }<P>while(1){ sleep(3); cout << CurrentID << "Watch your mouths, kids!\n"; }<P>return 0; }
As you can see, it just outputs
DAMN
YOU!
a few times, followed by
Watch your mouths, kids!
...Rinse & repeat. If, in main() (the parent), I want to, say, kill Child #1, how can I set it up to do that? Currently, the children do not in fact have unique identifiers...
Any and all help/suggestions/comments would be greatly appreciated...