1. /* The wait systems waits for the Child process to exit if the child process is
still under exection and
wait call returns with the process ID of the exited child process and it
stores the childs
exit status in the argument passed to it
pid_t wait(int *status);
But there are 2 different ways by which a child process can exit
1) Normal Termination
-This happens when the child process calls exit() or return
2) Abnormal termination
-this happens when the child process is terminated by by an SIGNAL
*when child calls abort system call , it will be killed ny ABORT
signal no 6
*when child tries to divide a number by 0 , it will be killed by
signal no 8
but the status obtained by the waitpid system call doesnt give an idea about
whether the
child process terminated normally or abnormally
to find it out we can use these 2 macros defined in <sys/wait.h> and
stdlib.h>
-if the process has terminated nomrally we can find out the exit status
ising these macros
-if the process had terminated abnormally by an Signal then we can find out
the Signal
number of the signal which killed the child process
the status obtained by wait system call has to be passed as an argement to
these macros
these macros are
1)WIFEXITED(status)
Description:
Returns True if status was returned for a child that terminated normally. In
this case, we can
execute WEXITSTATUS(status) to fetch the value that the child passed to exit,
_exit,or _Exit.
2)WIFSIGNALED(status)
Returns True if status was returned for a child that terminated abnormally, by
receipt of a signal
that it didn't catch. In this case, we can execute WTERMSIG(status)
*/
#define err_sys(msg) write(1,msg,50)
#include <stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include <sys/wait.h>
#include<sys/types.h>
void Info_on_termination_of_child(int status)
{
if (WIFEXITED(status))
printf("normal termination, exit status = %dn",WEXITSTATUS(status));
else if (WIFSIGNALED(status))
printf("abnormal termination, signal number = %dn",WTERMSIG(status));
2. }
int main(void)
{
pid_t pid;
int status;
if ((pid = fork()) < 0)//creating a child
err_sys("fork error");
else if (pid == 0) /* child process*/
exit(7); /*child process exiting normally ,only parent left*/
if (wait(&status) != pid) /*parent waiting for child */
err_sys("wait error");
Info_on_termination_of_child(status);/*calling the function to get termination
info*/
if ((pid = fork()) < 0)//forking to create a child again
err_sys("fork error");
else if (pid == 0)/*child proces*/
abort();/*Abnormal termintionof child process by ABORT signal only parent
left*/
if (wait(&status) != pid)/*parent waiting for child to exit*/
err_sys("wait error");
Info_on_termination_of_child(status);/*calling the function to get termination
info*/
if ((pid = fork()) < 0)
err_sys("fork error");
else if (pid == 0)
status =(status/0);
/* Division by Zero error generates SIGNAL.NO 8 which kills the child,only
parent left*/
if (wait(&status) != pid) /*parent waiting for child process to end*/
err_sys("wait error");
Info_on_termination_of_child(status);
exit(0);
}