Basic concepts of Linux processes - process status, process composition, process control

Table of contents

Linux process

Basic concepts of process

process status

Process composition

process control

Process creation and termination


Linux process

reference:

Basic concepts of process

Programs and Processes

In layman's terms, a program is a file that contains executable code and is a static file. A process is an instance of a program that has started execution but has not yet ended. It is the specific implementation of the executable file. A program may have many processes, and each process may have many child processes.

process status

  1. Creation state: The process is being created and has not yet been transferred to the ready state.

  2. Ready state: The process has obtained all required resources except the processor, and can run immediately once it obtains the processor.

    • Status characteristics: Processor (or scheduler) resources: only the processor is missing. Resource acquisition: The required resources have been obtained. When a processor is obtained: run immediately.

    • State transition: Ready state -> Running state: After the process in the ready state is scheduled, it obtains processor resources, so the process switches from the ready state to the running state.

  3. Running state: The process is running on the processor; for a single processor, only one process is in the running state at the same time.

    • State transition: Running state -> Ready state: Case 1: After the time slice is used up, the process in the running state has to give up the processor and then transition to the ready state. Case 2: In a deprivable operating system, when a higher-priority process is ready, the scheduler converts the executing process into a ready state to allow the higher-priority process to execute.

    • State transition: running state -> blocking state (active behavior): When a process requests the use of a certain resource (such as a peripheral) or waits for the occurrence of a certain event (such as the completion of an I/O operation), it changes from the running state Convert to blocking state. The process requests the operating system to provide services in the form of a system call, which is a special form in which a user-mode program calls the operating system kernel process.

  4. Blocking state: Also known as waiting state, the process is waiting for a certain event and suspends running/sleeping. For example, waiting for a certain resource or IO to complete, the process cannot run even if the processor is idle.

    • Status characteristics: Processor (or scheduler) resources: may be lacking; may not be lacking. Resource acquisition: waiting for a resource to be available or waiting for something to be completed. When getting a processor: Even if the processor is idle, it still cannot run when the thing it is waiting for has not been completed.

    • State transition: Blocked state -> Ready state (passive behavior, requiring assistance from other related processes): When the event that the process is waiting for arrives, such as the end of the I/O operation or the end of the interrupt, the interrupt handler must change the status of the corresponding process from blocked state transition to ready state.

  5. Termination state: The process is disappearing from the system. It may be that the process terminates normally or exits for other reasons.

Corresponding to the flags of each process status in the Linux kernel:

  • TASK_RUNNINGIt means that the process is ready. It depends on whether the operating system allocates a time slice to execute on the CPU. If the process obtains a time slice, it is the execution state. If it does not allocate a time slice, it is the ready state. The fields representing the status do not need to change.

    • In fact, TASK_RUNINGthis field corresponds to both the ready state of the process and the running state of the process.

    • Only processes in this state may run on the CPU. There may be multiple processes in the executable state at the same time, and the task_struct structures (process control blocks) of these processes are put into the executable queue of the corresponding CPU (a process can only appear in the executable queue of one CPU at most). The task of the process scheduler is to select a process from the executable queue of each CPU to run on that CPU.

    • As long as the executable queue is not empty, its corresponding CPU cannot be lazy and must execute one of the processes. The CPU at this time is generally called "busy". Correspondingly, CPU "idle" means that its corresponding executable queue is empty, so that the CPU has nothing to do.

    • Many operating system textbooks define processes that are executing on the CPU as the RUNNING state, and processes that are executable but have not yet been scheduled for execution as the READY state. These two states are unified into the TASK_RUNNING state under Linux.

  • TASK_INTERRUPTIBLEand TASK_UNINTERRUPTIBLEare two sleep states, corresponding to the blocking state above. TASK_INTERRUPTIBLECan be woken up by signals again, TASK_UNINTERRUPTIBLEbut cannot be woken up by signals.

    • TASK_INTERRUPTIBLE, interruptible sleep state. The process in this state is suspended because it is waiting for a certain event to occur (such as waiting for a socket connection, waiting for a semaphore). The task_struct structures of these processes are put into the waiting queue of the corresponding events. When these events occur (triggered by external interrupts or triggered by other processes), one or more processes in the corresponding waiting queue will be awakened. Through the ps command, we will see that, under normal circumstances, the vast majority of processes in the process list are in the TASK_INTERRUPTIBLE state (unless the load on the machine is very high).

    • TASK_UNINTERRUPTIBLE, uninterruptible sleep state. Similar to the TASK_INTERRUPTIBLE state, the process is in a sleep state, but the process is uninterruptible at this time. Uninterruptible does not mean that the CPU does not respond to interrupts from external hardware, but that the process does not respond to asynchronous signals. That is, kill -9 cannot shut down/kill this kind of process. The significance of the TASK_UNINTERRUPTIBLE state is that certain processing flows of the kernel cannot be interrupted. If you respond to an asynchronous signal, a process for processing asynchronous signals will be inserted into the execution flow of the program (this inserted process may only exist in the kernel mode, or may extend to the user mode), so the original process will be interrupted. .

      When a process operates on certain hardware (for example, the process calls the read system call to read a device file, and the read system call eventually executes the code of the corresponding device driver and interacts with the corresponding physical device), it may be necessary to Use the TASK_UNINTERRUPTIBLE state to protect the process to prevent the interaction between the process and the device from being interrupted and causing the device to fall into an uncontrollable state. The TASK_UNINTERRUPTIBLE state in this case is always very short-lived and is basically impossible to capture through the ps command. There is also a TASK_UNINTERRUPTIBLE state that is easy to capture in Linux systems. After executing the vfork system call, the parent process will enter the TASK_UNINTERRUPTIBLE state until the child process calls exit or exec.

  • TASK_STOPPEDIt is the state when the process receives signals such as SIGSTOP and SIGTTIN. When your Linux process is running and you press Ctrl + z, the process will be in this state.

    • Send a SIGSTOP signal to a process, and it will enter the TASK_STOPPED state in response to the signal (unless the process itself is in the TASK_UNINTERRUPTIBLE state and does not respond to the signal). (SIGSTOP, like the SIGKILL signal, is very mandatory. The user process is not allowed to reset the corresponding signal processing function through the signal series system call.)

    • Sending a SIGCONT signal to the process can restore it from the TASK_STOPPED state to the TASK_RUNNING state.

  • TASK_TRACEDIt is the status of the process being monitored.

    • When a process is being traced, it is in the special state TASK_TRACED. "Being tracked" means that the process is paused and waiting for the process that is tracking it to operate on it. For example, if you set a breakpoint on the tracked process in gdb, the process will be in the TASK_TRACED state when it stops at the breakpoint. At other times, the tracked process is still in the states mentioned earlier.

    • TASK_STOPPED and TASK_TRACED status judgment. For the process itself, the TASK_STOPPED and TASK_TRACED states are very similar, both indicating that the process is suspended. The TASK_TRACED state is equivalent to an additional layer of protection on top of TASK_STOPPED. The process in the TASK_TRACED state cannot be awakened in response to the SIGCONT signal. The debugged process can only return to the TASK_RUNNING state until the debugging process performs operations such as PTRACE_CONT and PTRACE_DETACH through the ptrace system call (operations specified through the parameters of the ptrace system call), or the debugging process exits.

  • TASK_DEAD- EXIT_ZOMBIE, exit status, the process becomes a zombie process. EXIT_DEADIt is the final state. Entering this state means that the process will be deleted from the system. EXIT_ZOMBIEIt is EXIT_DEADthe previous state. At this time, the process has terminated, but the parent process has not yet waited for wait()the system call to obtain its termination information. The process in this state is called a zombie process. . In this state, the kill command cannot kill you. You can think about how to clear zombie processes and how to avoid the existence of zombie processes.

    Regarding the exit-related process status (the above four), for more details, please see  Linux Process Analysis_deep_explore's blog-CSDN blog .

Linux process state transition diagram:

Common STAT codes for system processes:

Ready queue and blocking queue:

Ready queue: There may be multiple processes in the ready state in the system, and they are usually placed in a queue. As long as the ready queue is not empty, the CPU can always schedule processes to run and remain busy. This has nothing to do with the number of ready processes; unless the ready queue is empty, the CPU enters a waiting state and the CPU efficiency decreases.

Blocking queue: The system usually puts blocked processes into a queue, and even sets up multiple blocking queues based on different reasons for blocking.

Process composition

Those data structures quoted from the process - Zhihu (zhihu.com) , the task_struct structure under Linux - Baidu Library (baidu.com) .

The process generally consists of the following parts:

  • Process Control Block (PCB) : When each process is created, the system will create a corresponding PCB for the process. The PCB is the only indication that a process exists.

    • The essence of creating a process is to create the PCB of the process. PCB should be able to display process identities and relationships, mark task status, mark permissions, help task scheduling, etc.

    • In the Linux kernel, processes and threads are unified as tasks. The process control block of the Linux kernel is a task_structstructure, which contains:

      • The identifier of the process (a process identifier , or PID) ; (the process’s own unique identifier PID)

      • Register values ​​for the process including, notably, the program counter and stack pointer values ​​for the process; Program counter PC, etc.)

      • The address space for the process;

      • Priority (in which higher priority process gets first preference. eg., nice value on Unix operating systems);(优先级)

      • Process accounting information, such as when the process was last run, how much CPU time it has accumulated, etc;

      • Pointer to the next PCB i.e. pointer to the PCB of the next process to run;

      • I/O Information (i.e. I/O devices allocated to this process, list of opened files, etc).

    • pid_t pid; // Display the id of your own process 
      pid_t tgid; // The id of the main thread of the process 
      struct task_struct *group_leader; // Point to the address of the main thread

      Each process will create a main thread, so if there is just a single process, and the main thread created by the process by default, then and pidwill tgidbe themselves. If it is a child thread created by a process, then pidit is its own idand tgidpoints to the id of the main thread of the process.

    • struct task_struct __rcu * real_parent; 
      struct task_struct __rcu * parent; // Points to the parent process 
      struct list_head children; // All child processes of the parent process are in the child process linked list, and this points to the head of the linked list. 
      struct list_head sibling; // Connect sibling processes

      The process is a tree-like structure (a tree composed of linked lists). Except for process No. 0, all processes are created by the parent process, so operations on the parent process can easily affect the child process. Therefore, the data structure of the process naturally shows which parent, child, and sibling processes the process has.

  • Program segment : A program segment is a segment of program code in a process that can be scheduled to be executed on the CPU by the process scheduler.

  • Data segment : It can be the original data processed by the process corresponding to the program, or it can be the intermediate result or result data generated when the program is executed.

process control

The main function of process control is to effectively manage all processes in the system. It has the functions of creating new processes, canceling existing processes, and realizing process state transitions . In short, process control is to achieve process state transition.

The general process control program segment is an "atomic operation" and is not allowed to be interrupted during the execution process; it uses the two privileged instructions "interruption off instruction" and "interruption on instruction" to achieve atomicity.

Process creation and termination

Process control concepts related to process creation and termination.

Events that cause process creation

  • User login: In the time-sharing system, if the user logs in successfully, the system will create a new process for him.

  • Job scheduling: In a multi-channel batch processing system, when a new job is put into the memory, a new process will be created for it.

  • Providing services: When the user makes certain requests to the operating system, a new process will be created to handle the request. Launching a program creates a new process.

  • Application request: The user process actively requests the creation of a child process.

The process by which the operating system creates a new process

  1. Step1: Assign a unique process identification number to the new process and apply for a blank PCB (PCB is limited). If the PCB application fails, the creation fails.

  2. Step2: Allocate required resources to the process, such as files, memory, I/O devices and CPU time. These resources are obtained from the operating system, or from its parent process. If there are insufficient resources (such as memory), the creation does not fail at this time, but it is in the creation state, waiting for memory resources.

  3. Step3: Initialize the PCB, mainly including initialization flag information, initialization processor status information and initialization processor control information, as well as setting the priority of the process, etc.

  4. Step 4: If the process ready queue can accept the new process, insert the new process into the ready queue and wait to be scheduled to run.

Parent process creates child process

A process is allowed to create another process. At this time, the creator is called the parent process, and the created process is called the child process. The child process can inherit the resources owned by the parent process; when the child process is revoked, the resources it obtained from the parent process should be returned to the parent process; when the parent process is revoked, all its child processes are usually revoked at the same time. . The parent process and the child process share some resources, but they cannot share the virtual address space. When the child process is created, resources, such as virtual address space, will be allocated to the child process.

Of course, the parent process and the child process can execute concurrently. The process control block (PCB) is the only sign of the existence of a process, and each process has its own PCB. The parent process and the child process cannot use the same critical resource at the same time. Critical resources can only be used by one process at a time (critical resources have a lock mechanism and can only be accessed mutually).

All processes created in the Linux system are created by existing processes (except for process No. 0). The created process is called a child process, and the process that creates the child process is called the parent process. Linux processes are strung together in a tree structure.

Events that cause process termination

  • Normal end: Indicates that the task of the process has been completed and is ready to exit.

  • Abnormal end: It means that some abnormal event occurred while the process was running, which prevented the program from continuing to run, such as storage area out of bounds, protection error, illegal instruction, privileged instruction error, run timeout, arithmetic operation error, I/O failure, etc.

  • External intervention: refers to the process terminating at the request of the outside world, such as operator or operating system intervention, parent process request and parent process termination.

The process by which the operating system terminates a process

  1. Step1: According to the identifier (PID) of the terminated process, retrieve the PCB of the process and read the status of the process from it.

  2. Step2: If the terminated process is in the running state, the running of the process should be terminated immediately and the processor resources should be allocated to other processes.

  3. Step3: If the process has descendant processes, all of its descendant processes should be terminated.

  4. Step4: Return all resources owned by the process to either its parent process or the operating system.

  5. Step5: The PCB is deleted from the queue.

View and interpret individual processes

ps - efWe can view the current process of the system through the command on the Linux system .

  • UID is the user's identifier (the UID of the process created by the root user is root. If I create it myself, it should be my user name.

  • PID represents the ID of the current process.

  • PPID represents the parent process id of the current process.

Create process No. 1 and process No. 2 through process No. 0, then create a user-mode process through process No. 1, and then create a kernel-mode process through process No. 2, thus generating a Linux process tree.

  • 0号进程: During the kernel initialization process, process No. 0 will be created first through the instruction struct task_struct init_task = INIT_TASK(init_task). This is the only process that was not spawned via fork or kernel_thread. is the first in the process list. But this process is not a process in the actual sense, it is similar to the head of a linked list. Therefore, although process No. 0 was created in the kernel state, we cannot say that process No. 0 is the first process in the kernel state. Instead, we must say that process No. 2 is the first process in the kernel state.

  • 1号进程: Created by calling the instruction kernel_thread(kernel_init, NULL, CLONE_FS) to switch from kernel mode to user mode. Process No. 1 is the ancestor of all user modes. Process 1 is also called the init process . It is the second kernel thread created during kernel initialization. Its running code is the kernel function init(). As long as the system does not end, the init process will never terminate. It is responsible for creating and monitoring the activities of all processes outside the operating system.

  • 2号进程: Created by calling the instruction kernel_thread(kthreadd, NULL, ClONE_FS | CLONE_FILES). Process No. 2 is responsible for the scheduling and management of all kernel state processes and is the ancestor of all processes in the kernel state. (Note that the kernel state does not distinguish between threads and processes, so processes and threads are both tasks).

pstreeCommand to display the entire process tree,  Linux basic command---display process tree pstree_weixin_34023863's blog-CSDN blog .

Guess you like

Origin blog.csdn.net/Staokgo/article/details/132630630