4. foreground Process Group
(1) Foreground process Group
① groups that automatically accept terminal signals are called foreground process groups
② the signal generated by the terminal through Ctrl + C is first accepted by the foreground process group .
③ the number of process groups started in the shell by default is that the parent process is in the same group as the foreground process group, and the other process group is the background process group
④ is scheduled to become a foreground process group unless it is the default
(2) Get/Set foreground process group ID
Header file |
#include <unistd.h> |
Function |
pid_t tcgetpgrp (void); // get foreground process group ID, error return-1 int tcsetpgrp (int fd, pid_t pgrpid); // Set foreground process group ID, return 0 successfully, error 1 |
Function |
Gets or sets the foreground process group ID |
Parameters |
FD: Must refer to the control Terminal of the session,0 represents the terminal currently in use |
"Programming experiments" set up foreground process groups
Process_foregroup.c
#include <unistd.h>#include<stdio.h>#include<stdlib.h>//test foreground Process Group//divided into the foreground process group and the background process group, press CTRL + C when the foreground process group will be killed. intMainvoid) {pid_t group1, group2; //Create Process group 1, parent process as leader ProcessSetpgid (Getpid (), Getpid ()); Group1=Getpgid (Getpid ()); pid_t pid; inti =0; for(; i<3; i++) {PID=Fork (); if(PID <0) {perror ("Fork Error"); Exit (1); }Else if(PID >0){//Parent Process if(i = =0 ){ //Create a Process group 2Setpgid (PID, PID); Group2=Getpgid (PID); //set Group2 as the foreground process groupTCSETPGRP (0, group2); } if(i = =1){ //Add a 2nd child process to group2Setpgid (PID, group2); } if(i = =2){ //3rd child process join Group1Setpgid (PID, group1); } }Else{//Child Process if(i = =0){ //Create a Process group 2Setpgid (Getpid (), Getpid ()); Group2=Getpgid (Getpid ()); //set Group2 as the foreground process groupTCSETPGRP (0, group2); } if(i = =1){ //Add a 2nd child process to group2Setpgid (Getpid (), group2); } if(i = =2){ //3rd child process join Group1Setpgid (Getpid (), group1); } Break;//process fan, child process must exit}} printf ("pid:%d, Ppid:%d, Pgid:%d\n", Getpid (), Getppid (), Getpgid (0)); Pause (); return 0;}
Chapter 7th Process Relationship (4) _ Foreground process Group