Chapter 7th Process Relationship (2) _ Process chain and process fan

Source: Internet
Author: User

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

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.