Linux multitasking mechanism

Introduction of multitasking mechanism under Linux

Multitasking means that users can run multiple applications at the same time, and each application being executed is called a task. Linux is an operating system that supports multi-tasking. Compared with single-tasking systems, its functions are much enhanced. Multitasking operating systems use a certain scheduling strategy to support concurrent execution of multiple tasks. In fact, the processor (single core) can only perform one task at a time. Each task is assigned a time slice (ms level) when it is created, and when the task is executed (occupying the CPU), the time slice decreases. The operating system will schedule other tasks to execute when the time slice of the current task runs out. Because tasks will frequently switch execution, it gives the user the feeling that multiple tasks are running at the same time.
There are usually three basic concepts in a multitasking operating system: tasks, processes, and threads.

1. Task

A task is a logical concept that refers to an activity completed by a piece of software, or a series of operations to achieve a certain purpose. Usually a task is a run of a program. A task contains one or more subtasks that complete independent functions. This subtask is a process or thread. The relationship between tasks, processes and threads is shown in the figure below
The relationship between tasks, processes and threads

2. Process

2.1 Basic concept of process

A process refers to a dynamic execution process of a program with independent functions on a certain data set. It is the basic unit of the operating system for resource allocation and scheduling. The running of a task can activate multiple processes, which cooperate with each other to complete an ultimate goal of the task. The process is concurrency, dynamic, interactive and independent.
The Linux system mainly includes the following three types of processes:

  • Interactive process: This type of process often interacts with the user and needs to wait for the user's input
  • Batch process: This type of process does not require interaction with the user, and usually runs in the background
  • Daemon: This type of process has been running in the background and is not associated with any terminal
2.2 Process structure

The process not only includes the instructions and data of the program, but also includes the program counter and all registers of the processor and the process stack for storing temporary data. The kernel stores all processes in a two-way circular linked list (process linked list). Each item in the linked list is a task_struct, which is called a process control block (PCB) structure.
The two most important fields in the task_struct structure are: state (process state) and pid (process identifier)
1. Process state

  • Running status (TASK_RUNNING): The process is running, or is waiting for scheduling in the running queue
  • Interruptible blocking state (TASK_INTERRUPTIBLE): The process is in a blocking (sleeping) state, waiting for certain events to occur or being able to occupy certain resources
  • Uninterruptible blocking state (TASK_UNINTERRUPTIBLE): similar to interruptible blocking state, except that it will not process the signal, and the process that passes the signal to this state cannot change its state
  • Suspended state (TASK_STOPPED): The execution of the process is suspended. When the process receives SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU, etc. signals, it will enter the suspended state
  • Zombie state (EXIT_ZOMBIE): the child process is over, the parent process does not exit, and the wait function family is not used to reclaim the exit status of the child process
  • Extinction state (EXIT_DEAD): This is the final state. After the parent process calls the wait function family to recycle, the child process is completely deleted and invisible
    Process state diagram

2. Process identifier The
Linux kernel uses a unique process identifier PID to identify each process. PID is stored in the pid field of task_struct. There is a limit to the number of processes that the system can create. You can check /proc/sys/kernel/pid_max to determine the upper limit.

2.3 Process creation, execution and termination
  • Create a child process by copying the current process through the fork() function
  • Read the executable file through the exec function family and load it into the address to start running
  • Set the terminated process to a dead state, and its parent process calls the wait function family at a certain time to reclaim the exit status of the child process, and then all resources occupied by the child process are released
2.4 The memory structure of the process

The Linux operating system uses virtual memory management technology, so that each process has an independent address space, which is a linear virtual space with a size of 4GB. This technology is not only safer (users cannot directly access physical memory), but user programs can use a larger address space than actual physical memory.
The 4GB process address space is divided into two parts-user space and kernel space. The user address space is 0 ~ 3GB (0xC0000000), and the kernel address space occupies 3 ~ 4GB. The
user space includes the following functional areas (segments)

  • Read-only section: It has read-only attributes and contains program code (.init and .text) and read-only data (.rodata)
  • Data segment: global variables and static variables are stored. The initialized data segment (.data) stores the global variables and static variables that are initialized; the uninitialized data segment (.bss) stores the global variables and static variables that have not been initialized.
  • Stack: automatically allocated and released by the system, storing function parameter values, local variable values, return addresses, etc.
  • Heap: Stores dynamically allocated data, which is generally allocated and released dynamically by the programmer. If the programmer does not release, the operating system may reclaim it when the program ends.
  • Memory mapping area of ​​shared library: the mapping area of ​​Linux dynamic linker and other shared library code

Three, thread

Process is the basic unit of program execution and resource allocation in the system. Each process has its own data segment, code segment, and stack segment, which results in a relatively large operating system overhead when the process is switched. In order to improve efficiency, the operating system has introduced another concept-thread, also known as lightweight process.
A process can have multiple threads, and each thread shares the resources owned by the process. Because the threads share the resources and address space of the process, any thread's operation on system resources will affect other threads.

Guess you like

Origin blog.csdn.net/Chuangke_Andy/article/details/108273129