Introduction to multiple tasks in Linux
First, let's briefly introduce what is a multitasking system? What are the tasks, processes, and threads? What is the difference between them ?, In this way, you can take a macro look at the three, and then explain them carefully.
What is a multitasking system?A multi-task system allows you to run multiple applications at the same time.ProgramEach application is called a task..
Task definition: a task is a logical concept. It refers to a task completed by a software, or a series of operations to achieve a specific goal.
Process Definition: A process is a dynamic execution process of a program with independent functions on a dataset. It is the minimum unit for the system to allocate and schedule resources.
Thread definition: A thread is an independent running route in a process. It is the smallest unit of Processor Scheduling and can also be a lightweight process.
After reading the definition, I am a little dizzy. Let's talk about their differences in plain words.① A task is usually executed once by a program. A task contains one or more subtasks that complete independent functions. This independent subtask is a process or thread. ② A process can have multiple threads, and each thread must have a parent process.
Task
A task is a logical concept. It refers to a task completed by a software, or a series of operations to achieve a specific goal. A task is usually executed once by a program. A task contains one or more subtasks that complete the independent functions. This independent subtask is a process or thread. For example, a antivirus software runs a task at a time to protect computer systems from virus attacks. This task contains multiple independent sub-tasks (processes or threads ), including real-time monitoring, timed detection and removal, firewall and user interaction. 1 shows the relationship between tasks, processes, and threads.
Process
Basic concepts of processes
A process is a dynamic execution process of a program with independent functions on a dataset. It is the basic unit for the system to allocate and schedule resources. Multiple processes can be activated concurrently when a task is running. These processes work together to fulfill a final goal of the task.
The process features concurrency, dynamic, interactive, independent, asynchronous, and other main features.
There is a fundamental difference between a process and a program: The program is a static section.CodeIt is an ordered set of commands stored in non-volatile memory without any execution concept, while a process is a dynamic concept, which is a process of program execution, it includes the entire process of dynamic creation, scheduling, and elimination. It is the smallest unit of program execution and resource management.
Process Structure in Linux
Processes include not only program commands and data, but also all registers of program counters and processors and process stacks that store temporary data. Therefore, the processes being executed include all the current activities of the processor.
Because Linux is a multi-task, multi-process operating system, other processes can run only after the system assigns the processor permission to itself. When a running process waits for other system resources, the Linux kernel takes control of the processor and assigns the processor to other waiting processes.AlgorithmDecide which process the processor is allocated to, that is, the kernel will not let the processor idle.
The kernel stores all processes in a two-way cyclic linked list (process linked list), where the head of the linked list is the init_task descriptor. Each item in the Linked List is of the task_struct type and is called the process descriptor structure. This structure contains all information related to a process, which is defined in the <include/Linux/sched. h> file. Task_struct has a large kernel structure, which can fully describe a process, such as the process Status, basic information of the Process, process identifier, memory information, parent process information, terminal information related to the process, current working directory, opened file information, received.
The following describes in detail the two most important fields in the task_struct structure: State (Process status) and PID (process identifier ). To learn more about task_struct, click here.
(1) Process status
Processes in Linux are in the following states.
● Running status (task_running): The process is currently running or waiting for scheduling in the running queue.
● Resumable congestion (task_interruptible): the process is in the blocking (sleep) state, waiting for some events to occur or can occupy some resources. Processes in this state can be interrupted by signals. After receiving a signal or an explicit wake-up call (for example, calling the wake_up macro: wake_up, wake_up_interruptible, etc.), the process changes to the task_running status.
● Non-disruptive blocking status (task_uninterruptible): this process is in a state similar to an interrupted blocking status (task_interruptible), but it does not process signals, A process that transmits signals to this state cannot change its state. In some specific circumstances (the process must wait until some events that cannot be interrupted occur), this status is very useful. The wake-up call is displayed only when the event it is waiting for occurs.
● Terminable blocking status (task_killable): the running mechanism in this status is similar to task_uninterruptible, except that processes in this status can respond to a fatal signal. It can replace the valid but potentially unrecoverable blocking status (task_uninterruptible) and the stoppedblocking status task_interruptible that is easy to wake up but with poor security ).
● Paused (task_stopped): the execution of a process is paused. When a process receives signals such as sigstop, sigstp, sigttin, and sigttou, it enters the paused state.
● Trace status (task_traced): the execution of the process is paused by the debugger. When a process is monitored by another (for example, the debugger uses the ptrace () system to call the Monitoring Test Program), any signal can place the process in the Tracking State.
● Botnet status (exit_zombie): The process stops running. The parent process has not used the wait function family (for example, the waitpid () function) and other system calls to "zombie ", that is, wait for the parent process to destroy it. In this state, the process "corpse" has abandoned almost all the memory space, no executable code, and cannot be scheduled. It only keeps a position in the process list, records the launch status and other information of the process for other processes to collect.
● Zombie withdrawal status (exit_dead): This is the final state. After the parent process calls the wait function family "zombie", the process is completely deleted by the system.
The Conversion Relationship between them is as follows:
A process can use the set_task_state and set_current_state macros to change the status information of the specified process and the status of the current process.
(2) process identifier
The Linux kernel uses the unique process identifier PID to identify each process (just like the file descriptor ). PID is stored in the PID field of the Process descriptor, the newly created PID is usually the PID of the previous process plus 1, but the PID value has the upper limit (maximum = PID_MAX_DEFAULT-1, usually 32767 ), you can check/proc/sys/kernel/pid_max to determine the maximum number of processes in the system.
After the system is started, the kernel is usually represented by a process. A macro current pointing to task_struct is used to record running processes. Current often appears in kernel code as a process descriptor structure pointer. For example, current-> PID indicates the PID of the process being executed by the processor. When the system needs to view all processes, the for_each_process () macro is called, which is much faster than the system can search for arrays.
In Linux, the system call functions for obtaining the current process number (PID) and the parent process number (ppid) are getpid () and getppid (), respectively ().
Process creation, execution, and Termination
(1) process creation and execution
First, we need to know what is creation and what is execution! I didn't understand it at the beginning. Creating a process is to generate a new process, which everyone knows. The execution of the process, when talking about the definition of the Process, refers to the running sub-tasks. To put it bluntly, the execution of the process is to let the generated process do something, don't take care of that.
Many operating systems provide a mechanism to generate processes. That is to say, first create processes in the new address space, read executable files, and then start execution. In Linux, process creation is very special. It splits the above steps into two separate functions for execution: fork () function and exec function family.First, the fork () function creates a sub-process by copying the current process (note that the resource has not been copied at this time. Let's take a look at the page Copy technology when writing ), the difference between a child process and a parent process is that different PIDs, ppids, and some resources and statistics. The exec function family reads executable files and loads them into the address space to start running.
(2) termination of a process
Process Termination also requires a lot of tedious work. The system must ensure that the resources occupied by the process are recycled and notify the parent process. In Linux, the terminated process is set to a zombie state. At this time, the process cannot be put into operation. The existence of the process only provides information for the parent process and the application is rejected. After the parent process obtains information, it starts to call the wait function family and stops the child process. All resources occupied by the child process are released.
Memory Structure of processes
The Linux operating system uses virtual memory management technology, so that each process has a process address space that does not interfere with each other. This address space is a linear virtual space of 4 GB. All users see and access this virtual address, and the actual physical memory address cannot be seen. Using this virtual address can not only protect the Operating System (users cannot directly access the physical address), but also, more importantly, the user program can use a larger address space than the actual physical memory.
The 4 GB process address space is divided into two parts: user space and kernel space. The user address space ranges from 0 to 3 GB (0xc000 0000), and the kernel Address Space occupies 3 GB to 4 GB. Generally, a user process can only access the virtual address of the user space, but cannot access the virtual address of the kernel space. You can access the kernel space only when the user process uses a system call (representing that the user process is executed in the kernel state. When a process is switched, the user space changes. The kernel space is mapped by the kernel, which does not change with the process and is fixed. The kernel space address has its own page table, and user processes have different page tables. The user space of each process is completely independent and irrelevant. The virtual memory space of a process is shown in Figure 3. The user space includes the following functional areas:
● Read-only segment: Contains program code (. init and. Text) and read-only data (. rodata ).
● Data Segment: stores global and static variables. The readable and writable data segment (. Data) Stores initialized global and static variables, and the BSS data segment (. BSS) Stores uninitialized global and static variables.
● Heap: the system automatically allocates and releases the function, and stores the function parameter values, local variable values, and return addresses.
● Stack: stores dynamically allocated data, which is generally dynamically allocated and released by programmers. If the programmer does not release the program, it may be recycled by the operating system at the end of the program.
● Memory ing area of the Shared Library: This is the ing area between the Linux dynamic linker and other shared code library code.
In Linux, each process has a directory corresponding to the/proc file system (for example, describe the information of the INIT process in the/proc/1 directory), therefore, you can use the proc file system to view the address space ing of a process. For example, if you run an application whose process number is 13703, enter the "cat/proc/13703/maps" command to view the memory ing of the process.
Thread
As mentioned above, processes are 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 leads to complicated context switches and other actions required by the process during switchover. To further reduce the idle time of the processor, support for multi-processor and reduce the overhead of context switching, another concept emerged in the process evolution-thread. It is an independent running route in a process and the smallest unit of Processor Scheduling. It can also be called a lightweight thread.The thread can access the memory space and resource allocation of the process and share it with other threads in the same process.. Therefore, the context switching overhead of the thread is much lower than that of the creation process.
A process can have multiple threads, and each thread must have a parent process. A thread does not have system resources. It only has some data structures necessary for running, such as the stack/register and thread control block (TCB ), the thread shares all resources of the process with other processes of its parent process. Note that because the thread shares the resources and address space of the process, any thread's operations on system resources will affect other processes. We can see that synchronization in multiple threads is a very important issue. In a multithreaded system, the relationship between processes and threads is shown in Figure 4.
In Linux, there are three types of threads: ① user thread ② lightweight thread ③ kernel thread
Next: http://blog.csdn.net/mybelief321/article/details/9054405