1, related concepts.
1. Programs, processes, threadsThe program does not run alone, and the program is assigned resources to run only if it is loaded into memory, and this execution is called a process. The difference between a program and a process is that a program is a set of instructions, which is a static descriptive text of the process, and a process is an execution activity of a program, which belongs to the dynamic concept. Allows multiple programs to be loaded into memory at the same time, which can be implemented concurrently under the dispatch of the operating system. This is the design, the advent of the process so that each user feel that their own CPU. A thread is an entity of a process that is the basic unit of CPU dispatch and dispatch, and it is the basic unit that can run independently of the process. The thread itself basically does not own system resources and only has a bit of resources that are essential in the run ( such as program counters, a set of registers and stacks), but it can share all of the resources owned by the process with other threads belonging to one process. (This is a general understanding, and then in the kernel, threads and processes are more ambiguous, sometimes even as synonyms, often referred to as threads for process and architecture-related parts, which should be noted)
2. Differences in processes and threadsThe main difference between processes and threads is that they are different ways to manage operating system resources. The process has a separate address space, and after a process crashes, it does not affect other processes in protected mode, and the thread is just a different execution path in a process. Thread has its own stack and local variables, but there is no separate address space between the threads, a process dead equals the entire thread dead, so the multi-process program is more robust than the multithreaded program, but in the process of switching, the cost of large resources, efficiency is worse. But for some concurrent operations that require simultaneous and shared variables, only threads can be used, and processes cannot be used.
3. The multi-tasking truthIn a multiprocessor system, The number of processes that really run in parallel does not exceed the number of physical CPUs. The kernel and the processor set up a multi-tasking illusion, through a very short interval of time in the system running between the various programs to achieve the purpose, because the switching time is very short, the user can not notice a short period of stagnation, from the senses that the computer can handle multitasking, Out of your eyes.
4. Process PriorityHard real-time processes-the processing of tasks has strict time limits, must be executed within a given time, and Linux versions do not support hard real-time processing. Soft real-time process-the weakening form of hard real-time processes. 5. Preemptive multitasking (preemptive multitasking) each process is assigned to a certain period of time can be executed, after the time period expires, the kernel from the process to take back control, let another process run, regardless of the previous process performed by the task, the preempted Process execution environment will be saved , the environment can be fully recovered when execution is resumed. The length of the time slice varies depending on the importance of the process, which is done by the scheduler policy.
6. Process life cycle。 Run: process is running. Wait: The process can run without permission, and the scheduler can select the process at the next task switch. Sleep: The process cannot run in sleep because the scheduler cannot select the process at the next task switch because it waits for an external event to wake up.
7. Preemption Multi-tasking。 Two process state options: User state-kernel mindset, establish closed enclosures to prevent processes from interfering with other components of the system;. Processes usually handle the user state, only access their own data, and can not interfere with other processes in the system, the process does not feel the existence of other programs outside of itself, as if only one of its own program to enjoy the same system. The process can switch to the kernel mentality to access the system resources by system call or interrupt mode. But the interrupt switches to the kernel mentality when the user virtual memory space is inaccessible. Normal processes can be preempted by other processes. If the system processes the kernel mentality and is processing system calls, other processes cannot preempt the seizure of their CPUs, except for interrupts, which have the highest priority, can preempt the user state or the kernel mentality process CPU (particularly important kernel operations can also stop interrupts).
8. Presentation of the process。 All the algorithms of the process are built around the task_struct data structure, which contains many members that link the process to each of the kernel subsystems. /include/linux/sched.h:1295. Task_struct Resource restriction Member: rlim_cur: Process Current resource limit-soft limit; Rlim_max: Process allows maximum resource limit-hard limit;. This diagram is architecture-dependent.
Namespaces (namespace)。 namespaces provide a lightweight implementation of virtualization implementations, providing a resource isolation scheme where system resources such as pid,ipc,network are no longer global, but belong to a specific namespace. Resources under each specific namespace are transparent and invisible to other namespace resources. So at the OS level there will be a number of the same PID process, due to different namespce, these same PID process will not conflict, and at the user level can only see the resources belonging to their own namespace, using the PS command can only display their own namespace under the process, So each namespace looks like a separate OS, equivalent to a kernel running multiple OSes, which is the basic implementation of virtualization. namespace provides a container for placing a set of processes, each of which is isolated from each other, so that there is no relationship between the members in each container, but also the containers are shared. Essentially, the namespace establishes a different view of the system level. Namespace can also be organized into hierarchical relationships . 。 The creation of namespace can be created by two system calls: 1. Fork or clone when you create a new process, there are specific options to control. 2. Unshare system calls can detach a process from the current namespace. 3. The connection between the processes and the namespaces. 。 Important data structure the UTS (Unix timesharing System) namespace contains information about the name, version, architecture, and so on of the running kernel; the IPC namespace holds all relevant information about the IPC communication; The MNT namespace is associated with the file system view; PID The namespace contains information about the PID, and the NET namespace contains all network-related information: Each process is associated with its own namespace:. User namespace creation is implemented using fork or clone, which is eventually implemented using the Do_fork system call, with the following code flow: Process Diagram:
The process ID.。 Related concepts: PID: This is a number assigned to it that uniquely identifies a process in its namespace in Linux, called the process ID number, or PID. Processes generated when using a fork or clone system call are assigned a new, unique PID value by the kernel. Tgid: In a process, if a process called clone with the Clone_thread flag is a thread of the process, they are in a thread group and the ID of the thread group is called Tgid. All processes in the same thread group have the same tgid, and the thread group leader's Tgid is the same as the PID, and the Tgid is the same as the PID if a process does not use a thread. Pgid: In addition, independent processes can compose process groups (using SETPGRP system calls), and process groups can simplify the operation of signaling to processes within all groups, such as processes connected by pipelines in the same process group. The process group ID is called Pgid, and all processes within the process group have the same pgid, which equals the PID of the group leader. SID: Several process groups can be combined into a single conversation group (using the SETSID system call), which can be used for terminal programming. All processes in a conversation group have the same SID. Global ID: The unique ID in the kernel itself and in the initial namespace, the Init process that started during system startup belongs to that initial namespace. Each process in the system corresponds to a PID of that namespace, called the global ID, and is guaranteed to be unique throughout the system. Local ID: For a particular namespace, the ID assigned to it within its namespace is a local ID, which can also appear in other namespaces. Data structure: The type of ID Pidtype_max represents the number of ID types. The thread group ID is not included because the kernel already has a task_struct pointer to the thread group Group_leader, and the thread group ID is group_leader The pid.tasks is an array in which each array item is a hash header corresponding to an ID type, and all task_struct instances shared to the same given PID are linked by the list. NR represents the value of the PID. If there are now three processes A, B, C for the same process group, the process leader is a, data structure diagram: Detailed: http://www.cnblogs.com/hazir/p/linux_kernel_pid.html
process relationships.。 If process a branches form process B, process A is called the parent process of process B, and process B is a child process;. If process B branches again to form process C, then process A and process C are directly referred to as grandchild relationships;. If process a branches n times to form n processes b1,b2: Bn, then the direct relationship of each process is called the sibling process.
Kernel threads.The number of kernel threads is directly initiated by the kernel itself (as described at the beginning, in the Linux kernel, the process and threading concepts are ambiguous, usually synonyms) ....
Professional Linux Kernel Architecture-Learn notes. Process management and scheduling