A few special processes under Linux

Source: Internet
Author: User
Tags session id

1, the Linux login environment

Linux is a multi-tasking, multi-user operating system designed to achieve a multi-user, simultaneous use of a single computer large task.

Multi-User: Early computer resources are tight, so that more people can use it.

Multitasking: Serving multiple users while increasing the throughput of your computer.

Early login is logged in with a dumb terminal connection.

Early login to the Linux model

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/87/4A/wKiom1fal-KxhjUrAAA6NN68NN4699.png-wh_500x0-wm_3 -wmp_4-s_2512333930.png "title=" Qq20160915204503.png "alt=" Wkiom1fal-kxhjuraaa6nn68nn4699.png-wh_50 "/>

With the development of Ethernet, the advent of Linux, in the multi-user connection, the full use of Ethernet. Improve the application layer of network services, these network services replace the previous polling to monitor the serial port service process. SSH is encrypted relative to telnet and is more secure.


2. Process Group

Under Linux processes in addition to organizational relationships such as parent-child relationships, there is a grouping of organization relationships, and any process needs to belong to a process group.

Each process group has a separate process group number, which can be obtained through the Getpgid () method .

Each process group owns and has only one leader process. The team leader can be used to manage the uniform behavior of other processes within its group. (Example: The leader process if a special signal is obtained, the signal can be passed to all processes within the group).

The Process group ID is the process ID of the process group leader.

The members of the process group are the children and descendants of the leader.


3. Session

Each process needs to belong to a session in addition to belonging to a process group. The concept of a session is mainly obtained after logging on to the computer from the terminal.

When a terminal log on to the computer, in order to facilitate the separation of the different terminals, while the full reasonable management of all the processes generated under a terminal, therefore, the concept of a session is proposed. In other words, a session is a collection of user logons from the logon service process to the shell process.

Logically speaking, a session controls a continuous process of interaction between a computer and a terminal.

A session is usually made up of multiple process groups, divided into two parts (foreground process Group, background process Group).

A session has a session first process. The operating system manages all the process groups throughout the session through the first process of the session.

(1), foreground process Group

Is tied to the Terminal Services process, bash process, and is directly related to the terminal. Then, any operation of the terminal will affect all the foreground process groups.

Executing a command in the shell interaction environment will result in a new process to execute the command, not only that it will also produce a new process group, the team leader is the new process formed by the execution of the command.

foreground Process group The biggest problem is that you need to look at the face of the terminal! The terminal only needs to stop the group leader, and all processes inside it will stop.

(2), background process Group

Disconnect the relationship with the terminal (not the input, output, error output relationship), the process group relationship, is not subject to the terminal, this process and process group is called backstage.

The reason behind the background process is that it requires resident memory to provide a service.


4. How the process group is created

There are 2 ways to create a new process group, 2 of which are different:

(1), by calling Setpgid () to set the current process that invokes the method as the leader of the new process group. To create a new process.

(2), by calling the Setsid () method to create a new session, the new process group will appear, and the process calling the Setsid () method will become the first process of the session, and the team leader of the new group.


5. Zombie Process

In the Linux operating system design, when a process is required to end, it needs to inform its parent process of the end of the process, the parent process also needs to know the state of the end of its child process, because the parent process sometimes needs to be based on the state of the end of the child process to do some follow-up.

The demise of the process:

(1), the process calls the exit () method, notifies the operating system memory, the current process wants to end its own life;

(2), the operating system will immediately reclaim the process of almost all the main content, and then inform its parent process, one of its child processes end;

(3), the parent process needs to explicitly reply to the operating system kernel, and has received a message that the child process has ended. Otherwise, the operating system kernel will keep a portion of the PCB information that will end the process. At the same time, the status of the process is set to defunct. This is the zombie process.

i>, zombie processes can not be eliminated directly.

ii>, Zombie Process hazards: The use of PCB resources (PID resources).

iii>, child processes before the end of the parent process, the parent process does not have a corpse, is the zombie process.

The code that produces the zombie process is as follows:

#include <stdio.h> #include <stdlib.h> #include <unistd.h>int main (void) {     pid_t pid;    pid = fork ();     if (pid ==  0) {         printf ("This is child, will  finish. pid = %d\n ",  getpid ());     }else if (pid >  0) {         while (1) {             printf ("this is father, run allways. pid =  %d childpid = %d\n ",  getpid (),  pid);             sleep (1);        }        }else{        perror ("");     }       return 0;} 

Run results

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/87/47/wKioL1fasODiozrmAADhEQijI20629.png-wh_500x0-wm_3 -wmp_4-s_4001937369.png "title=" Qq20160915223142.png "alt=" Wkiol1fasodiozrmaadheqiji20629.png-wh_50 "/>


650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M02/87/4A/wKiom1fasWKjVhJ4AACXkY3rYDQ142.png-wh_500x0-wm_3 -wmp_4-s_3962390040.png "title=" Qq20160915223359.png "alt=" Wkiom1faswkjvhj4aacxky3rydq142.png-wh_50 "/>

How do I solve a zombie process problem? Avoid the production of zombie processes.

The root of the zombie process is that the parent process needs to handle the end of the child process. Resolves by calling the wait () function family. Wait waits for the end of the child process, and the process that calls wait is blocked until the end message of the child process is accepted. The parent process waits for the end message of the child process to be a SIGCHLD (17) signal.

Code to resolve the zombie process:

#include <stdio.h> #include <stdlib.h> #include <unistd.h>int main (void) {     pid_t pid;    pid = fork ();     if (pid ==  0) {         int i;         for (i = 0; i < 5; i++) {             printf ("this is child, will finish. pid = %d\ N ",  getpid ());             sleep (1);         }       }else if (pid  > 0) {         int status;         wait (&status);  //has handled the zombie process very well.         printf ("Child has&nbSp;gone, status = %d\n ",  status);         while (1) {              printf ("This is father ,  run allways. pid = %d childpid = %d\n ",  getpid (),  pid);             sleep (1);         }       }else{         perror ("");     }       return 0;}

This causes the parent process to start doing nothing, only waiting for the end of the child process to start running.

The solution to the zombie process is actually the process recycling process.


6. Orphan process

If a process's parent process ends before itself, does the process still have a parent process? There must be, because all processes under Linux must exist in the entire process tree and do not allow the existence of complete isolation.

If the parent process ends before the child process, the child process is taken to the process numbered 1, which is the INIT/SYSTEMD process.

At this point, however, the process group relationship for the process has been disrupted. The process group in which it resides is not its parent or ancestor process. Because, at this point, the child process still retains the previous process group information, it is clear that the session information is incorrect for the process group.

This process is the orphan process, losing the original process of all the process groups to participate in the conversation relationship. So the INIT/SYSTEMD process will also be called the orphanage process, because it has adopted many orphans.

because the parent process of the child process ends, the signal of its group leader cannot be passed to the child process. So that the sub-process is out of the original process group relationship, out of the original session relationship. Although it retains the original process group and session ID, it has no effect.

The code that generated the orphan process:

#include <stdio.h> #include <unistd.h>int main (void) {    pid_t pid;     pid = fork ();     if (pid == 0) {              while (1) {             printf ("this is along child, pid = %d,  pgid = %d sid = %d\n ",  getpid (),  getpgid (Getpid ()),  getsid ( Getpid ());             sleep (1);         }       }else if (pid > 0) {      printf ("this is father, pid = %d, pgid =  %d sid = %d\n ",  getpid (),  getpgid (Getpid ()),  getsid (Getpid ()));     }else{  &nbSp;     perror ("");    }        return 0;}

At this time the orphan process is generated, will use CTRL + C to terminate, can only use kill PID to kill the orphan process.


7. Guardian Process

Guard a service that resides in memory for a long time and is not subject to a terminal. Usually refers to the service process in the operating system. These service processes typically contract the last letter of their name as D.

Another name for the daemon: Sprite process (demon).

How do I make a process a daemon? Leave a process out of the foreground process group relationship (so you can get rid of the terminal's control of it).

How do I get the process out of the foreground process group? 1), create a new session, 2), constitute the orphan process.

These two steps are usually made.

Process of daemon creation:

pid_t pid;pid = fork (), if (PID = = 0) {setsid ();        A session process is generated while (1) {//child processes are running, sooner or later constituting an orphan process. ...    }}  else if (PID > 0) {... exit (0); Parent process End}else{perror ();}


8. A link between the zombie process and the orphan process

Init/systemd, also known as the God process , has created all the processes inside the operating system and managed these zombie processes to help clear them off.

If a process produces a zombie subprocess, when the process is finished, the zombie process is passed on to the INIT/SYSTEMD process, and then the INIT/SYSTEMD process discovers that the adoptive new child process is a zombie process and will be corpse- Thus eliminating the existing zombie process.



This article is from the "11586096" blog, please be sure to keep this source http://11596096.blog.51cto.com/11586096/1853027

A few special processes under Linux

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.