Linux Kernel Driver (V): 1. Linux Process Control

Source: Internet
Author: User

I. process 4 elements

1. There is a program for execution. This program is not necessarily dedicated to a specific process and can be shared with other processes.

2. Process-specific kernel space Stack

3. There is a task_struct data structure in the kernel (actually the PCB Process Control Block)

4. Independent user space

PS. Analysis of processes, user threads, and kernel threads in Linux: (remember this figure)

2. process description

1. in Linux, threads and processes are represented by struct task_struct (this data structure is what we call PCB). It contains a large amount of information describing processes/threads, here we only list some important

(1) pid_t PID; // process number; maximum value: 1 billion. In fact, the thread (User thread, kernel thread) and process correspond to a PID

(2) Volatile long state; // Process status

= Task_running, corresponding to the ready and running state. The process is in the task_running state after it is created. Therefore, as long as a process can be executed, it is in task _ running

= Task_interrptible, which corresponds to the interruptible blocking in the blocking state. (In addition to being awakened when the waiting condition is true, it can also be awakened by a signal or interruption)

= Task_uninterrptible, which corresponds to the non-disruptive blocking in the blocking state. (Wake up only when the resource is valid. It cannot be awakened by signal or interruption)

= Task_stopped, corresponding to the terminated execution in the blocking state. When receiving signals such as sigstop and sigtstp, the process enters this state. After receiving the sigcont signal, the process returns to task_running.

= Task_killable: corresponds to the sleep state in the blocking state. The principle is similar to task_uninterrptible, but can be awakened by the fatal signal sigkill.

= Task_traced: A debugging process. In the Code debugging stage, we can control the process to stop and run.

= Task_dead: when the process exits (do_exit is called), the state field is set to this state.

(3) int exit_state; // The status when the process exits (in Linux, the process exits and is divided into many States, instead of being changed from task_running to task_dead)

= Exit_zombie (zombie process): the execution of the process is terminated, but the parent process has not released waitpid () to collect information about the dead process. (The body remains there when the person is dead)

= Exit_dead: indicates the final state of the process. The parent process has used wait4 () or waitpid () System Call to collect information, so the process will be deleted by the system. (People are dead and bodies are burned)

2. struct mm_struct * mm; // process user space description pointer. the pointer of the kernel thread is null (because it does not exist)

3. Unsigned int policy; // scheduling policy of the process

4. Int PRIO; // process priority, which is within the range of (0-max_prio-1. The (max_rt_prio-MX_PRIO-1) is a non-real-time process. Max_prio = 100.

5. Int static_prio; // static priority

6. struct sched_rt_entiry RT; RT-> time_slic; // time slice. By default, the time slice of a process depends on the Static Priority of the process.

7. struct distribution in the kernel:

As the kernel continues to grow and expand, the content of task_struct is constantly increasing. To fix the fixed size of a process description (one or two pages), after the 2.6 kernel, we separate task_struct, use thread_info struct to replace the original task_struct location, and then set a pointer in thread_info to point to the task_struct outside, so as to ensure a fixed process size.

8. Current: use the current pointer in the Linux kernel to point to the task_struct of the currently running process.

3. Process Creation Diagram

4. Process destruction Diagram

Process destruction can be driven by several events: normal process termination, signal, or exit function calls. No matter how you exit, you must call the kernel function do_exit to end the process.

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.