Interprocess relationship

Inter-process relationship

There needs to be a way to know which process group is the foreground process, so that the pseudo-terminal program knows where to send terminal input and signals generated by the terminal.

#include <unistd.h>
pid_t tcgetpgrp(int fd);
int tcsetpgrp(int fd, pid_t pgrpid);

The function tcgetpgrp returns the id of the foreground process group, which is associated with the terminal opened on fd.

Given the file descriptor that controls the tty, the application can obtain the process group id of the first process through the tcgetsid function.

#include <termios.h>

pid_t tcpgetsid(int fd);

The application k that needs to manage the control terminal maliciously calls the tcgetsid function to identify the session id of the first process of the control terminal.

demo:

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <termios.h>
#include <fcntl.h>

static void judge(void){
    pid_t pid;
    pid = tcgetpgrp(STDIN_FILENO);
    if(pid == -1){
        perror("tcgetpgrp");
        return;
    }else if(pid == getpgrp()){
        printf("foreground\n");
    }else{
        printf("background\n");
    }
}

int main(void){
    printf("tcgetsid:%d,pgrp=%d,sid=%d\n",tcgetsid(STDIN_FILENO),getpgrp(),getsid(getpid()));
    signal(SIGTTOU,SIG_IGN);
    judge();
    int result;
    result = tcsetpgrp(STDIN_FILENO,getpgrp());
    if(result == -1){
        perror("tcsetpgrp");
        return -1;
    }
    judge();
    return 0;
}

9.8 Job control

Operational control is a feature added in 1980. He allows multiple jobs (process groups) to be started on one terminal. It controls which jobs can access the terminal and which jobs are running in the background. Job control has the following three forms of support.

1) Shell supporting job control

2) The terminal driver in the kernel must support job control.

3) The kernel must provide signal support for certain job control

A job is a collection of several processes, usually a process pipeline. E.g

vi main.c

Switch between foreground process and background process

When running a command in a Linux terminal, add an ampersand at the end of the command to let the program run in the background

[root@Ubuntu$] java Main &

If the program is running in the foreground, you can use the Ctrl+z option to pause the program, and then use jobs -l to view the number (job number) of the suspended program, and then use the bg %[number] command to make the suspended program continue to run in the background , Jobs command to view the tasks running in the background of the current terminal, and also to see the running status of the task.

ps and jobs. The difference is that jobs can only view the tasks executed in the background of the current terminal, and you can't see them if you change the terminal. The ps command is suitable for viewing the dynamics of the instantaneous process, and you can see the tasks of other terminals.

For all running programs, we can use the jobs -l command to view

[root@Ubuntu$] jobs -l

You can also use the fg %[number] command to transfer a program to the foreground, that is, transfer the process running in the background to the foreground

Two, fg, bg, jobs, &, nohup, ctrl+z, ctrl+c commands

1. & is added to the end of a command, you can put this command in the background for execution, such as watch -n 10 sh test.sh & #Execute the test.sh script in the background every 10s

2. ctrl + z can put a command being executed in the foreground to the background, and it is in a paused state. 3. Jobs Check how many commands are currently running in the background. The jobs -l option can display the PIDs of all tasks. The status of jobs can be running, stopped, or Terminated. But if the task is terminated (kill), the shell deletes the task's process ID from the list known by the current shell environment. 4. fg transfers the commands in the background to the foreground to continue running. If there are multiple commands in the background, you can use fg %jobnumber (the command number, not the process number) to call out the selected command. 5. bg changes a command that is paused in the background to continue execution in the background. If there are multiple commands in the background, you can use bg %jobnumber to call out the selected command. 6. Kill method 1: Use the jobs command to view the job number (assuming num), and then execute kill %num Method 2: Use the ps command to view the job process number (PID, assuming pid), and then execute kill pid to terminate the foreground process : Ctrl+c 7, nohup If the program is always executed in the background, even if the current terminal is closed (the previous & cannot be done, we use ssh to connect to the server, if we disconnect, the background task running on the current terminal It will also be closed, because all the commands we run on the current terminal, the parent process is the current terminal, and once the parent process exits, it will send the hangup signal to all child processes, and the child process will also receive hangup Will exit. If we want to continue running the process when exiting the shell, we need to use nohup to ignore the hangup signal, or setsid will set the parent process to the init process (process number is 1)). At this time, nohup is required. After using nohup, If we exit the terminal, the parent process of the program we are running will become 1. This command can continue to run the corresponding process after you log out of the account/close the terminal. After closing the interrupt, jobs in the other terminal can no longer see the programs running in the background, because jobs can only see the programs related to the current terminal process. At this time, you can use ps to view the specified process.

But it should be noted that nohup will close the standard input stream of the program we are running, that is, we can't just use the standard input stream. The output stream will be reset to the nohup.out file by default. Of course, you can also customize the output file.

9.9 Shell execution program

Let's check how the shell runs, and how this relates to the process group, process terminal, and session concepts, and execute the ps command again.

ps -o pid,ppid,pgid,sid,common

zhanglei@zhanglei-virtual-machine:~$ ps -o pid,ppid,pgid,sid,comm
    PID    PPID    PGID     SID COMMAND
   4463    4453    4463    4463 bash
   4504    4463    4504    4463 ps

The parent process of ps is the shell, which is exactly what we expect to see. The process group that does not support job control is the foreground process group.

The last process in the pipeline is a child process of the shell, and the first process in the pipeline is a child process of the last process. It can be seen that the shell forks a copy of itself, and then forks a process for each command in the copy.

If you execute this pipeline in the background

When there is job control, the background job is placed in the background process group. If the background job tries to read the control terminal, SIGTTIN will be generated. When there is no job control, the processing method is: if the process does not redirect input and output, then Redirect standard input and output to /dev/null. Reading /dev/null produces an end of file. This means that the background process cat reads to the end and ends normally.

9.10 Orphan Process Group

The orphan process is generally adopted by init, and generally adopted by init when the parent process has died.

The parent process sleeps for 5 seconds

The child process establishes a signal processor (SIGHUP) for the hang-up signal, so that it can be observed whether SIGHUP has been sent to the child process

The child process uses kill to send SIGTSTP to itself to stop itself

After the parent process terminates, the child process becomes an orphan process, and the parent process becomes an init process.

Now the child process becomes a member of the orphan process group. The orphan process group is defined as: the parent process of each member of the group is either a member of the reorganization, or is not a member of the session to which the group belongs. Another description of the orphan process is

After the parent process is stopped, the process group contains a stopped process. The process is composed of an orphan process group. Posix requires sending a sighup signal to the orphan process, and then sending a sigcont signal

Guess you like

Origin blog.csdn.net/qq_32783703/article/details/107294931