Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!iuvax!cica!ctrsol!ginosko!uunet!apctrc!zjmw0a From: zjmw0a@apctrc.trc.amoco.com (Joe M. Wade) Newsgroups: comp.windows.x Subject: fork() and popen() in an X environment Message-ID: <1015@apctrc.UUCP> Date: 7 Sep 89 22:01:23 GMT Reply-To: zjmw0a@apctrc.trc.amoco.com (Joe M. Wade) Distribution: na Organization: Amoco Production Company, Tulsa Research Center Lines: 104 In an effort to separate functionality between programs, one of my colleagues (cohorts?) produced code using popen to start another process which would use X to plot data created by the initial process. He then found the initial process had to be forked so that the command widget which spawned the popen would return to its sensitive state. Everything works hunky-dory except for one thing-the initial process dies when the quit button is pushed, but the window doesn't go away until the spawned-off process is also exited. Killing the window manager didn't help, although it seems as though that would probably be the source of the difficulty. Below is a short piece of code demonstrating the problem. It produces two buttons, fork and quit. If you hit fork, it does a popen of this same process. Try using the quit button of the original process and its pid disappears, but the windows remain. Pertinent info: Sun 3/60 OS 4.0.1, X11R3, twm #include #include #include #include #include #include #include #include #include #include void quit(); void fork_process(); main(argc, argv) int argc; char *argv[]; { extern void time_out(); Widget toplevel, outerform, quitter, forker; int screen; Display *display; Arg arg[5]; int fork_value; /* * fork this process */ if ( (fork_value = fork()) != 0 ) exit(0); toplevel = XtInitialize(NULL, "XLabel", NULL, 0, &argc, argv); display = XtDisplay(toplevel); screen = XDefaultScreen(display); outerform = XtCreateManagedWidget("outerform", boxWidgetClass, toplevel, (Arg*) arg, NULL); XtSetArg(arg[0], XtNlabel, (String) "Quit"); quitter = XtCreateManagedWidget("Quit", commandWidgetClass, outerform, (Arg*) arg, ONE); /* * Create a command widget to popen a new process */ XtSetArg(arg[0], XtNlabel, (String) "Fork"); forker = XtCreateManagedWidget("Fork", commandWidgetClass, outerform, (Arg*) arg, ONE); XtAddCallback(quitter, XtNcallback, quit, &toplevel); XtAddCallback(forker, XtNcallback, fork_process, NULL); XtRealizeWidget(toplevel); time_out(); XtMainLoop(); } void fork_process() { FILE *fptr; /* * start a new process */ fptr = popen( "fork", "r"); pclose( fptr ); return; } void quit ( button, top_widget ) Widget button; caddr_t *top_widget; /* * destroy the top level widget and exit */ { XtDestroyWidget( *((Widget *) top_widget) ); exit (0); } Any info/input would be greatly appreciated. * * * * * * * * * * * * * * * * * * * * * * * Joe M. Wade (zjmw0a@apctrc.trc.amoco.com) (918) 660-4387 * * Amoco Research Center * 4502 E. 41st St. * Tulsa, OK 74102 * * * * * * * * * * * * * * * * * * * * * * *