2. process chain and process fan
(1) Create process Chain
① Process Chain : That is, the parent process creates a child process, and the child process that is created creates the child process that belongs to itself again, so that it loops down as shown in turn.
② key implementations: determines that if the parent process exits , the parent process will only create a child process. if it is a child process, continue to create the next process and then exit.
"Programming Experiment" build process Chain
Process_link.c
#include <unistd.h>#include<stdio.h>#include<stdlib.h>intMainintargcChar*argv[]) { intCounter = (ARGC <2) ?2: Atoi (argv[1]); pid_t pid; inti =0; //Build Process Chain for(i =1; i<counter; i++) {PID=Fork (); if(PID <0) {perror ("Fork Error"); Exit (1); }Else if(PID = =0) {//child process continues to create a process chain for further child process formation Continue; }Else{//parent process exits loop Break; }} printf ("pid:%d, ppid =%d\n", Getpid (), Getppid ()); Wait (0);}
(2) Create process fan
① Process fan: is a parent process that creates multiple sub-processes, as shown in.
② key implementations: Determines that the child process exits the cycle of creating the child process , leaving the opportunity for the creation process to the parent process only .
"Programming Experiment" build process fan
Process_swing.c
#include <unistd.h>#include<stdio.h>#include<stdlib.h>intMainintargcChar*argv[]) { intCounter = (ARGC <2) ?2: Atoi (argv[1]); pid_t pid; inti =0; //build Process Fan for(i =1; i<counter; i++) {PID=Fork (); if(PID <0) {perror ("Fork Error"); Exit (1); }Else if(PID = =0) {//the child process exits Break; }Else{//parent process continues to create child process Continue; }} printf ("pid:%d, ppid =%d\n", Getpid (), Getppid ()); for(i=0; i<counter; i++) Wait (0); }
Chapter 7th Process Relationship (2) _ Process chain and process fan