SlideShare uma empresa Scribd logo
1 de 2
/* 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));
}

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);
}

Mais conteúdo relacionado

Mais de Karthic Rao

Mais de Karthic Rao (8)

Scaling your GraphQL applications with Dgraph
Scaling your GraphQL applications with DgraphScaling your GraphQL applications with Dgraph
Scaling your GraphQL applications with Dgraph
 
Forkexpe
ForkexpeForkexpe
Forkexpe
 
Usrinfogetpwuid
UsrinfogetpwuidUsrinfogetpwuid
Usrinfogetpwuid
 
Countrytime.c
Countrytime.cCountrytime.c
Countrytime.c
 
Timesleep
TimesleepTimesleep
Timesleep
 
Time2.c
Time2.cTime2.c
Time2.c
 
Fork.c
Fork.cFork.c
Fork.c
 
2 buffer overflows
2 buffer overflows2 buffer overflows
2 buffer overflows
 

Último

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Último (20)

Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 

Childexittest

  • 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); }