[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: fork()/exec() dumping



On Tue, Jun 22, 1999 at 09:14:44PM +0200, Erik Mouw wrote:
...
> This is becoming a FAQ. Never mind, here we go again:

Indeed, this should be in a FAQ somewhere.  Your program below isn't
very clear either:


>   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();
>       /* of course you can also use exec() over here */
>     }
>   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 */
>     }

system() will also do a fork(), followed by exec() and wait().
Further more, system() fork's /bin/sh, to interpret the command, which
unless it's a builtin sh command, will then fork *AGAIN* to carry out
the command.

Effectively, there are now three child processes where most likely,
most people just want one.

Your code example is a good idea, but the reasons behind using _exit()
instead of exit() clearly needs to be stuck in a FAQ (and not any of
the invisible GTK documentation).

regards,
--andy



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]