fork() question...

Status
Not open for further replies.

Ajar

Ars Tribunus Angusticlavius
9,701
Subscriptor++
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:

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...
 

earl

Well-known member
3,066
Um, I don't think you quite know what you are talking about. First of all, fork creates processes, not threads -- there is a big difference. Second, when you fork, you need the return of 0 to tell which copy is the child copy given that you copy the entire memory space, etc. So to answer your second question, you know that a given process is the child if fork returns 0 to that process. You only create one process at a time with fork, so what is the problem?

If you want multithreading, look at pthreads or your system library -- each OS does it differently if you don't use pthreads.

Um, and of course each child process has a unique identifier; it's the PID returned to the spawning process when you fork. So pseudocode:

code:<HR>
Code:
while (want more children)
{
  if 
    fork is 0 then break (I'm a child and I only want the parent to fork)
  else
    add the return to fork to an array (it's the PID of the child)
}<P>if fork returned 0 then (know I'm a child)
  establish a link with the parent -- choose your IPC mechanism (named pipes and sockets are both nice)

If you don't explicitly tell the children the pid of the creating process, look at getppid
And if the children want to know their own pid, look at getpid

-earl-
 

Ajar

Ars Tribunus Angusticlavius
9,701
Subscriptor++
EB - *light bulb goes on*

Thanks a million! :D

...now it gets messy... :)

I've got to get a handle on this signal thing, though. I can't even kill the child processes at the moment... :oops:

_________________________

Edit: Yes, I know they aren't threads, and I know that they have access to the forking process's entire memory space. I had missed that fork's return value wasn't equivalent to the PID of the child process... as EB pointed out. Also, pthread.h isn't avaliable in Darwin AFAIK...
 

Ajar

Ars Tribunus Angusticlavius
9,701
Subscriptor++
Well, it has been "Suggested(tm)" by the prof that we use signals... i.e. he only showed us signals and has not mentioned the word "pipe..." since this is my first *nix experience, I don't know how to pipe :) ...which leaves me with signals.

I've got to write signal handlers, but I also have to figure out how to combine them with setjmp and longjmp to get my context switching between children...
 

JiveTurkey

Ars Tribunus Angusticlavius
6,299
Subscriptor
If you create an array (size 2) of ints and pipe it you will have created a pipe. From the man page:
pipe creates a pair of file descriptors, pointing to a
pipe inode, and places them in the array pointed to by
filedes. filedes[0] is for reading, filedes[1] is for
writing.
So if you create a pipe before you fork the child will inherite the pipe and you can read/write to/from the pipes like you would a normal file. Its just another method of IPC.

EDIT - fork bombs rule.
 

Ajar

Ars Tribunus Angusticlavius
9,701
Subscriptor++
I did a little bit of reading about pipes, and they're great, but I'm supposed to be using signals... though if I can't accomplish what I need to with signals I'll have to resort to pipes ;)

Right now, though I have IPC between my two forked child processes and my main() :D

Victory!

Well, until I have to learn setjmp/longjmp.

[This message was edited by twinky on November 30, 2001 at 12:51.]
 
Status
Not open for further replies.