Can a task fork and the child be a task?



next up previous contents
Next: About this document Up: Advanced programming Previous: Can I spawn

Can a task fork and the child be a task?

Yes, but you must first disconnect the child from PVM manually. Otherwise, both tasks will attempt to talk to the pvmd through the same socket, using the same task context, and things will get scrambled.

The following code fragment can be used to fork and reconnect the child as another PVM task:

    if (cc = fork()) {    /* parent */
        if (cc == -1)
            perror("fork");

        /*
        *  parent does stuff here
        */

    } else {              /* child */
        pvmendtask();
        pvm_mytid();

        /*
        *  child now enrolled as separate task
        */
    }

Note that the child task gets an anonymous context - as though it was not spawned by PVM. Most importantly, it does not have PVM output redirection. However, if the parent task stays alive at least as long as the child, the child's output will be collected through the pipe to the pvmd as though the parent had printed it.



Bob Manchek
Fri Mar 3 15:08:11 EST 1995