I. Terminal
The process records who their control terminal is, using the PS AJX command, where the TTY section represents what the terminal is currently using . Mark represents this process is no control terminal, there is control terminal will record control terminal number is what
The TTY represents the character Terminal , and PTS represents the graphical interface terminal, /dev/tty, pointing to the terminal currently in use .
Two. Process group
A process group is a collection of one or more processes, and the process group ID is a positive integer. function to get the current process group
// Gets the process group ID of the specified process ID, and if passed 0, represents the process group ID for getting the current processpid_t getpgrp (void)
The leader process is the process where the process group ID is equal to the process ID. The leader process can create a process group, and if the leader process terminates at this point, the process group number of the other processes in the process group is still the same, and the process group is truly extinct when the last process of the process group dies.
Examples of the use of 1:getpgid and GETPGRP:
Getpgrp
#include <stdio.h>#include<unistd.h>#include<stdlib.h>intMain () {pid_t pid; PID=Fork (); if(PID >0) {printf ("I am Parent, PID is:%d, pgid ISI:%d\n", Getpid (), GETPGRP ()); } Else if(PID = =0) {printf ("I am Child, PID was:%d, Pgid is:%d\n", Getpid (), GETPGRP ()); } Else{perror ("Fork"); Exit (1); } return 0;}
Getpgid
#include <unistd.h><stdio.h>int main () { printf ("current Progress group ID is:%d\n", Getpgid (0)); while (1); return 0 ;}
Operation Result:
Current Progress Group ID is:5665
A process can set the process group ID for itself or a child process
int Setpgid (pid_t pid, pid_t pgid) If you change a child process to a new group, it should be used after the fork, the non-root process can only change the child process that you created, or the process that has permission to manipulate
Example: Using Setpgid to set up a new process group
#include <stdio.h>#include<unistd.h>#include<stdlib.h>intMain () {pid_t pid; PID=Fork (); if(PID >0) {Sleep (5); Setpgid (PID, PID); while(1); } Else if(PID = =0) { while(1) {printf ("I am Child, PID was:%d, GID is:%d\n", Getpid (), GETPGRP ()); Sleep (2); } } Else{perror ("Fork"); Exit (1); } return 0;}
Operation Result:
I am Child, PID is:5758, GID is:5757
I am Child, PID is:5758, GID is:5757
I am Child, PID is:5758, GID is:5757
I am Child, PID is:5758, GID is:5758
I am Child, PID is:5758, GID is:5758
I am Child, PID is:5758, GID is:5758
Three. session
The SID column in PS AJX refers to a session ID, which is bound to the shell. A typical scenario for a session is:
When the user exits the shell, all processes initiated by the shell receive this signal (SIGHUP), and the default action is to terminate the process. Send signal number NO. 0 to a process to test if the process is alive.
We can use the following function to make a program a new session (the process will work when the control terminal is finished)
pid_t setsid (void)
Use this function to be aware of:
1. The calling process cannot be the leader process of the process group , and when the call is complete, the calling process becomes the new session header process and the lead process for the group.
2. Need to have root permission, Ubuntu does not need.
3. The new session discards the original control terminal, which does not have a control terminal.
4. If the calling process is the leader process, an error is returned.
5. When a new session is established, the fork is called first, the parent process terminates, and the child process is called.
Cases:
#include <stdio.h>#include<unistd.h>#include<stdlib.h>intMain () {pid_t pid; PID=Fork (); if(PID = =0) {printf ("I am Child process, PID was:%d, GID is:%d, sid is:%d\n", Getpid (), Getpgrp (), GetSID (0)); Setsid (); printf ("I am Child process, PID was:%d, GID is:%d, sid is:%d\n", Getpid (), Getpgrp (), GetSID (0)); while(1); } Else if(PID <0) {perror ("Fork"); Exit (1); } return 0;}
Operation Result:
I am child process, PID is:5960, GID is:5959, sid is:5795
I am child process, PID is:5960, GID is:5960, sid is:5960
where GETSID (0) is used to view the session ID of the current process
6. Inter-process relations