Wait for process to change state # include <sys / types.h> # include <sys / wait.h> P id – t Wait (int * status) P id – t Wait (p id – t p id, int * status , int options); Int wait id (id type id type, id – t id, sigin to – t * in fop, int options);
The wait () system call suspends executor of the current . process until one of its children terminates the call wait (& status ) is equivalent to Wait p id (- 1, & status , 0); The wait p id () system call suspends execution of the current a child specified by p id argument has changed state , by default wait p id () waits only for terminated children . but this behavior is modified via the options argument as described below < -1 - meaning wait for any child process whose process group ID is equal to the absolute value of P id -1 - meaning wait for any child process 0 - meaning wait for any child process whose process group ID is equal to that of calling process >0 - meaning wait for the child whose process ID is equal to value of Pid Wait () - is only for parent to wait and not child to wait () for parent & exited value is calls. wait for child exd, this a voids child to be Wait () - on success, returns the process ID of the terminated child , on error , -1 is returned. Wait P id() - on Success , returns the process ID of the child whose state has changed ; on error , -1 is returned if ‘WNO HAN &’ was specified and no child specified by p id has yet changed state then o is returned . Wait id () - Returns 0 on success or if WNO HAN & Was specified and no child specified by id has yet changed state ; on error , -1 is returned.
ECHID - (for wait ()) the calling process does not have any un waited – for children ECHILD - (for wait p id () or wait id ()) [ The process specified by p id (wait p id ()) or id type and id (wait id ()) does not exits or is not a child of the calling process . (This can happen for one’s own child if the action for SIGCHILD is set to SIG. IGN) ] EINUAL - The options argument was in valid EINTR - WNDHANG was not set and an unblocked signal or a SIGCHILD was caught.
A separate copy of data will be created , whenever parent/ child modifies the common data. #include <stdio.h> #include <stdlib.h> Int g = 0; Main () { Int l = 0; P F (“l & g modified to l= 20, g = 10 globally \n ”); L = 20; G= 10; If (fork () ) { PF (“Parent : l = %d, g = %d\n”, l ,g); } Else { PF (“Child : l = %d, g = %d\n”, l ,g); L ++; G++; PF (“l ++, g++ done in child \ n”); PF (“child : l + % d, g = %d \n” l , g); } } Output L = 0, g =0 L & g modified to l = 20, g = 10 globally Child : l =20, g =10 L++, g ++ done in child Child : l =21, g =11 Parent : l =20, g =10
So, data modified by in child is not reflected in parent. Name -exit, - Exit – terminate the current process #include <unistd.h> #include <stdlib.h> Void – exit (int status ) Void – Exit (int status )
Return Value
#include <stdio. h> #include <stdlib.h> Main () { If (fork ()) { Int st, ret Pf(“parent executing \ n”); Pf(“parent Waiting \ n”); Ret = Wait (& st); Pf(“child exited with % d as collected in MSB \ n”, st); St = st >> 8 ; // Hence it is shifted 8 Times Pf(“child exited with % d \ n”, st); Pf(“parent about to exit \ n”); Exit (0); // this exit value is returned to bash } Else { Int t ; Srand (get p id ()); T = rand () % 10 +1 ; Sleep (t); Exit (i); } } Output Parent executing Parent waiting Child exited with 256 as collected in MSB Child exited with 1 Parent about to exit W. a. p to input two small integers (in between 1 to 50) from command line create a new process by fork code. The sum of two integers by the child process and print the result Note These should not be any print statement in child #include <stdio. h> #include <stdlib.h> Main (int argc, char ** arg v) { If (arg c ! =3) { Pf (“Usage : ./a.out <Integer 1> <Integer 2> ” /n); Return; } If (fork ()) { Int st, ret; Ret = wait (& st); St = st >>8; Print F(“Sum = % d /n”, st); Exit (0); } Else { Int a, b ; A = a to I (arg V [1]); b= a to I (arg V [2]); exit (a + b); } } Output ./a.out 12 14 Sum = 26
#include <stdio. h> #include <stdlib.h> Main () { If (fork ()) { If (fork ()) { If (fork ()) { //parent process Int st , ret; PF (“ parent : p id = % d, p pid = % d/n” get p id (), get p id ()); While (ret ! = -1) { Ret = wait (& st ), St >>=8; If (st = = 1) PF (“child 1 exited with % d /n ”, st); If (st = = 2) PF (“child 2 exited with % d /n ”, st); If (st = = 3) PF (“child 3 exited with % d /n ”, st); } Exit (0); } Else { //child 3 process Int t; Srand (get p id ()); T = rand () % 10 +1; Sleep (t); PF (“child 3 : p id = % d, p pid = %d \ n ”, get p id (), get p pid ()); Exit (3); } } Else { Int t; Srand (get p id ()); T = rand () % 10 +1; Sleep (t); PF (“child 2 : p id = % d, p pid = %d \ n ”, get p id (), get p pid ()); Exit (2); } } Else { //child 1 process Int t; Srand (get p id ()); T = rand () % 10 +1; Sleep (t); PF (“child 3 : p id = % d, p pid = %d \ n ”, get p id (), get p pid ()); Exit (1); } } OUTPUT Here no child is exited after execution. Hence no child becomes orphan. (ii) Make the parent wait only for 2nd child . If exit states is ‘2’, then wait () #include <stdio.h> #include <stdlib.h> Main () { If (fork ()) { If (fork ()) { If (fork ()) { //parent process Int st, ret; PF(“Parent : p id = % d, p pid = % d\n ”, get p id (), get p pid ()); While (ret ! =1) { Ret = wait (& st) St>>8; If (st = = 2) { Pf (“child 2 exited with % d \n ” , st); Exit (0); } Else { //child 3 process Int t ; Srand (get p id ()); T = rand () % 10 +1; Sleep (t); Pf(“Child : p id = % d, p pid = % d\n ”, get p id (), get p pid ()); Exit (3); Else { //child 2 process Int t ; Srand (get p id ()); T = rand () % 10 +1; Sleep (t); Pf(“Child 2 : p id = % d, p pid = % d\n ”, get p id (), get p pid ()); } } Else { //child 1process Int t ; Srand (get p id ()); T = rand () % 10 +1; Sleep (t); Pf(“Child 1 : p id = % d, p pid = % d\n ”, get p id (), get p pid ()); } } Output: Hence is child 2 is executed , then child 1 & child 3 becomes orphan. Depending on, random generator of child 2 execution the process that are executed after child 2 becomes orphan.
You liked the article?
Like: 0
Vote for difficulty
Current difficulty (Avg): Medium
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.