Scheduling and algorithm of Linux common process state

Source: Internet
Author: User

I. Status of the process:

1. in the operating system:

A process is a dynamic entity, so he is alive. From creation to extinction, is the entire life cycle of a process. During this cycle, the process may go through a variety of different states. In general, all processes undergo the following 3 states:

(1) Ready state. means that the process has obtained all the additional resources needed to process the processor resources and is ready to begin execution. In this case, the process is said to be in the ready state.

(2) Blocking state. Refers to the process because it needs to wait for the required resources to abandon the processor, or the process does not own the processor, and other resources are not satisfied, so that even the processor can not start running. In this case, the process is in a blocking state. The blocking state is also called a sleep state or a wait state.

(3) Operation state. The process gets the processor, does not need to wait for any other resources, is executing the state, called the run-state. A process can use the requested resource only if it is in a running state.


2. in a Linux system, various states have been re-organized, resulting in several states of the Linux process:

  • R (runing): The process is in a running or ready state . This is the synthesis of the running state and the ready state process mentioned above. A process in the running state does not necessarily mean that he is being executed. Because in a multitasking system, each ready process requires concurrent execution, at some point in the running state, only one can get the processor, while the other processes must wait in a ready queue. Even in multiprocessor systems, Linux can only allow one processor to perform tasks at the same time.
  • D (disk sleep) Uninterruptable: Non-interruptible blocking state. is blocked while waiting for an event to not be activated by the signal, in addition to the I/O signal, the process in this state is waiting for the queue, when the resource is valid, can be awakened by the operating system, otherwise, will remain in the waiting state.
  • S (sleeping) interruptable: interruptible blocking state. is blocked while waiting for an event that may be activated by a signal , as in the non-interruptible blocking state, the process in this state waits for the queue, and when the resource is valid, an operating system can wake up. Unlike non-interruptible blocking states, a process in this state can also be awakened by signals from other processes.
  • T (Stopped): Pending state. The process is paused and requires a signal from another process to be awakened. There are two causes for this state. One is the response to the signal (Sigstop,sigstp,sigttin or Sigttou). The second is controlled by the ptrace call of the parent process, and the processor is temporarily handed over to the control process.
  • T (tracing stop): Trace stop
  • X (dead): The process has completely died, This is forever invisible
  • Z (Zombie): Zombie status. A state that represents the end of a process but not yet extinct. At this point the process has finished running and freed most of the resources, but the process control block has not been released.

3. Each process puts its information in the task_struct in this data structure, task_struct contains the contents:

    • status (task status, exit code, Exit signal ...)
    • priority
    • process ID (PID)
    • parent process ID (PPID)
    • child process
    • use case (CPU time, open file ...)
    • tracking information
    • dispatch information
    • memory management information

The data structure that holds the process information is called task_structand can be found in the include/linux/sched.h . All processes running in the system exist in the kernel in the form of task_struct lists.


                           

Joy, haha~:

about D and z an interesting explanation:
 
    A class of rubbish is not so easy to clean , which is our common state of D (uninterruptible Sleep), and a garbage process with a status of Z (Zombie). These garbage processes are either begging or not, like a disgruntled woman waiting for resources (D), or stiff and immortal, waiting for the cross-over (Z) like ghost, they are stuck in the CPU run_queue, the Load Average is old high, Have not seen my previous blog of the International friends also thought here resentment again what big thing. What to do? Shoot! kill-9! Let's see if you go. But the two kinds of garbage process is invulnerable, no matter what kind of marksmanship can not kill them. Helpless, had to reboot, like to destroy the avian flu as indiscriminately all culling!
 
    resentment D, often because I/O resources are not met, and trigger wait, in the kernel source fs/proc/array.c, its text is defined as "D (disk Sleep)",/* 2 * * ( So D is originally the letter of disk, which corresponds to the "#define Task_uninterruptible 2" in Include/linux/sched.h. For example, when the NFS server is closed, if the relevant directory is not umount in advance, the NFS client executes DF to hang up the entire login session, pressing CTRL + C, CTRL + Z is useless. Disconnect and then log in, perform PS AXF see just the DF process status bit has become D, kill-9 can not kill. The correct way to do this is to restore the NFS server immediately, again to provide services, just suspended DF process found its hard to wait for the resources, then complete the task, automatic extinction. If the NFS server is unable to recover the service, the relevant NFS mount entry in/etc/mtab should be removed before reboot, so that the system restart process hangs if the wait resource is not repeated when the reboot process calls Netfs stop.

Ghost Z why Kill, because it is dead, otherwise how to call Zombie (zombie)? Ghost does not disperse, nature is the reason that the knot is not solved. In Unix/linux, each process has a parent process, howl PID (Process ID), and, accordingly, the parent process number is called PPID (parent PID). When the process dies, it automatically closes the open file, discards the system resources that have occupied memory, swap space, and so on, and returns an exit status value to its parent process, reporting the death. If the program has a bug, it will be a problem in this last step. My son said I was dead, but I did not hear, not in time to collect the coffin departures, the son became a zombie. In Unix/linux the means of eliminating zombies is more brutal, perform PS AXJF find the zombie process of the parent process number (PPID, the first column), first kill its father, and then by the process of the Emperor Init (its PID is 1,ppid 0) to clean up the father and son zombies, cross the Wraith, to the birth of bliss. Note that the child process becomes a zombie just an eyesore, not in the way, if the zombie parent process is currently a priority, you must not be rushed to kill it.


Second, the process scheduling algorithm :
1, the basic state of process scheduling
1. Wait state: Wait for an event to complete, 2. Ready state: Wait for the system to allocate the processor for operation; 3. Operating state: Possession processor is running. Running state → wait states are often caused by waiting for peripherals, waiting for resources such as main memory to be allocated or waiting for manual intervention. Wait state → Ready state The waiting condition has been fulfilled and can be run only after being assigned to the processor. Run state → Ready state is not due to its own reasons, but by external causes the process of running the processor to let go, this time becomes the ready state. For example, a time slice is exhausted, or a higher priority process is used to preempt the processor, and so on. Ready state → run-state system Select a process in the ready queue to occupy the processor in a certain policy, and it becomes a running state2. ProcessorAdvanced, intermediate, and low-level scheduling jobs from the start of submission to completion, often experienced the following three levels of scheduling: Advanced scheduling: (high-level scheduling) is also known as job scheduling, it decided to put the backup job into memory operation; low-level dispatch: (Low-level scheduling) Also known as process scheduling, it determines the ready queue of a process to obtain the CPU, intermediate scheduling: (Intermediate-level scheduling) is also known as the introduction of virtual memory, in the internal, external memory swap area for process swap.3, the waynon-deprivation modeonce the Dispatch programProcessorassign to a process and let it run until the process finishes or occurswhen an event is blocked, the processor is assigned to another process. deprivation ModeWhen a process is running, the system can, based on some principle, deprive the processor that has been assigned to it and distribute it to other processes. The principle of deprivation is: priority principle, short process priority principle, time slice principle.4. Algorithm    metrics to measure process scheduling performance are: turnaround time , response time, cpu-i/o execution period.

<1> First-come-first service and short job (process) priority scheduling algorithm1. First come first service scheduling algorithm (FCFS)

First come first service (FCFS) scheduling algorithm is the simplest scheduling algorithm, 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 first enter the queue from the fallback job queue, puts them into memory, assigns them resources, creates processes, and then puts them in the ready queue. When the FCFS algorithm is used in the process scheduling, each schedule is to select a process from the ready queue that is first entered into the queue, assigning the processor to it and putting it into operation. The process has been running until it has completed or an event has been blocked before discarding the processor. (Evaluation:)FIFO algorithm service quality is poor, easy to cause the job user dissatisfaction, often as an auxiliary scheduling algorithm .

As follows:



Features: (1) conducive to long work, not conducive to short-term operations;

(2) is advantageous to the processor busy operation, is unfavorable to the I/O busy operation;


2. Short job (process) Priority scheduling algorithm SJ (P) F

Short job (process) Priority scheduling algorithm SJ (P) F, refers to the algorithm for short or short process priority scheduling. They can be used for job scheduling and process scheduling, respectively. The scheduling algorithm for short job first (SJF) is to select one or several jobs with the shortest estimated run time from the fallback queue and transfer them into the memory run. The short process first (SPF) scheduling algorithm chooses a process that estimates the shortest running time from the ready queue, assigns the processor to it, causes it to execute immediately and executes until it is completed, or when an event is blocked to abort the processor and then re-dispatched. (evaluation:) Although the algorithm can obtain better scheduling performance, it is difficult to know exactly the next CPU execution period, but only according to the execution history of each process to predict.

As follows:



<2> high priority priority scheduling algorithm1. Type of priority scheduling algorithm (FPF)

The highest priority priority (FPF) scheduling algorithm is introduced in order to take care of the urgent operation, so that it gets preferential treatment after entering the system. This algorithm is often used in batch processing system, as a job scheduling algorithm, also as a process scheduling algorithm in a variety of operating systems, can also be used in real-time systems. When the algorithm is used for job scheduling, the system will select several of the highest priority jobs from the fallback queue to load the memory. When used for process scheduling, the algorithm is to assign the processor to the ready queue of the highest priority process, at this time, the algorithm can be further divided into the following two kinds.

1) Non-preemptive priority algorithm

In this way, once the system has allocated the processor to the highest priority process in the ready queue, the process continues until it is completed, or the system can reassign the processor to another process that has the highest priority because of an event that causes the process to abandon the processor. This scheduling algorithm is mainly used in batch processing systems, and can also be applied to some real-time systems where real-time requirements are not stringent.

2) preemptive priority scheduling algorithm

In this way, the system also assigns the processor to the process with the highest priority and makes it execute. During its execution, however, as soon as another process with its higher priority arises, the process scheduler immediately stops the execution of the current process (the one that has the highest priority) and re-assigns the processor to the new highest priority process. Therefore, when this scheduling algorithm is used, the priority pi is compared with the priority PJ of the executing process J whenever a new ready process I is present in the system. If PI≤PJ, the original process PJ will continue to execute, but if it is PI>PJ, then immediately stop the execution of PJ, do a process switch, so that I process into execution. Obviously, this preemptive priority scheduling algorithm can better meet the requirements of urgent operation, so it is often used in the demanding real-time system, and in the batch processing and timeshare system with high performance requirements.


2. High response ratio priority scheduling algorithm

In batch processing system, the short job first algorithm is a better algorithm, and its main disadvantage is that the operation of long operation is not guaranteed. If we can introduce the dynamic priority described above for each job, and the priority of the job increases with the wait time to increase at rate A, then the long job waits for a certain period of time, there must be a chance to assign to the processor. The change law of this priority can be described as:

As the sum of waiting time and service time is the response time of the system to the job, the priority is equal to the response than the RP. Accordingly, it can also be expressed as:

It can be seen from the above formula:

(1) If the waiting time of the job is the same, the shorter the service time, the higher the priority, and therefore the algorithm is advantageous to the short job.

(2) When the time required for the service is the same, the priority of the job depends on its waiting time, the longer the waiting time, the higher the priority, so it realizes the first to serve first.

(3) For long jobs, the priority of the job can increase with the waiting time, and when its waiting time is long enough, its priority can be raised to high, thus also can obtain the processor. In short, the algorithm not only takes care of the short work, but also takes into account the order of the arrival of the operation, will not make long work long-term service. Therefore, the algorithm realizes a good tradeoff. Of course, when using this algorithm, the response ratio calculation must be done before each dispatch, which increases the system overhead.


<3> rotation scheduling algorithm based on time slice

1. Time Slice Rotation method(Rating:) The previous algorithms are mainly used toBatch processing System, cannot be used asCtssin the mainScheduling algorithm, in the time-sharing system, all useTime Slice rotationlaw.

1) Fundamentals

In the early time-slice rotation, the system queues up all the ready processes on a first-come-first-served basis, allocating the CPU to the team's first process and making it perform a time slice each time it is scheduled. The size of the time slice is from several ms to hundreds of Ms. When the elapsed time slice runs out, the clock interrupt request is made by a timer, which signals the scheduler to stop the execution of the process and sends it to the end of the ready queue, and then assigns the processor to the new team first process in the ready queue, while also allowing it to execute a time slice. This ensures that all processes in the ready queue can receive a time slice of processor execution times within a given time. In other words, the system can respond to requests from all users within a given time.

As follows:


2. Multilevel feedback queue scheduling algorithm

<4> Comparison of various algorithms:

(1)







Scheduling and algorithm of Linux common process state

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.