Analyze the linux process model

1. Process concept

  A process is a running activity of a program with independent functions on a set of data. It can apply for and own system resources, is a dynamic concept, is an active entity. It is not just the code of the program, but also the current activity, represented by the value of the program counter and the contents of the processing registers.

2. Introduction to linux system

  Linux is known for its high efficiency and flexibility. The modular design structure of Linux enables it to run on expensive workstations and realize all Unix features on cheap PCs. ability. Linux is freely available under the GNU Public License and is a POSIX-compliant operating system. The Linux operating system software package not only includes a complete Linux operating system, but also includes application software such as text editors and high-level language compilers. It also includes an X-Windows graphical user interface with multiple window managers, allowing us to operate the system using windows, icons, and menus as we did with Windows NT.

3. How does linux organize processes

  3.1 State of the process

    When a process executes, it changes state depending on the situation. Process state is the basis for scheduling and swapping.

    ①R (TASK_RUNNING): executable state/running state

    ②S (TASK_INTERRUPTIBLE): interruptible sleep state

    ③ D (TASK_UNINTERRUPTIBLE): Uninterruptible sleep state

    ⑤T (TASK_STOPPED or TASK_TRACED): Pause state or tracking state

    ⑥Z (TASK_DEAD - EXIT_ZOMBIE): exit status, the process becomes a zombie process

  3.2 Process Scheduling Information

    The scheduler uses this information to decide which process in the system should most run.

    need_resched scheduling flag (next scheduling opportunity)

    Nice static priority (time slice of the process)

    Counter dynamic priority (remaining time slice)

    Policy scheduling policy

    rt_priority real-time priority (only meaningful for real-time processes)

  3.3 Process identifiers

    Used to identify different processes, so the process identifier is introduced.

    Pid Process Identifier

    Uid, Gid User Identifier, Group Identifier

    Euid, egid Effective User Identifier, Effective Group Identifier

    Suid, sGid backup user identifier, backup group identifier

    Fsuid, fsgid file system user identifier, file system group identifier

  3.4 Process Organization

    3.4.1 current macro

      When the process is executed on the cpu, the kernel obtains a pointer to the process task_struct through current

       Here is a piece of code related to the architecture:

       static inline struct task_struct *get_current(void)
          {  struct task_struct *current; __asm__("andl %%esp,%0; ":"=r" (current) : "0" (~8191UL));return current;}

    3.4.2 Bidirectional circular list

        The prev_task and next_task fields in the task_struct structure of each process are used to implement this linked list.

      The head and tail of the linked list are init_task, which corresponds to process 0 (pid is 0), which is the so-called empty process, which is the ancestor of all processes.

    3.4.3 Run Queue      

        Run queue When the kernel is looking for a new process to run on the CPU, it must only consider the process in the runnable state (that is, the process in the TASK_RUNNING state), because scanning the entire process linked list is quite inefficient, so the introduction of runnable A doubly linked list of state processes, also known as a run queue.

      The queue is maintained by a linked list of two pointers run_list in the task_struct structure. There are two signs of the queue: one is the "empty process" idle_task; the other is the length of the queue, that is, the number of processes in the runnable state (TASK_RUNNING) in the system, represented by the global integer variable nr_running.

    3.4.4 Waiting queue        

    Waiting queue processes must often wait for certain events to occur, for example, waiting for a disk operation to terminate, waiting for system resources to be released, or waiting for time to pass a fixed interval. A wait queue implements conditional waiting on events, that is, a process that wishes to wait for a particular event puts itself in the appropriate wait queue and relinquishes control. Thus, the wait queue represents a group of sleeping processes that are woken up by the kernel when a certain condition becomes true. The waiting queue is implemented by a circular linked list.

    3.5 State transition diagram of the process

    

4. Process scheduling

  4.1 First Come First Served Scheduling Algorithm

  The First Come First Served (FCFS) scheduling algorithm is one of the simplest scheduling algorithms, which can be used for both job scheduling and process scheduling. When this algorithm is used in job scheduling, each schedule selects one or more jobs that enter the queue first from the backup job queue, calls them into memory, allocates resources for them, creates processes, and puts them in ready queue. When the FCFS algorithm is used in process scheduling, each scheduling is to select a process from the ready queue to enter the queue first, assign a processor to it, and put it into operation. The process runs until it completes or an event occurs that blocks before giving up on the processor.

  4.2. Priority scheduling algorithm

   In the priority scheduling algorithm, each process is associated with a priority, and the kernel assigns the CPU to the highest priority process. Processes with the same priority are scheduled on a first-come, first-served basis. Assume that the execution time and priority of the process are as follows, and when these processes exist in memory at the same time, the priority-based scheduling algorithm should consider the problem of process starvation, because the high-priority process will always be scheduled first, with low priority Processes at the highest level may never be scheduled for execution by the kernel. Aging is a solution to this problem. The so-called Aging refers to gradually increasing the priority of the long-waiting process in the system. For example, if the priority range is from 127 to 0 (127 is the lowest priority), then we can increase the priority of the waiting process by 1 every 15 minutes. Eventually, after a period of time, even the process with the lowest priority of 127 will become the highest priority process in the system and thus be executed.

  4.3. Short process priority

  Shortest CPU runtime priority scheduling algorithm (SCBF--Shortest CPU Burst First) This algorithm selects the next process with "the shortest CPU execution period" from the ready queue and assigns a processor to it . Shortest job first scheduling is a special case of priority scheduling. In priority scheduling, we schedule according to the priority of the process, and in the shortest job priority scheduling, we schedule according to the execution time of the job.

  4.4.cfs scheduling algorithm

        The design idea of ​​the fully fair scheduler (CFS) is to model an ideal and accurate multitasking CPU on a real hardware. The ideal CPU model runs at 100% load, runs each task in parallel at exactly equal speed, and each task runs at 1/n speed, that is, an ideal CPU has n tasks running, and each task runs at the speed of the entire CPU. 1/n of the load.
       Since on real hardware, only one task can be run at a time, the concept of "virtual runtime" has to be introduced. The virtual runtime is the next time slice for a task to be executed on an ideal CPU model. In fact, the virtual run time of a task is the actual run time considering the total number of running tasks. 

       The main idea behind CFS is to maintain a balance (fairness) in providing tasks with processor time. In order to reflect the fair performance of CFS, there are two aspects
      (1) the running time of the process is equal

      (2) The sleep process is compensated

 The following are scheduling classes

static const struct sched_class fair_sched_class = {  
    .next           = &idle_sched_class,  
    .enqueue_task       = enqueue_task_fair,  
    .dequeue_task       = dequeue_task_fair,  
    .yield_task     = yield_task_fair,  
  
    .check_preempt_curr = check_preempt_wakeup,  
  
    .pick_next_task     = pick_next_task_fair,  
    .put_prev_task      = put_prev_task_fair,  
  
#ifdef CONFIG_SMP  
    .select_task_rq     = select_task_rq_fair,  
  
    .load_balance       = load_balance_fair,  
    .move_one_task      = move_one_task_fair,  
    .rq_online      = rq_online_fair,  
    .rq_offline     = rq_offline_fair,  
  
    .task_waking        = task_waking_fair,  
#endif  
  
    .set_curr_task          = set_curr_task_fair,  
    .task_tick      = task_tick_fair,  
    .task_fork      = task_fork_fair,  
  
    .prio_changed       = prio_changed_fair,  
    .switched_to        = switched_to_fair,  
  
    .get_rr_interval    = get_rr_interval_fair,  
  
#ifdef CONFIG_FAIR_GROUP_SCHED  
    .task_move_group    = task_move_group_fair,  
#endif  
}; 

 Function description

enqueue.task: This function will be called when a task becomes runnable. It puts the scheduling entity (process) into the red-black tree and increments the nr_running variable by 1.
dequeue.task: This function is called when a task exits the runnable state, it will remove the corresponding scheduling entity from the red-black tree and subtract 1 from the nr_running variable.
yield_task: With the compat_yield sysctl off, this function actually performs dequeue then enqueue; in this case it puts the dispatch entity at the far right of the red-black tree.
check.preempt curr: This function will check if the currently running task is preempted. The CFS scheduler module will perform a fairness test before actually preempting running tasks. This drives wakeup preemption. pick_next_task: This function selects the most appropriate process to run next.
load_balance: Each scheduler module implements two functions, load_balance_start() and load_balance_next(), using these two functions to implement an iterator that is called in the module's load balance routine. The kernel scheduler uses this method to achieve load balancing of processes managed by the scheduling module.
set_curr_task: This function is called when a task modifies its scheduling class or modifies its task group.
task_tick: This function is usually called from the time tick function; it may cause a process switch. This drives runtime preemption.
task new: The kernel scheduler provides the scheduling module with an opportunity to manage the launch of new tasks. The CFS scheduling module uses it for group scheduling, while the scheduling module for real-time tasks does not use this function.

5. Personal thoughts

  linux has many advantages as an open source system

    ① Open source allows everyone to develop the system at will and design the system they need.

    ②Security, provide a large number of network management software, network security software

    ③ Extensive hardware support, can run perfectly on today's mainstream processors

  However, it does not have a friendly graphical interface like windous. For beginners, it needs to learn a lot of operating commands, so it takes a long learning time to be familiar with the use.

    

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

eg:

https://www.aliyun.com/jiaocheng/158665.html

https://blog.csdn.net/fdssdfdsf/article/details/7894211

http://www.cnblogs.com/hanxiaoyu/p/5576277.html    

http://www.cnblogs.com/yangjiannr/p/Linux-jin-cheng-zhuang-tai.html

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325088492&siteId=291194637