Experimental content: There are 3 processes, one of which is the parent process, the remaining two are the child processes created by the parent process, one of the child processes runs the "ls-l" instruction, and the other child processes the exception exits after the 5s pauses, the parent process waits for the first child process to end in a blocking manner, and then waits for the other child process to exit Waiting for information to be collected to the end of the second child process, the parent process returns.
/*MULTI_PROC.C*/#include<stdio.h>#include<stdlib.h>#include<sys/types.h>#include<unistd.h>#include<sys/wait.h>intMainvoid) {pid_t child1, child2, child; /*Create two sub-processes*/Child1=Fork (); /*error handling for child process 1*/ if(Child1 = =-1) {printf ("Child1 Fork error\n"); Exit (1); } Else if(Child1 = =0)/*Call the EXECLP () function in child process 1*/{printf ("In child1:execute ' ls-l ' \ n"); if(EXECLP ("ls","ls","- L", NULL) <0) {printf ("Child1 EXECLP error\n"); } } Else /*Create process 2 again in the parent process, and then wait for two child processes to exit*/{child2=Fork (); if(Child2 = =-1)/*error handling for child process 2*/{printf ("Child2 Fork error\n"); Exit (1); } Else if(Child2 = =0)/*pause 5s in child process 2*/{printf ("In child2:sleep for 5 seconds and then exit\n"); Sleep (5); Exit (0); } printf ("In father Process:\n"); Child= Waitpid (Child1, NULL,0);/*Blocking Wait*/ if(Child = =child1) {printf ("Get child1 Exit code\n"); } Else{printf ("Error occured!\n"); } Do{ Child= Waitpid (Child2, NULL, Wnohang);/*non-blocking wait*/ if(Child = =0) {printf ("The child2 process has not exited!\n"); Sleep (1); } } while(Child = =0); if(Child = =child2) {printf ("Get child2 Exit code\n"); } Else{printf ("Error occured!\n"); } } return 0;}
The second type of code:
/*multi_proc_wrong.c*/#include<stdio.h>#include<stdlib.h>#include<sys/types.h>#include<unistd.h>#include<sys/wait.h>intMainvoid) {pid_t child1, child2, child; /*Create two sub-processes*/Child1=Fork (); Child2=Fork (); /*error handling for child process 1*/ if(Child1 = =-1) {printf ("Child1 Fork error\n"); Exit (1); } Else if(Child1 = =0)/*Call the EXECLP () function in child process 1*/{printf ("In child1:execute ' ls-l ' \ n"); if(EXECLP ("ls","ls","- L", NULL) <0) {printf ("Child1 EXECLP error\n"); } } if(Child2 = =-1)/*error handling for child process 2*/{printf ("Child2 Fork error\n"); Exit (1); } Else if(Child2 = =0)/*pause 5s in child process 2*/{printf ("In child2:sleep for 5 seconds and then exit\n"); Sleep (5); Exit (0); } Else /*waits for two child processes to exit in a parent process*/{printf ("In father Process:\n"); Child= Waitpid (Child1, NULL,0);/*Blocking Wait*/ if(Child = =child1) {printf ("Get child1 Exit code\n"); } Else{printf ("Error occured!\n"); } Do{ Child= Waitpid (Child2, NULL, Wnohang);/*non-blocking wait*/ if(Child = =0) {printf ("The child2 process has not exited!\n"); Sleep (1); } } while(Child = =0); if(Child = =child2) {printf ("Get child2 Exit code\n"); } Else{printf ("Error occured!\n"); } } return 0;}
Writing multi-process programming