Home  >  Article  >  Backend Development  >  Linux--Terminal, job control, and daemons

Linux--Terminal, job control, and daemons

黄舟
黄舟Original
2017-01-18 10:24:341534browse

1. The concepts of process group, job, and session
1. Process group: It is a collection of one or more processes. Typically, associated with the same job, various signals from the same terminal can be received. Each process has a unique process group ID. Each process group can have a leader process. The identity of the group leader process is that its process group ID is equal to its process ID. The group leader process can create a process group, create the processes in the group, and then terminate. As long as there is a process in a process group, the process group exists, regardless of whether the group leader process terminates.

2. Job: Shell is divided into front and backends to control not the process but the job (job) or process group. A foreground job can be composed of multiple processes, and a background job can also be composed of multiple processes. 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 creates a child process, the child process does not belong to the job.
Once the job ends, the Shell will bring itself to the foreground. If the original foreground process still exists (if the child process has not been terminated), it will automatically become a background process group.
3. Session: It is a collection of one or more process groups. A session can have one controlling terminal. The session first process that establishes a connection with the controlling terminal is called the controlling 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 (the first process of the session), a foreground process group and any background process group.
**************** (The process group is equivalent to a class, and the group leader is equivalent to the squad leader. The conversation is equivalent to a grade. Each grade has an Academic Affairs Office .(control terminal)).
2. Terminal
1. The concept of terminal: After the user logs in to the system through the terminal, he gets a Shell process. This terminal is called the control terminal of the Shell process.
Each process can pass a special device file /dev/tty accesses its controlling terminal. In fact, each terminal device
corresponds to a different device file. /dev/tty provides a universal interface. A process can access its control terminal through /dev/tty or through the terminal device. Corresponding device file to access. The ttyname function can find out the corresponding file name from the file descriptor. The file descriptor must point to a terminal device and not any file.
******************Check the device file names corresponding to various 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 only has one set of keyboard and monitor, that is, only one set of terminal equipment, but it can be switched to 6 by Ctrl-Alt-F1~Ctrl-Alt-F6 Character terminals are equivalent to 6 sets of virtual terminal devices. They share the same set of physical terminal devices. The corresponding device files are /dev/tty1~/dev/tty6, so they are called virtual terminals. The device file /dev/tty0 represents the current virtual terminal. For example, when switching to the character terminal of Ctrl-Alt-F1, /dev/tty0 represents /dev/tty1. When switching to the character terminal of Ctrl-Alt-F2, /dev/tty0 represents Represents /dev/tty2, which is a universal interface just like /dev/tty, but it cannot represent the terminal corresponding to the graphical terminal window.
a. When the system starts, the init process determines which terminals need to be opened based on the configuration file /etc/inittab.
b. getty opens the terminal device as its control terminal according to the command line parameters, points the file descriptors 0, 1, and 2 to the control
terminal, and then prompts the user to enter an 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 (turn off the terminal's echo during password input), and then verifies the correctness of the account password. If the password is incorrect, the login process is terminated, and 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 to the user's home directory, and then executes Shell:
execl("/bin/bash", "-bash", NULL);
三, Daemon process
1. The daemon process, also called the daemon process, is a special process running in the background. It is independent of the control terminal and periodically performs some task or waits for some event to occur.
2. Use ps axj | grep -E 'd$' to view the daemon process
Parameter a indicates that not only the processes of the current user are listed, but also the processes of all other users, and parameter x indicates that not only the processes of the control terminal are listed Process also lists all processes without control terminals. Parameter j indicates listing information related to job control.
3. Create a daemon process

Call the setsid function to create a new Session and become the Session Leader (session first process). The newly created Session id will be returned if the call is successful, and -1 will be returned on error;

a. Call umask to set the file mode creation mask to 0.
b. Call fork and the parent process exits (exit). Reasons:
1) If the daemon is started as a simple shell command, then the parent process termination causes the shell to think that the command has been executed.
2) Ensure that the child process is not the leader process of a process group.
c. Call setsid to create a new session. setsid will cause:
1) The calling process becomes the first process of the new session.
2) The calling process becomes the leader process of a process group.
3) The calling process does not have a controlling terminal. (Fork again to ensure that the daemon process will not open the tty device afterwards)
d. Change the current working directory to the root directory.
e. Close file descriptors that are no longer needed.
f, Others: Ignore the SIGCHLD signal.

#include<stdio.h>  
  2 #include<stdlib.h>  
  3 #include<unistd.h>  
  4 void mydeamon(void)  
  5 {  
  6     pid_t  id=fork();  
  7     umask(0);//将文件模式创建屏蔽字设置为0.  
  8     if(id>0)   
  9     {  
 10         exit(0);//调用fork,父进程退出(exit)  
 11     }  
 12     setsid();//调用setsid创建一个新会话  
 13     chdir("/");//将当前工作目录更改为根目录。  
 14     close(0);//关闭不在需要的文件描述符。  
 15     close(1);  
 16     close(2);  
 17 }  
 18 int main()  
 19 {  
 20     mydeamon();  
 21     while(1);  
 22     return 0;  
 23 }

Use ps axj |grep "file name" to view the daemon process just created

Linux--Terminal, job control, and daemons

The above is Linux--terminal, job control, and daemon process For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn