Fork + Waitpid

L

lasek

Hi all,
i have made a simple script that use fork, waitpid and sigaction for
SIGCHLD.

#include<sys/types.h>
#include<sys/wait.h>
#include<stdio.h>
#include<errno.h>
#include<unistd.h>
#include<signal.h>

int pid;

void chld_signal(int sig)
{
int status,child_val;

printf("\n SIGHCHILD trapped \n");

if(waitpid(pid,&status,0)<0)
{
fprintf(stderr, "waitpid failed\n");
return;
}

if(WIFEXITED(status))
{
child_val=WEXITSTATUS(status);
printf("child's pid %d exited normally with status
%d\n",pid,child_val);
}
}

int main(int argc, char *argv[])
{
char *prog_argv[4];

int iCount;

struct sigaction chld_action;

/* Initialize the signal structure mask. */

chld_action.sa_handler=chld_signal;
sigemptyset(&chld_action.sa_mask);
chld_action.sa_flags=0;

if(sigaction(SIGCHLD,&chld_action,NULL)<0)
{
fprintf(stderr,"\nSigaction failed\n");
return 1;
}
else
printf("\nSigaction Success\n");


/*
** Build argument list
*/

prog_argv[0] = "/usr/bin/ls";
prog_argv[1] = "-lrt";
prog_argv[2] = "/usr1/sdc/corsis/stuff";
prog_argv[3] = NULL;

/*
** Create a process for the cmd */

if((pid=fork()) < 0)
{
fprintf(stderr,"Fork failed");
exit(errno);
}

if(!pid)
{
/* Child */

printf("\nExecuting command [%s]\n",prog_argv[0]);

printf("\nParent Pid %d\n\n",getppid());

execvp(prog_argv[0], prog_argv);
}
if(pid)
{
/*
* Parent
*/

printf("\nWe're in the parent; let's wait for the child pid [%d] to
finish \n",pid);

sleep(1);
}

return 0;
}

Now the problem, if i delete the 'sleep(1)' i can't see anything about all
the printf into chld_signal function.
If i put the 'sleep(1)' i can see all the printf, the code,status of
waitpid.

WHY ???

Thanks and have a nice week.
 
A

Artie Gold

lasek said:
Hi all,
i have made a simple script that use fork, waitpid and sigaction for
SIGCHLD.
[code snipped]

Sorry, as your question has nothing to do with standard C, we can't help
you here. Please try where your question
would be *on* topic!

HTH,
--ag
 
W

Walter Roberson

i have made a simple script that use fork, waitpid and sigaction for
SIGCHLD.

That's not a "script", that's a program.

fork() and waitpid() are not specified by the C standards. Nor are
several of the include files you list.
Now the problem, if i delete the 'sleep(1)' i can't see anything about all
the printf into chld_signal function.
If i put the 'sleep(1)' i can see all the printf, the code,status of
waitpid.

Try flushing your buffers. But moreso, try reading the
documentation about which functions may legally be called from
a signal handling routine. If you are looking at POSIX.1-1990,
it's in section 3.3.1.3 "Signal Actions".
 
M

Martin Ambuhl

lasek said:
Hi all,
i have made a simple script that use fork, waitpid and sigaction for
SIGCHLD.

You need to post this to a newsgroup for your implementation. None of
'fork', 'waitpid', 'sigaction', or 'SIGCHLD' have any meaning in C
itself. These are features of your implementation. Similarly, the headers
#include<sys/types.h>
#include<sys/wait.h>
> #include<unistd.h>
are not standard C headers, but are implementation-specific.

Your other 3 headers *are* standard ones:
#include<stdio.h>
#include<errno.h>
#include<signal.h>
[...]

Now the problem, if i delete the 'sleep(1)' i can't see anything about all
the printf into chld_signal function.
sleep() is not a standard C function. An implementation-specific
newsgroup or mailing list is what you want.
 
S

SM Ryan

# void chld_signal(int sig)
# {
# int status,child_val;
#
# printf("\n SIGHCHILD trapped \n");
#
# if(waitpid(pid,&status,0)<0)
# {
# fprintf(stderr, "waitpid failed\n");
# return;
# }
#
# if(WIFEXITED(status))
# {
# child_val=WEXITSTATUS(status);
# printf("child's pid %d exited normally with status
# %d\n",pid,child_val);
# }
# }

You should avoid calling non-reentrant functions from a signal handler; stdio
is generally nonreentrant.


# Now the problem, if i delete the 'sleep(1)' i can't see anything about all
# the printf into chld_signal function.

When the program exits, you should expect all of its signal/interrupt handlers
to be terminated at the same time unless you've arranged something with the
operating system to allow persistent handlers. If you don't sleep, the next
statement exits the program, probably before the child exits.

(On unix, you synchronise a parent with a child exit with the wait* family
functions (see also `man waitpid`). The SIGCHLD handler doesn't do the
synchronise; it merely lets you know a child is awaiting synchronisation.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top