fork() and system()

fork() and system()

Post by Francisco Obisp » Fri, 15 Sep 2000 04:00:00



[ Attached Message ]

From:
To:
Date: Wed, 13 Sep 2000 12:07:31 +0400
Local: Wed, Sep 13 2000 4:07 am
Subject: fork() and system()
Hello...

    I'm writing a program which reads from the serial port and then
executes an external program (a perl script) using system...

everything works Fine... but the thing is, that I don't want the main
program to wait for the process to finish with waitpid(pid,&status,0) I
want my c prog to execute but as soon as system my program fork()s for
system(), return to the reading state, regardless of how much time does
it take to perform the external execution..... if I remove the waitpid()
line I get a zombie process for every system call I make... do I have to
use a threads library or something similar?? can I do it with fork???

the reason I care about this is because I might miss a read if the
external program takes too long.. so I basically want to be reading from
the serial port the most I can..

Thankx

 
 
 

fork() and system()

Post by Barry Margoli » Fri, 15 Sep 2000 04:00:00




Quote:>    I'm writing a program which reads from the serial port and then
>executes an external program (a perl script) using system...

>everything works Fine... but the thing is, that I don't want the main
>program to wait for the process to finish with waitpid(pid,&status,0) I
>want my c prog to execute but as soon as system my program fork()s for
>system(), return to the reading state, regardless of how much time does
>it take to perform the external execution..... if I remove the waitpid()
>line I get a zombie process for every system call I make... do I have to
>use a threads library or something similar?? can I do it with fork???

The comp.unix.questions FAQ (available on www.faqs.org) has a section on
zombies that answers this in some detail.  Briefly, your options are:

1) Have the child immediately fork a grandchild, and then have the child
exit; the external execution should be performed in the grandchild.  The
parent's waitpid() will return as soon as the child exits, so it shouldn't
have to wait long.  The grandchild will be adopted by init.

2) Create a signal handler for SIGCHLD, and call wait() in it.

3) On some versions of Unix, setting the handler for SIGCHLD to SIG_IGN
causes zombies to be reaped automatically.

--

Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.