Chapter 7th Process Relationship (4) _ Foreground process Group

Source: Internet
Author: User

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

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.