[GRASS5] R_open_driver() after fork()

Radim Blazek blazek at itc.it
Thu Feb 13 09:08:50 EST 2003


Thanks for help,

On Thursday 13 February 2003 12:34 pm, Eric G. Miller wrote:
> On Thu, Feb 13, 2003 at 11:23:29AM +0100, Radim Blazek wrote:
> > Hi,
> >
> > I have one problem: if module creates child process by fork(),
> > it is impossible open driver by R_open_driver() in parent.
> > In sync_driver() it hangs on read(). SIGALRM is recieved and dead()
> > executed, but module doesn't continue in execution - still hanging on
> > read(), until child is killed.
>
> The following seems to work okay for me.  Perhaps you are trying to have
> both parent and child connected to the same server at the same time?
> The display server architecture only will process one connection at a
> time.

No, just from parent.

> #include <stdio.h>
> #include <sys/types.h>
> #include <unistd.h>
> #include "gis.h"
>
> int main (int argc, char *argv[])
> {
>    pid_t pid;
>    G_gisinit(argv[0]);
>
>    if ((pid = fork()) ==  0) {
>         /* child */
>         sleep(5);
>    } else if (pid > 0) {
>         /* parent */
>         if (R_open_driver() == 0) {
>             fprintf (stderr, "Driver Opened\n");
>             R_close_driver();
>             fprintf (stderr, "Driver Closed\n");
>         } else {
>             /* failed to open driver ... */
>             G_fatal_error("Failed to open driver\n");
>         }
>    } else {
>        /* fork() failed */
>        G_fatal_error ("Fork failed!\n");
>    }
>
>    if (pid > 0) {
>        int status;
>        waitpid (pid, &status, 0);
>    }
>
>    return 0;
> }

Yes, this works, problem is if fork() is called with opened monitor:

int main (int argc, char *argv[])
{
    int i;
    pid_t pid;
    G_gisinit(argv[0]);
 
    if (R_open_driver() == 0) fprintf (stderr, "Driver Opened\n");
    else G_fatal_error("Failed to open driver\n");
 
    if ((pid = fork()) ==  0) { /* child */
        sleep(5);
        fprintf (stderr, "Child end\n");
    } else if (pid > 0) { /* parent */
        R_close_driver();
        fprintf (stderr, "Driver Closed\n");
 
        if (R_open_driver() == 0) {
            fprintf (stderr, "Driver Opened\n");
            R_close_driver();
            fprintf (stderr, "Driver Closed\n");
        } else {
            G_fatal_error("Failed to open driver\n");
        }
    } else { /* fork() failed */
       G_fatal_error ("Fork failed!\n");
    }
 
    if (pid > 0) {
       int status;
       waitpid (pid, &status, 0);
    }
 
    return 0;
}

Thank you anyway, with help of your example I discovered where 
the problem is.

Radim




More information about the grass-dev mailing list