linux--Terminal, Operation control, and daemon process

Source: Internet
Author: User
Tags session id terminates

I. Concept of process group, job, session

1. Process group: is a collection of one or more processes. Typically, associated with the same job, you can receive various signals from the same terminal. Each process has a unique process group ID. Each process group can have a leader process. The identification of the leader process is that its process group ID equals its process ID. The leader process can create a process group, create a process in that group, and then terminate. As long as a process exists in a process group, the process group exists, regardless of whether the leader process terminates.

2, homework: Shell sub-platform to control is not a process but the job (job) or process group. A foreground job can consist of multiple processes, one background can be composed of multiple processes , and the shell can run a foreground job and any number of background jobs, which is called job control.

the difference between a job and a process group: If a process in the job has created a child process, the child process does not belong to the job .

Once the job runs, the shell mentions itself to the foreground, and if the original foreground process still exists (if the child process has not yet terminated), it automatically becomes the background process group.

3. Session: is a collection of one or more process groups. A session can have a control terminal. The first process of establishing a session connected to a control terminal is called the control process. several process groups in a session can be divided into a foreground process group and one or more background process groups. Therefore, a session should include the control process (session first process), a foreground process group, and any background process group.

(The process group is equivalent to a class, the leader process is like being a monitor.) A session is equivalent to a grade. One grade has an academic office. (Control terminal)).
Second, the terminal

1, the concept of the terminal: the user through the terminal login system to get a shell process, this terminal is called Shell Process Control terminal,

Each process can access its control terminal through a special device file, /dev/tty . In fact, each terminal

Corresponds to a different device file,/dev/tty provides a common interface, a process to access its control terminal can be accessed either through the /dev/tty or through the device file corresponding to the terminal equipment. The ttyname function can be used to identify the corresponding file name by a file descriptor, which must point to an end device and not to any file.

Check the device filenames for each of the different terminals.

1 #include <stdio.h> 2 #include <unistd.h> 3 int main () 4 {5 printf ("FD:%d-%s\n", 0,ttyname (0));  6 printf ("FD:%d-%s\n", 1,ttyname (1));  7 printf ("FD:%d-%s\n", 2,ttyname (2)); 8}

2. Terminal Login Process:

A PC usually has only a set of keyboards and monitors, that is, only a terminal device, but can be switched to 6-character terminal via CTRL-ALT-F1~CTRL-ALT-F6, equivalent to 6 sets of virtual terminal equipment, they share the same set of physical equipment, the corresponding device files are/dev /tty1~/dev/tty6, so called virtual Terminal. Device file/dev/tty0 represents the current virtual terminal, such as switch to the CTRL-ALT-F1 character Terminal/dev/tty0 is the/dev/tty1, switch to CTRL-ALT-F2 character terminal/dev/tty0 represents/dev/tty2, like /dev/tty is also a common interface, but it cannot represent the terminal that the Graphics terminal window corresponds to.

A, when the system starts, the INIT process determines which endpoints need to be opened based on the profile/etc/inittab.

B, Getty According to the command line parameters open Terminal Equipment as its control terminal, the file descriptor 0, 1, 2 point to control

Terminal, and then prompt the user to enter the account. After the user enters the account, Getty's task is completed, and it executes the login program:

Execle ("/bin/login", "Login", "-P", username, NULL, envp);

C, the login program prompts the user to enter a password (during the password to turn off the terminal echo), and then verify the correctness of the account password. If the password is incorrect and the login process terminates, Init will re-fork/exec a Getty process. If the password is correct, the login program sets some environment variables, sets the current working directory as the user's home directory, and executes the shell:

Execl ("/bin/bash", "-bash", NULL);

Third, the Guardian process

1, Daemon also known as the wizard process (Daemon), is running in the background of a special process. It is independent of the control terminal and periodically performs some sort of task or waits to handle certain occurrences.

2, with PS AXJ | Grep-e ' d$ ' view daemon

Parameter A indicates that not only the process of the current user is listed, but also all other users ' processes, the parameter x indicates that not only the process of the control terminal is listed, but also all the processes that have no control terminal, and the parameter J indicates that the information related to the job control is listed.

3. Creating daemons

Call the function Setsid function to create a new session, and become session Leader (session first process), the call successfully returned to the newly created session ID, error return-1;

A, call Umask to set the file mode creation screen Word to 0.

b, call fork, parent process exit (exit). Reason:

1) If the daemon is started as a simple shell command, the parent process terminates so that the shell considers that the command has been executed.

2) Ensure that the child process is not a process group leader process.

C, call Setsid to create a new session. Setsid can cause:

1) The calling process becomes the first process of a new session.

2) The calling process becomes the leader process of a process group.

3) The calling process does not control the terminal. (Fork once again to ensure that the daemon process does not open the TTY device afterwards)

D. Change the current working directory to the root directory.

E, close the file descriptor that is not required.

    f, other: ignores SIGCHLD signals.

 1  #include <stdio.h>  2  #include <stdlib.h>  3  #include < Unistd.h>  4 void mydeamon (void)   5 {  6      pid_t  id=fork ();   7     umask (0);//Set the file mode to create the mask word to 0.   8     if (id>0)    9     {  10         exit (0);//Call fork, parent process exit (exit)  11      } 12     setsid ();//Call Setsid to create a new session  13      chdir ("/");//Change the current working directory to the root directory.  14     close (0);//Close the file descriptor that is not required.  15     close (1);  16     close (2);  17 }  18 int main ()  19 { 20     mydeamon (); 21      while (1); &NBsp;22     return 0; 23 } 

Use PS axj |grep "file name" To view the daemon you just created

650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M01/80/11/wKiom1c1xFuQHoSVAADj3ZRrWDc376.jpg "title=" Deamon . JPG "alt=" Wkiom1c1xfuqhosvaadj3zrrwdc376.jpg "/>

linux--Terminal, Operation control, and daemon process

Related Article

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.