Linux process management subsystem

1.Linux process elements

Procedures and processes

  • Program: stored on the disk image of a series of executable code and data, it is a static entity
  • Process: is a program being executed, it is a dynamic entity.

Four elements of the process

  • There was a program for its implementation , this program is not necessarily a proprietary process, can be shared with other processes
  • There are process-specific kernel stack space .
  • In the kernel has a task_struct data structure , known as the process control block (the PCB) . With this data structure, the process in order to become a basic unit of kernel scheduling, dispatching receiving core.
  • There are separate user space (distinguishing process, user thread, a kernel thread method)

  • Note: You can not share the same address space between different processes, the same process in different threads share the same process of resources.

Linux process state
                             switching diagram of three classical state

About Linux process status (more than six states):

  • . 1, a TASK_RUNNING (ready state or the running state)
  • The process is being executed CPU, or ready, subject to execution. When a process has just been created, in TASK_RUNNING state.
  • 2, TASK_INTERRUPTIBLE (blocking state)
  • Is in the process of waiting, waiting to be awakened when the condition is true, it may be a signal or interrupt.
  • . 3, the TASK_UNINTERRUPTIBLE (special blocking state of one)
  • It is in the process of waiting, to be an effective wake-up resources, but not by other processes by signal or interrupt. --- This is an irrational state, if you want the signal to kill the process there is no way.
  • . 4, of TASK_KILLABLE (supplement unreasonable third signals)
  • In the process of introducing a new version of Linux2.6.25 turn sleep state. The principle is similar TASK_UNINTERRUPTIBLE, but can be awakened SIGKILL signal.
  • 5, TASK_TRACED (is in the process being debugged state)
  • 6, TASK_DEAD ((call do_exit), which the state when the process exits)

Linux process descriptor

  • In the Linux kernel code, threads, processes use structure task_struct (sched.h) to represent ,, in the file /include/linux/Sched.h. It contains a wealth of information describing processes / threads, one of the more important ones are
    • pid pid_t;        // process ID
    • State Long ; // Process Status
    • PRIO int;    // process priority

2. Process Scheduling

Learning Scheduling need to have the following three points:

  • 1. scheduling policy
  • Scheduling an opportunity
  • Step 3. Scheduling

Scheduling policy

  • Priority real-time processes of higher priority than the general process.
  • SCHED_FIFO: First In First Out of real-time process , the process executed by the CPU in the order they request the CPU
  • SCHED_RR: round-robin of real-time processes , each process is assigned a period of time, allowing the process to run in the time period, if the process is still running at the end of the time slice, CPU will be assigned to another denial process. If the blocking or end before the end of the time slice, the CPU is switched immediately. Usually the time slice is set 20-50ms
  • SCHED_NORMALL (SCHED_OTHER): ordinary time-sharing process
  • SCHED_BATCH: Batch Process
  • SCHED_IDLE: Only when the system is idle scheduling process can be executed

Scheduling time

  • What happened when scheduling that call at what time schedule () function

Active

  • Call the kernel directly schedule (); When a process needs to wait for resources and temporarily stop operating, will make its state put on hold, and take the initiative to request scheduling, allowing the CPU.
    • Example:
    • // current is a macro is a pointer to task_struct, ie * task_struct.
    • to current-> State = TASK_INTERRUPIBLE ; // represents the current state of the process is set in blocking state, i.e., give up the CPU
    • schedule();

Divided into two types according to seize the opportunity: user mode seize (Linux2.4 linux2.6) and kernel mode seize (Linux 2.6 support only)

User mode preemption

  • Preemption occurs in user mode:
    • a) from the system call returns the user space .
    • b) to return from the interrupt handler user space .
  • Kernel is about to return to user space of time, if need_resched flag is set, it will result in schedule () is invoked, that user preempt
  • Need_resched case flag is set:
    • a) When a process is exhausted its time slice, will mark set need_resched
    • b) When a higher priority process entered an executable state flag is also set need_resched
  • Defects user preemption mode:
    • Process / thread to run once the kernel mode can execute until it is time to give up or run out of film, and this will lead to some emergency processes or threads get long run, reduce the entire system in real time

Improved ways:

  • It allows the system also supports preemptive kernel mode, higher priority processes or threads can preempt lower-priority processes or threads running kernel mode

Preemptive kernel mode

  • Kernel preemption may occur:
    • a) interrupt handler is complete, return to the kernel space before
    • b) When the kernel code again with preemption, such as soft interrupt enable unlocking and the like

In the system supports kernel preemption, certain exceptions are not allowed to seize:

  • a) kernel is running interrupt processing ;
  • b) being the kernel interrupt context Bottom Half (bottom half interrupt) process. Hardware interrupt soft interrupt will execute before returning, this time still in the interrupt context
  • c) the process is holding spinlock spin lock, writelock / readlock read-write locks , etc., while holding the locks, should not be preempted, or due to the preemption process may lead to other long-term lack lock, and let the system is dead lock status.
  • d) the kernel is executing scheduler Scheduler. The reason is that in order to seize the new schedule, there is no reason to run the scheduler preempt scheduler.

Kernel preemption count:

  • To ensure that the Linux kernel will not be preempted In the above case, preemptive kernel uses a variable the preempt_count , called the kernel preemption count. This variable is set in thread_info structure in the process. Whenever the kernel to enter more than several states, variable preempt_count is increased by one, indicating the kernel preemption is not allowed. Whenever more than several exits from the kernel state variables preempt_count decrements while being determined with preemptive scheduling.

Scheduling step

Schedule function works as follows:

  • 1) clean-up processes currently running; (the current release process)
  • 2) Select a process to run; (a selection process next scheduling policy based on)
  • 3) Set the operating environment of the new process; (such as a stack, register)
  • 4) a process context switch.

Guess you like

Origin blog.csdn.net/qq_22847457/article/details/90814686