SlideShare a Scribd company logo
/* 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

Scaling your GraphQL applications with Dgraph
Scaling your GraphQL applications with DgraphScaling your GraphQL applications with Dgraph
Scaling your GraphQL applications with Dgraph
Karthic Rao
 
Forkexpe
ForkexpeForkexpe
Forkexpe
Karthic Rao
 
Countrytime.c
Countrytime.cCountrytime.c
Countrytime.c
Karthic Rao
 
Timesleep
TimesleepTimesleep
Timesleep
Karthic Rao
 
Time2.c
Time2.cTime2.c
Time2.c
Karthic Rao
 
Fork.c
Fork.cFork.c
Fork.c
Karthic Rao
 
2 buffer overflows
2 buffer overflows2 buffer overflows
2 buffer overflows
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 Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 

Recently uploaded (20)

How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 

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