Writing multi-process programming

Source: Internet
Author: User
Tags exit in

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.