Linux core-5. Linux process

Source: Internet
Author: User
Article title: Linux core-5. Linux process. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Original: David A Rusling
Translation: Banyan & fifa
  
Chapter 4 process management
  
This chapter focuses on how to create, manage, and delete processes in the Linux kernel.
  
A process executes a specific task in the operating system. Programs are static entities stored on disks that contain executable machine commands and data. A process or task is an active computer program.
A process is an entity that constantly changes with the execution process. Like programs that contain commands and data, processes also contain program counters and the values of all CPU registers, at the same time, its stack stores temporary data such as subroutine parameters, return addresses, and variables. The current execution program, or process, contains the activity status in the current processor. Linux is a multi-processing operating system. Processes have independent permissions and responsibilities. If a process crashes in the system, it does not affect other processes. Each process runs in its own virtual address space and can communicate with each other only through a reliable communication mechanism under core control.
  
The process will use system resources during its life cycle. It uses the CPU in the system to execute commands and place commands and data in the physical memory. Use the functions provided by the file system to open and use files, and directly or indirectly use physical devices. Linux must track every process and resource in the system to achieve fair resource allocation between processes. If a process occupies most of the physical memory or CPU usage, this is unfair to other processes in the system.
  
The most valuable resource in the system is the CPU, which usually has only one CPU. Linux is a multi-processing operating system. its ultimate goal is to execute tasks on every CPU in the system at any time, thus improving the CPU utilization. If the number of processes exceeds the number of CPUs, some processes must wait until the CPU is idle. The idea of multi-processing is simple. when a process requires a system resource, it will stop running and wait until the resource is available. In a single processing system, such as DOS, the CPU will be in the air and the time will be wasted. In a multi-processing system, because multiple processes can exist at the same time, when a process starts to wait, the operating system will take control of the CPU and hand it over to other processes that can run. The scheduler is responsible for selecting appropriate processes to run. in Linux, some scheduling policies are used to ensure the fairness of CPU allocation.
  
Linux supports multiple types of executable file formats, such as ELF and JAVA. Since these processes must use the system shared libraries, they must be transparent in management.
4.1 Linux process
To allow Linux to manage processes in the system, each process is represented by a task_struct data structure (tasks and processes can be mixed in Linux ). The array task contains pointers to all task_struct structures in the system.
  
This means that the maximum number of processes in the system is limited by the size of the task array. the default value is generally 512. When a new process is created, Linux allocates a task_struct structure from the system memory and adds it to the task array. The structure of the currently running process is indicated by the current pointer.
  
Linux also supports real-time processes. These processes must quickly respond to external time (which means "real-time") and the system will treat these processes and other processes separately. Although task_struct has a large and complex data structure, it can be divided into some functional components: State
The state of a process is changed according to the environment during execution. The Linux process has the following statuses:
Running
The process is running (it is the current process of the system) or ready to run (it is waiting for the system to allocate CPU to it ).
Waiting
A process is waiting for an event or resource. In Linux, the waiting process is divided into two types: resumable and non-disruptive. A process that can be interrupted by signals. a process that can be interrupted can be waited directly under hardware conditions and cannot be interrupted under any circumstances.
Stopped
The process is stopped, usually by receiving a signal. The process being debugged may be stopped.
Zombie
This is a process terminated for some reason, but the task_struct structure is retained in the task data. It is like a dead process.
Scheduling Information
The scheduler needs this information to determine which process in the system is most urgent.
Identifiers
Each process in the system has a process flag. The process flag is not an index of the task array. it is just a number. Each process also has a user and group sign that controls the process's access to files and devices in the system.
Inter-Process Communication
Linux supports Classic Unix IPC mechanisms, such as signals, pipelines and traffic signals, and IPC mechanisms in System V, including shared memory, traffic signals, and message queues. We will discuss in detail the IPC mechanism in Linux in the IPC chapter.
  
Links
All processes in Linux are interrelated. All processes except the initialization process have a parent process. The new process is not created, but copied, or cloned from the previous process. The task_struct structure corresponding to each process contains pointers to its parent process, sibling process (with the same parent process), and child process. We can use the pstree command to observe the relationship between running processes in Linux:
  
Init (1)-+-crond (98)
|-Emacs (387)
|-Gpm (146)
|-Inetd (110)
|-Kerneld (18)
|-Kflushd (2)
|-Klogd (87)
|-Kswapd (3)
|-Login (160) --- bash (192) --- emacs (225)
|-Lpd (121)
|-Mingetty (161)
|-Mingetty (162)
|-Mingetty (163)
|-Mingetty (164)
|-Login (403) --- bash (404) --- pstree (594)
|-Sendmail (1, 134)
|-Syslogd (78)
'-Update (166)
In addition, all processes in the system are connected with a two-way linked list, and their root is the task_struct data structure of the init process. This linked list is used by the Linux core to find all processes in the system. it supports ps or kill commands.
Times and Timers
The core needs to record the process creation time and the CPU time consumed during its lifecycle. Every time the clock beats, the core needs to update and save it in the jiffies variable to record the time consumed by the process in system and user mode. Linux supports interval timers related to processes. a process can be called by the system to set a timer so that a signal can be sent to it after the timer arrives. These timers can be one-time or periodic.
File system
The process can open or close the file freely. the task_struct structure of the process contains a pointer pointing to each open file descriptor and a pointer pointing to two VFS inode. Each VFS inode uniquely identifies a directory or file in the file and provides a unified interface for the underlying file system. Linux's support for the file system will be detailed in the filesystem chapter. These two pointers point to the root directory of the process and the other to its current or pwd directory. Pwd is derived from the Unix command pwd to display the current working directory. The two VFS inode contain a count field. when multiple processes reference them, the value increases. This is why you cannot delete the current directory of the process or its subdirectories.
Virtual memory
Most processes have some virtual memory (not the core thread and background process). The Linux core must track the ing between the virtual memory and the physical memory of the system.
Processor Specific Context
Processes can be considered as the sum of the current states of the system. When a process is running, it uses the registers and stacks of the processor. When a process is suspended, the context of the process-all CPU-related statuses must be stored in its task_struct structure. When the scheduler re-schedules the process, all contexts are reset.
  
4.2 Identifiers
Like other Unix systems, Linux uses user and group identifiers to check access to files and executable images in the system. All files in Linux have the owner and permitted permissions. these permissions describe the user's right to use files or directories. The basic permissions are read, write, and executable. these permissions are assigned to three types of users: the owner of the file, the processes belonging to the same group, and all processes in the system. Each type of user has different permissions. for example, a file allows its owner to read and write data, but the same group can only read data and other processes cannot access the file.
  
Linux uses a group to grant access privileges to files and directories to a group of users, rather than a single user or all processes in the system. For example, you can create a group for all users in a software project and set its permissions to allow only users to read and write the source code in the project. A process can belong to multiple groups at the same time (up to 32). These groups are all placed in the group array in the task_struct of the process. As long as a group of processes can access a file, the process derived from this group has access permissions to the file.
The task_struct structure has four pairs of processes and group identifiers:
Uid, gid
Indicates the user identifier and group identifier of the running process.
Valid uid and gid
Some programs can change the uid and gid of the execution process to the uid and gid of the program itself during execution (stored in the VFS inode attribute of the descriptive executable image ). These programs are called setuid programs and are often used to strictly control access to certain services, especially those running for other processes, such as network background processes. Valid uid and gid are the uid and gid generated when the execution of the setuid is executed. When a process attempts to access privileged data or code, the core checks the valid gid and uid of the process.
File system uid and gid
They are similar to valid uid and gid, but are used to verify the file system access permissions of processes. For example, when an NFS server running in user mode accesses a file, the NFS file system uses these flags. In this example, only the uid and gid of the file system are changed (instead of the valid uid and gid ). This prevents malicious users from sending KILL signals to the NFS server.
Saved uid and gid
POSIX standards require the implementation of these two flags, which are used by programs that change the uid and gid of processes through system calls. When the original uid and gid of the process change, they are used to save the real uid and gid.
  
4.3 Scheduling
All processes run in user mode in part of time and in system mode in part of time. The underlying hardware implementation varies depending on how these modes are supported, but there is a security mechanism that enables them to switch back and forth between the user mode and the system mode. The permissions in user mode are much smaller than those in system mode. The process switches to the system mode to continue execution. At this time, the core is the 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.