Childexittest

180 views
126 views

Published on

Program to find out whether the child process died normally or abnormally.If It died abnormally then the program finds out the signal which caused it .

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
180
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Childexittest

  1. 1. /* The wait systems waits for the Child process to exit if the child process isstill under exection and wait call returns with the process ID of the exited child process and itstores 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 ABORTsignal no 6 *when child tries to divide a number by 0 , it will be killed bysignal no 8 but the status obtained by the waitpid system call doesnt give an idea aboutwhether the child process terminated normally or abnormally to find it out we can use these 2 macros defined in <sys/wait.h> andstdlib.h> -if the process has terminated nomrally we can find out the exit statusising these macros -if the process had terminated abnormally by an Signal then we can find outthe Signal number of the signal which killed the child process the status obtained by wait system call has to be passed as an argement tothese macros these macros are 1)WIFEXITED(status)Description: Returns True if status was returned for a child that terminated normally. Inthis 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, byreceipt of a signal that it didnt 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. 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 terminationinfo*/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 parentleft*/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 terminationinfo*/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,onlyparent left*/if (wait(&status) != pid) /*parent waiting for child process to end*/ err_sys("wait error");Info_on_termination_of_child(status);exit(0);}

×