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.
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.