[comp.lang.c] errormsg in childprozess

frank@mute.ruhr.de (Frank Huemme) (03/24/91)

hello,
in a subroutine of a large C program i had to start another Programm
that nether ends.

ret=fork();
if (ret==0)			/* in child */
	{
	execlp(.........
	exit();
	}

My Problem:
What`s the best way for the child to tell the father-process if something
was going wrong with execlp ( and fork() was ok )??

	frank
-- 
Frank Huemme , 4600 DO 1, email: frank@mute.ruhr.de

gwyn@smoke.brl.mil (Doug Gwyn) (03/25/91)

In article <91.082.19:00:19@mute.ruhr.de> frank@mute.ruhr.de (Frank Huemme) writes:
>ret=fork();
>if (ret==0)			/* in child */
>	{
>	execlp(.........
>	exit();
>	}
>What`s the best way for the child to tell the father-process if something
>was going wrong with execlp ( and fork() was ok )??

This is a UNIX-specific question, better asked in comp.unix.questions.
However, the answer is that there are several possible solutions.  Note
first of all that "exit()" is incorrect; exit() requires an argument,
and that is one method of reporting failure to the parent, which if it
wait()s on the child will be able to use the exit status to determine
whether of not the child had a problem.  (Actually you should use _exit()
in this context, to avoid unwanted flushing of the forked copy of the
stdio buffers.)  If the parent is not waiting on the child, you could
send a signal from the child branch to the parent branch before the
child branch terminates, and have the parent use a signal handler to set
a flag that you can test at appropriate places.

bengsig@dk.oracle.com (Bjorn Engsig) (03/26/91)

[I'm taking this over to comp.unix.programmer)

Article <91.082.19:00:19@mute.ruhr.de> by frank@mute.ruhr.de (Frank Huemme) says:
|
|ret=fork();
|if (ret==0)			/* in child */
|	{
|	execlp(.........
|	exit();
|	}
|
|My Problem:
|What`s the best way for the child to tell the father-process if something
|was going wrong with execlp ( and fork() was ok )??
The problem is that it is much easier to test and report errors in the
mother program.  In the case above, you use the execlp call which will look
up in the PATH for you.  If you are willing to do this yourself (or you
already have the complete path of the new executable), you can do most of
the checking before calling fork().  You can easily check if the executable is
there, if it has execute permission, etc.  If so, do fork and exec, and you
can do with a simple and crude way of reporting errors, e.g. print to stderr,
and _exit(somevalue), since the most common errors are found even before
fork().

-- 
Bjorn Engsig, ORACLE Corporation, E-mail: bengsig@oracle.com, bengsig@oracle.nl