The process of programming Linux System (v) terminal, Operation control and daemon process

Source: Internet
Author: User

First, the concept of the terminal

In the UNIX system, the user obtains a shell process through the terminal login system, this terminal becomes the Shell Process Control terminal (controlling Terminal), the control terminal is the information which holds in the PCB, but we know fork will copy the information in the PCB, Therefore, the control terminal of other processes initiated by the shell process is also the terminal. By default (no redirection), standard input, standard output, and standard error output for each process point to the control terminal, and the process reads from the standard input to read the user's keyboard input, and the process writes to the standard output or the standard error output, which is output to the display. Entering some special control keys at the control terminal can signal the foreground process, for example, Ctrl-c indicates that sigint,ctrl-\ represents Sigquit.

Each process can access its control terminal through a special device file/dev/tty. In fact, each terminal unit corresponds to a different device file,/dev/tty provides a common interface, a process to access its control terminal can be accessed either through/dev/tty or the corresponding device file of the terminal equipment. The Ttyname function can detect the corresponding file name by a file descriptor that must point to a terminal device and not any file. Command TTY on Linux can also view the current terminal.

For example, we open a terminal in the graphical interface may be/dev/pts/0, the second may be/dev/pts/1 ... (Network terminal)

and switch to the character interface may be/dev/tty1 ... (Virtual terminal)

Second, the Operation control

In fact, the shell does not control the process but the job (job) or process group. A foreground job can consist of multiple processes, and a background job can consist of multiple processes, which can run both a foreground job and any number of background jobs, known as Job Control (Jobcontrol). For example, start 5 processes with the following command (this example is from Apue):

$ Proc1 | PROC2 &

$ PROC3 | proc4 | Proc5

where Proc1 and proc2 belong to the same background process group, PROC3, proc4, and proc5 belong to the same foreground process group, and the shell process itself belongs to a separate process group. The control terminals of these process groups are the same, they belong to the same session, and one of the sessions is related to a control terminal. When a user enters a special control key (such as CTRL-C) in the control terminal, the kernel sends the corresponding signal (for example, SIGINT) to all processes in the foreground process group. The relationships of each process, process group, and session are shown in the following illustration.

In the example above, PROC3, proc4, proc5 are placed by the shell in the same foreground process group, where one process is the Leader,shell call of the process group waiting for them to run. Once they are all running, the shell calls the TCSETPGRP function to refer itself to the foreground to continue accepting the command. Note, however, that if a process in Proc3, proc4, Proc5 is fork out of a subprocess, the child process also belongs to the same process group, but the shell does not know the existence of the subprocess and does not call wait for it to end. In other words, PROC3 | proc4 | PROC5 is the shell's job, and this subprocess is not, this is the conceptual difference between the job and the process group. Once the job is finished, the shell refers itself to the foreground, and if the original foreground process group still exists (if the child process is not terminated), it automatically becomes a background process and is taken over by the INIT process.

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.