SlideShare a Scribd company logo
1 of 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);
}

More Related Content

More from Karthic Rao

More from 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
 

Recently uploaded

How to Manage Closest Location in Odoo 17 Inventory
How to Manage Closest Location in Odoo 17 InventoryHow to Manage Closest Location in Odoo 17 Inventory
How to Manage Closest Location in Odoo 17 InventoryCeline George
 
philosophy and it's principles based on the life
philosophy and it's principles based on the lifephilosophy and it's principles based on the life
philosophy and it's principles based on the lifeNitinDeodare
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjMohammed Sikander
 
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatmentsaipooja36
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppCeline George
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................MirzaAbrarBaig5
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhleson0603
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽中 央社
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文中 央社
 
Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).Mohamed Rizk Khodair
 
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...Denish Jangid
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnershipsexpandedwebsite
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxMohamed Rizk Khodair
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024Borja Sotomayor
 
How to Analyse Profit of a Sales Order in Odoo 17
How to Analyse Profit of a Sales Order in Odoo 17How to Analyse Profit of a Sales Order in Odoo 17
How to Analyse Profit of a Sales Order in Odoo 17Celine George
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...EduSkills OECD
 

Recently uploaded (20)

How to Manage Closest Location in Odoo 17 Inventory
How to Manage Closest Location in Odoo 17 InventoryHow to Manage Closest Location in Odoo 17 Inventory
How to Manage Closest Location in Odoo 17 Inventory
 
philosophy and it's principles based on the life
philosophy and it's principles based on the lifephilosophy and it's principles based on the life
philosophy and it's principles based on the life
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 
“O BEIJO” EM ARTE .
“O BEIJO” EM ARTE                       .“O BEIJO” EM ARTE                       .
“O BEIJO” EM ARTE .
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).
 
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptx
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
How to Analyse Profit of a Sales Order in Odoo 17
How to Analyse Profit of a Sales Order in Odoo 17How to Analyse Profit of a Sales Order in Odoo 17
How to Analyse Profit of a Sales Order in Odoo 17
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 

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