[Date Prev][Date Next] [Thread Prev][Thread Next]
[Thread Index]
[Date Index]
[Author Index]
Re: fork problem
- From: Erik Mouw <J A K Mouw its tudelft nl>
- To: gtk-app-devel-list redhat com
- Subject: Re: fork problem
- Date: Mon, 14 Jun 99 15:14:48 +0200
On Mon, 14 Jun 1999 11:55:16 +0000, gtk-app-devel-list@redhat.com (Wang XinKai) wrote:
> I'm a new user of GTK, I meet a strange problem when I trying to use it.
> In my programe, I fork a new process to open a new "man window", as
> below.
>
> if(!fork())
> {
> system("nxterm -T 'Alloc' -n KMan -e man /usr/man/man3/Alloc.3");
> exit(0);
> }
Although this doesn't cause your problems, this is not the correct way to
use fork(). The default fork() code snippet is:
pid_t pid;
int status;
pid = fork();
if(pid == -1)
{
perror("fork");
exit(-1);
}
else if(pid == 0) /* child */
{
/* child code */
system("nxterm -T 'Alloc' -n KMan -e man /usr/man/man3/Alloc.3");
_exit(0); /* note the use of _exit() instead of exit();
}
else
{
/* parent code: just wait for the child to exit */
/* you can also install a signal handler for SIGCHLD */
waitpid(pid, &status, WUNTRACED); /* anti zombie */
}
> The new window appeared succesfully, and the orignal process continue to
> run. But the problem is every time when I close the man page (new
> window)
> my main program will dump a big core for me. :(
>
> Gdk-ERROR **: an x io error occurred
> aborting...
> Aborted (core dumped)
>
> and sometimes tell me:
> Xlib: unexpected async reply (sequence 0x10f5)!
You use exit() in the child. This will call all atexit() functions,
including ones that close all file descriptors which are shared between
parent and child. One of those descriptors is the X socket: the child
closes the socket, so the parent can't work anymore. Call _exit() instead
of exit().
> when I use gdb to see the core file, I can see the stack state when core
> dump:
>
> #0 0x401dc111 in __kill ()
> #1 0x401dbd66 in raise (sig=6) at ../sysdeps/posix/raise.c:27
> #2 0x401dd447 in abort () at ../sysdeps/generic/abort.c:88
> #3 0x402c0721 in g_logv ()
> #4 0x402c07d2 in g_log ()
> #5 0x40138ae5 in gdk_x_io_error ()
> #6 0x40311a5d in _XIOError ()
> #7 0x4030f59e in _XRead ()
> #8 0x4030efb3 in _XEventsQueued ()
> #9 0x40303998 in XPending ()
> #10 0x40144998 in gdk_event_prepare ()
> #11 0x402be5a3 in g_main_iterate ()
> #12 0x402be9b9 in g_main_run ()
> #13 0x400a0e4a in gtk_main ()
> #14 0x8049c0c in main (argc=1, argv=0xbffff914) at menu.c:74
> #15 0x401d5cb3 in __libc_start_main (main=0x8049bbc <main>, argc=1,
> argv=0xbffff914, init=0x8049568 <_init>, fini=0x804b668 <_fini>,
> rtld_fini=0x4000a350 <_dl_fini>,
>
> I don't think this can help me solve the problem.
Yes, this shows the problem: _XRead() fails because the socket is closed,
so it calls _XIOError(), which on its turn calls the GDK X IO error
handler, which decides to terminate the program.
> by the way, the same sort of code runs perfectly under Motif.
> Is there any difference about this between motif and gtk???
Maybe Motif has its own atexit() function that calls _exit(). Motif is
just too hairy to catch the error, GTK is clean ;-).
For more info on fork() and friends, read "Advanced programming in the
Unix environment" by Richard Stevens (Addison Wesley).
Erik
--
J.A.K. (Erik) Mouw, Information and Communication Theory Group, Department
of Electrical Engineering, Faculty of Information Technology and Systems,
Delft University of Technology, PO BOX 5031, 2600 GA Delft, The Netherlands
Phone: +31-15-2785859 Fax: +31-15-2781843 Email J.A.K.Mouw@its.tudelft.nl
WWW: http://www-ict.its.tudelft.nl/~erik/
[Date Prev][Date Next] [Thread Prev][Thread Next]
[Thread Index]
[Date Index]
[Author Index]