Analysis of process scheduling for Linux,windows and Unix

Source: Internet
Author: User

Summary : This article takes Linux, Unix, Windows operating system as an example, analyzes its process scheduling strategy, in order to have a deeper understanding of process scheduling process

Key Words : process scheduling priority time slice rotation real-time process sharing technology

Introduction : In both batch and time-sharing systems, the number of user processes is generally more than the number of processors, which will cause them to compete with each other for the processor. In addition, the system process also needs to use the processor. This requires the process scheduler to dynamically assign the processor to a process in the ready queue for execution, according to a certain strategy.

The specific functions of process scheduling can be summarized as follows:

as preparation for process scheduling, the Process Management module must record the execution and status characteristics of each process in the system in the PCB tables of each process. And, according to the status characteristics and resource requirements of each process, the process Management module also queues the PCB tables of each process into corresponding queue and makes dynamic queue transfer. Process scheduling module through the PCB changes to master the system of all the processes in the implementation and status characteristics, and at the appropriate time from the ready queue to select a process to occupy the processor.

The process of choosing a possessive processor

The main function of process scheduling is to select a process that is in a ready state and get the processor to execute according to a certain strategy. According to different system design purposes, there are a variety of selection strategies, such as the static priority number scheduling method with less overhead, which is suitable for the rotation method (Round Rolin) and multi-level mutual feed rotation (Round Robin with MULTIP1E feedback) of time-sharing system. These selection strategies determine the performance of the scheduling algorithm.

Making a Process context switch

-the context of a process includes the state of the process, the values of the variables and data structures, the value of the machine register and the PCB, and the program, data, and so on. The execution of a process is performed in the context of the process. When a process is being executed for some reason to give up the processor, the system does a process context switch to enable another process to execute. When a context switch is made, it is first checked whether context switching is allowed (in some cases, context switching is not allowed, such as when the system is performing a primitive that does not allow interrupts). The system then retains enough information about the process being switched so that it can be successfully resumed when it is later switched back to the process. After the system retains the CPU field, the scheduler chooses a new process in the ready state and assembles the context of the process so that the control of the CPU is held in the hands of the selected process.

Unix Process Scheduling

A process is an entity that is active in the execution system of a program. In a UNIX system, a process is defined as the execution of an image. An image is the execution environment of a computer, which includes the values of various registers and memories, the status of open files, the current directory, and so on. Part of the process Image: Register, Process control block proc structure and user structure, process data area PPDA (shared body segment (controlled by text structure), data segment and stack segment (with user stack and core stack). For the image of a process, the proc structure, the Process page table, and the text structure are resident memory, while the user structure, body segment, and data segment are very memory-resident parts. They form a whole in the user's virtual space, swapping in and out.

In Unix operating systems, the programs, whether performed at the user level or at the kernel level, appear on the site of a process, and all user programs run on their own process site. When these user processes request the kernel service through a system call, the kernel code that implements the system call continues to execute on the scene of the request process, which allows the kernel to easily access all state and other address space of the process. It also provides a way to record the current state of the kernel execution on behalf of the user program. For example, if you need to suspend the execution of a system call to wait for the I/O operation to complete, the kernel has a process for the status of the system call processing.

Because all the activities of the system, whether at the user level or at the kernel level, occur in the field of a process, all UNIX cores dispatch only the processes that need to be executed. When using the traditional time-sharing scheduling strategy, processes executed at the user level are not executed in the time of the split. Only the current kernel process is explicitly allowed to switch to another process executing in the kernel.

UNIX The core idea of process scheduling


Process state transition diagram

Process scheduling mechanism asking quantity

In traditional UNIX. The process priority setting is done through nice and set priorities, but unfortunately, the speed of the two system calls cannot be performed immediately with the highest priority process because of the cyclic scheduling (time slice scheduling) mechanism. When the process is finished with the smell of the film. No matter how the first level of scrambling out of the CPU. Also, because it is a non-preemptive kernel, a high-priority process cannot immediately interrupt the currently running process and gain CPU resources. This is not sufficient for some of the real-time applications that require the process to preempt the CPU immediately and run once.

Unix system is a multi-user time-sharing system, and its time-sharing is achieved by frequent scheduling of user processes, and the system scheduler is divided into two parts, namely the processor Scheduler (Swtch) and the process Swap program (SCHED). What I'm dealing with here is the processor scheduler. The processor scheduler is invoked in the following cases: (1) If a process has reached a point that it cannot exceed, then it will call "sleep", while "sleep" calls "Swtch";(2) a process that runs in a nuclear mindset, The variable "Runrun" is tested when it is going to be transferred to the user state, and if its value is not 0, it means that the higher priority process is ready to run. At this point the nuclear mentality process will also invoke "Swtch". The UNIX process scheduling policy is based on the dynamic priority number, and the precedence number setting has the following characteristics:

The Swtch is called by trap, sleep, expand, exit, stop, Xalloc. It is a very special process, divided into three paragraphs, involving 3 nuclear mentality processes.

① the first phase belongs to the part of the current process, and if the current process is not process 0, calling the Savu procedure saves the environment variables of the current process to U. U-rsav. Call the Retu process to recover the environment variables of process 0.

② the second phase belongs to process 0, the first clear Runrun flag, which indicates that a process with a higher priority than the current process is ready to run. Swtch through the Do loop to find the highest priority process (recalculate the priority of each process). If a ready process is found that satisfies the condition, it is removed from the ready queue and the priority of the process is set to the current priority curpri.

③ the third stage belongs to the selected process. The process has become the current process starting to run, depending on whether the process's Sswap flag is set, calling the Aretu process from U. Environment variables for the recovery process in U-ssav (R5 and R6).

When the program discovers that a process priority is higher than the priority of the current running process, it sets the flag, checks whether the flag is set before the interrupt falls into the handler, and, if set, calls the Swtch () program to dispatch, and sets the Runrun flag once every 1 seconds in the clock interrupt processing. And through the software interrupt mode, executes the swtch () procedure, this is in order to increase the scheduling opportunity, guarantees the good time-sharing. Swtch program calls Savu, Retu, Idle, aretu and other processes.

Savu program function: This is an assembly language process, it will R5 and R6 values into an array, the address of the array as a parameter passed to the Savu;

Retu Program function: assembly process, it resets the seventh kernel mentality segment address register, and then from the "U. U-rsa" The latest accessible copy reset R6 and R5;

Aretu program function: assembly language process, it from the address passed as a parameter to reload R5 and R6;

The role of the Idle program: assembly language Process, let the processor idling wait.

Swtch program powerful and simple and concise program code (a total of 71 lines of code, where the comment statement, such as 30 lines, the effective code is 41 lines), which is the entire UNIX system code prominent features.

UNIX systems are multi-user, multitasking operating systems that provide a high degree of program portability between UNIX implementations by providing machine-independent abstraction services to the process. The execution of the program is limited to the process that holds the current state of the program, including the virtual address space, the value of the program's variables, and the hardware status. The kernel provides an environment for each process to make the environment appear as if the process were the only process being executed in the system. This is achieved primarily by giving each process its own virtual address space. System calls can create a new process, change the program that the process is executing, and terminate the process, and can use many other system calls, including system calls that dynamically allocate uninitialized data.

Linux the process scheduling

The traditional UNIX operating system scheduling algorithm must achieve several conflicting objectives: The process response time as fast as possible, background job throughput as high as possible, the process of starvation as far as practicable, low-priority and high-priority processes need to reconcile and so on. Deciding when and how to choose a new process to run this set of rules is called a scheduling policy (scheduling).

Linux process scheduling is based on the time-sharing technology (time-sharing). Allowing multiple processes to run "concurrently" means that the CPU's time is roughly divided into slices, assigning one piece to each of the running processes.

Of course, a single processor can only run one process at any given time. A process switchover can occur when a concurrently executing process has not terminated when its time slice or timeframe (quantum) expires. Time-sharing relies on timed interrupts, so it is transparent to the process. To ensure CPU ticks, no additional code needs to be inserted into the program.

Scheduling policies are also based on processes that are queued according to priority. Sometimes a complex algorithm is used to find the current priority of the process, but the final result is the same: each process is associated with a value that indicates how the process is allocated appropriately to the CPU. In Linux, the priority of a process is dynamic. The scheduler tracks what the process does and periodically adjusts their priority. In this way, processes that do not use the CPU for a longer interval of time are promoted by dynamically increasing their priority. Accordingly, for processes that have been running on the CPU for a long time, they are punished by reducing their priority. Each process has a basic priority at the beginning of its creation, and the scheduling system dynamically adjusts its priority during execution, and the highly interactive task gets a high dynamic priority, while the low-level interactive task gets a low dynamic priority. The time slice of the class process is calculated as shown:


When it comes to scheduling issues, the process is traditionally categorized as "I/O range (i/o-bound)" or "CPU

Range (Cpu-bound) ". The former frequently uses I/O devices and spends a lot of time waiting for I/O operations to complete, while the latter is a numerical computing application that requires a lot of CPU time. The Linux operating system supports multiple processes, and the Process Control block PCB (Controlblock) is one of the most important data structures in the system. The various information necessary to store the process, the PCB is represented by the structure task-struct, including the type of process, process status, priority, clock information and so on. In a Linux system, the process dispatch operation is performed by the schedule () function, which is a function that runs only in the kernel state, and the function code is shared for all processes.

Linux timing of process scheduling

Linux process scheduling timing and modern operating systems in the scheduling time is basically the same, in order to determine whether the kernel process scheduler can be executed to schedule the process, Linux set the process scheduling flag need-resched, when the flag is 1 o'clock, executable scheduler. In general, Linux scheduling time is divided into the following two situations: (1) Active scheduling: The explicit invocation of the schedule () function explicitly frees the CPU, causing a new round of scheduling. Typically occurs in the current process state changes, such as: process termination, process sleep, process for some signal processing process medium. (2) Passive dispatch: Refers not to call the schedule () function, just the need_resched process dispatch flag in the PCB, the domain is set to 1 will cause the new process scheduling, and each time the interrupt processing and system calls return, the core scheduler will actively query need- The state of the resched (if set, the schedule () function is called actively. Typically occurs when a new process is generated, when a process priority changes, when a process waits for a resource to be awakened, when the current process time slice is exhausted, and so on.

Linux Process Scheduling Policy

In general, the scheduling strategy of different operating systems is different, the Linux process scheduling is the priority scheduling, time slice rotation scheduling, advanced first-out scheduling comprehensive application. Linux system. Different types of process scheduling policies are not the same.

1 . Data structures related to process scheduling

Each process is a dynamic entity whose life cycle is defined in sequence as: Task-running,task-interruptible. Task-uninterruptible,task-zombie and task-stopped, a process in which the state changes several times during its lifetime. corresponding to its data structure is the state of the Linux process, namely: running state, waiting state, suspended state and dead state.

2 . Description of process state and its conversion process

The process is created with a status of non-disruptive sleep, after the parent process wakes up before do-fork () ends. becomes the execution state, the executing process is moved to the Run-queue Ready task queue for scheduling. Appropriate time by the SCHEDULE0 by the scheduling algorithm selected, get the CPU, if the use of rotation, instant, by the clock interrupt trigger Timer-interrupt (), its internal call schedule (), causing a new round of scheduling, the current process state is still in the execution state, Thus the current process of the Sting Jruil-queue team tail. A process that obtains a CPU and is running if it does not request a resource. Call sleep-on () or interruptible-sleep-on () sleep, whose task-struct process control block hangs to the corresponding resource's wait-queue wait queue if sleep-on () is called. Then its state becomes non-disruptive to sleep, and if interruptible-sleep-on () is called, its state becomes interrupted sleep, sleep-on () or interruptible-sleep-on () will call schedule () function to release the sleep process.

3. Process Classification and the corresponding process scheduling policy


Linux system, in order to efficiently dispatch processes, the process is divided into two categories: real-time processes and ordinary processes (also known as non-real-time processes or general processes), real-time process priority is higher than other processes, if a real-time process is in the executable state, it will be executed first. The real-time process has two strategies: time-slice rotation and FIFO, in the time-slice rotation strategy. Each executable real-time process takes a time slice in turn, and the FIFO policy executes each process in the order in which it was run, and the order does not change.

In Linux, the process scheduling strategy has a total of 3 definitions:

Each process in a Linux system is described by the TASK,STRUCT structure, which is based on the policy, priority, counter, and RT-PRIORITY,PCB settings in the TASK-STRUCT structure, Its value is used to reflect the scheduling strategy used for different types of processes. SCHED-RR and Sched-fifo are used for real-time processes. respectively, the rotation scheduling strategy and the FIFO scheduling strategy are represented, and the sched-other represents a normal process and is also processed according to the rotation scheduling policy. These three types of scheduling strategies are based on priority. The priority data item is set in the PCB, and its value is the scheduling precedence of the normal process. The initial value of the available time slice of the normal process is that value, which can be changed by a system call. The Rt~p riority data item is set in the PCB, its value is the scheduling priority of the real-time process, and the initial value of the available time slice of the real-time process is the value. The priority can also be modified with the system, and the counter data item is set in the PCB. The count used for the time slice value of the process available, with an initial value of rt-priority or priority. The value is decremented at any time after the process is started.

Windows Process

Data structures related to process scheduling

Each Windows process is represented by an execution body process block. The eprocess block contains and points to many other related data structures in addition to many process-related properties. For example, each process has one or more threads, which are represented by the thread block of the execution body. The execution process block and the associated data structure are in system space, but the process block environment is an exception, in the process address space (because it contains some information that needs to be modified by user-mode code).

In addition to the eprocess block, the Windows subsystem process maintains a similar structure for each Windows process. Also, the kernel-mode portion of the Windows subsystem has a data structure for each process, which is created when a thread first invokes the Windows user or GDI function.

is a simplified block diagram of the process and thread data structures:


Windows analysis of the process scheduling method of the system

Every process in Windows is a eprocess struct. In addition to the process's properties, this struct references other structures that are closely related to the implementation process. For example, each process has one or several threads, and the thread is the Ethread struct in the system. Briefly describe the main information that exists in this structure, which is learned from the study of kernel functions. First, there are kprocess structures in the struct, which in turn have pointers to the kernel threads (kthread) lists that point to these processes (assigning address space), base priority, time in kernel mode or thread of user-mode execution process, processor Affini Ty (Mask, Defines which processor can execute the thread of the process), the time slice value. This information is also present in the ETHREAD structure: The process ID, the parent process ID, and the process image name.

There is also a pointer to PEB in the eprocess structure. The Ethread structure also contains the creation time and exit time, the process ID and pointers to eprocess, the start address, the I/O request list, and the kthread structure. The following information is included in Kthread: the creation time of kernel mode and user-mode threads, pointers to kernel stack base addresses and vertices, pointers to service tables, base priority to current priority, pointers to APC, and pointers to T E B. Kthread contains many other data that can be used to analyze the structure of kthread by observing the data. By traversing the ethread in the kprocess structure, all the current kthread structures in the system are found, and the offset in this structure is the affinity domain (WINDOWSXPSP3) at 0x124, which is the memory address where the CPU affinity mask is set.

Windows implements a preemptive-driven scheduling system-a running thread with the highest priority always running, which may be limited to running on the processor that allows it to run, a phenomenon known as processor affinity, which, by default, can run on any idle processor, However, you can change processor affinity by using the Windows dispatch function, or by setting an affinity mask on the image header.

In this context, the concept of CPU affinity is explained, and CPU affinity refers to the ability to bind one or more processes or threads to one or more processors on a system, which is a long-awaited feature. This means ": Run the program on processor 1th" or "Run these programs on all processors instead of on the No. 0 processor." The scheduler then follows this rule, and the program only runs on the allowed processors.

The process dispatch code for Windows is under its system process, so it does not belong to any user process context. The dispatch code switches the process context at the appropriate time, where the switching process context refers to the switching of the process environment, including the executable program in memory, and the various resources that the provider runs. The process has a virtual address space, executable code, data, object handle set, environment variable, base priority, and the maximum minimum working set, and so on.

The smallest scheduling unit in Windows is a thread, and only the thread is the real execution, and the process is just the container for the thread. When the Windows callback expires, or has a switch thread instruction execution (such as Sleep,kewaitforsingleobject), it will find the next thread-executing body to be dispatched from the process thread queue and mount to KPCR (Kernel ' s Processor control region, the kernel Process Control area) structure, the CPU executes the thread execution body code according to the KPRCB structure in the KPCR structure.

In the multi-core CPU, when the Windows dispatch code executes, the affinity is removed from the kthread structure that is currently scheduled for execution, and the CPU mask in the hardware configuration data on the current PC is manipulated, and the result is written to the specified CPU, such as a dual-core CPU The device mask is 0x03, if the current kthread in the affinity is 0x01, then 0x01&0x03=0x01, so that the execution of the body thread will be loaded into CPU1 KPRCB structure can be executed, the dispatcher will not give this thread to CPU2 to execute. This procedure is shown in 2. This is the principle of specifying the CPU core for the thread selection.

Summary : The selection of process scheduling strategy has a vital impact on the performance of the whole system, a good scheduling algorithm should consider many aspects: fairness, effectiveness, response time, turnaround time, system throughput and so on. But these factors are contradictory to each other, the final choice depends on the system to achieve the goal, at the same time we can also see that the management of multi-process is a very complex concurrent programming. The state of each process is not only determined by itself, but also influenced by many external factors. On the basis of the process scheduling, in order to ensure the stability of the operating system, improve efficiency and increase flexibility, there must be many methods, which are worthy of our research and discussion:.

Analysis of process scheduling for Linux,windows and Unix

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.