Windows CPU scheduling

Source: Internet
Author: User

At the beginning of this semester, the instructor asked me to prepare a discussion class about CPU scheduling, so I prepared early and waited for the class to arrive, every day, I think of a little nervous. This is especially true today, because I am going to speak on stage in the afternoon. However, when I stepped onto the podium, I felt so calm that the lecture was still smooth and there was no truck. But then I found that I didn't use Mike, I am still a little nervous ^-^.

The main content of this preparation is as follows:

 

Windows CPU in the operating system SchedulingAgenda • Overview of Windows scheduling • priority levels • thread States • dispatcher database • scheduling scenariosoverview • Priority-driven preemptible scheduling method is used in windows. In the ready queue, threads with the highest priority are always in the running state. When a thread is scheduled for execution, it obtains a time (quantum ). The amount of time varies with the system and process: different system settings (short or long quantum), foreground or background processes, or using the job object in the kernel). A thread may be preemptible by a high-priority thread at any time during its execution time. • The Scheduling Algorithm in Windows is implemented in the kernel, but there is no independent "scheduling" module or program, because the code in the kernel is distributed in scheduling-related event processing. All these scheduling processes are collectively called Kernel scheduling programs (dispatcher ). • Scheduling takes place at the following time:-A thread becomes ready (ready);-A thread leaves running state (running);-the priority of a thread changes; • During each scheduling, windows must determine the next thread to run. When Windows selects a thread for execution, context switch is required first ). • Note: Scheduling in Windows is based on threads, which means that the process does not run. It only provides resources and Context Environment for running the threads it contains. Therefore, we do not consider the specific process that the thread belongs to during scheduling. For example, if process a has 10 execution threads and process B has 2 execution threads, and these 12 threads have the same priority, these 12 threads will each occupy 1/12 of the CPU time, instead of 50% of the CPU time used by process a and process B. priority • 32-level priority in the Windows Kernel, from 0 to 31, as shown in: • the priority given to users in Windows APIS is only 6 levels: -Real-Time-High-above normal-below normal-idle • each process has only one basic priority, and each thread has two priorities: Base) and current priority (current ). • Scheduling is based on the current priority of the thread. • For a variable level (variable levels, 0 ~ 15). Windows will increase the priority of a thread in a specific situation ~ 31. • By default, the initial priority of a thread is inherited from the process to which it belongs, and the priority of a process is inherited from the process that created it. • Of course, you can also change the initial priority of processes and threads through functions or commands, and change the priority after creation. • Changing the priority of a process will change the priority of the threads it contains, but the relative priority between threads will not change. • When you change the priority of a thread in a process, do not change it unless the program is written by you or you are very clear about what each thread in the program does and the importance of each other, because changing the relative priority between them may lead to Abnormal Running of the program. • Generally, the basic priority of a process (that is, the basic priority during thread creation) is set to the intermediate value (24, 13, 10, 8, 6 or 4) of the priority range by default ). • The basic priority of some windows system processes is higher than the default priority, such as session manager(smss.exe) and local security authentication server(lsass.exe. • For a thread with a variable priority at the end of its time slice, the Windows scheduler will lower its priority but will not lower it to the basic priority of the thread. To reduce the thread priority, you can limit the CPU-bound thread's long CPU usage. • When the operation of a variable-priority thread in the waiting state is completed, the Windows scheduler increases the priority of the thread. The degree of improvement depends on the operations that the thread is waiting for. For example, the improvement of a thread waiting for keyboard input is much higher than that of a thread waiting for disk operation, in this way, the response time of the Interaction thread is shorter, the I/O-bound thread is kept busy, and the CPU-bound thread is used in the background. • In Windows 2000 and Windows XP, the thread state is converted as follows: thread state • ready thread waiting for execution. The scheduler selects the next thread to be executed from the ready thread. • Standby: The next thread to be executed. Each processing machine in the system can have only one thread in the STANDBY state. Of course, the Standby thread may also be preemptible before execution. • The Running thread. • The waiting thread waits for the synchronous execution of other objects and waits for I/O operations... • The transition thread is ready for execution, but its core stack is removed from the memory. Once its core stack is moved into the memory, it will become ready. • Terminated thread running ends • The initialized thread is created with the dispatcher database • To implement thread scheduling, the Windows Kernel manages a set of data structures called the dispatcher database. The scheduling database records which threads are waiting for execution and which threads are running on which processing machine. • In windows, a ready queue is maintained for each priority. • A 32-bit mask (a 32-bit mask) is maintained for Windows to accelerate thread selection ). The position (1) of each digit indicates that at least one waiting thread exists in the ready queue of this priority. Dispatch • in windows, which thread will obtain the CPU fully based on the thread priority, and how is it actually implemented? How is priority-driven preemptible multitasking implemented at the Thread level? • Voluntary switch • preemption • quantum end • terminationvoluntary switch • a thread takes the initiative to give up CPU usage right by waiting for some resources. For example, wait for the occurrence of an event, mutex, semaphore, and I/O operations, and wait for a process, thread, and Windows message. This shows that a thread enters the waiting state and Windows selects a new thread for execution: preemption • after a high-priority thread enters the ready state, it will seize the low-priority threads being executed. • There are two reasons for this situation:-The operation waiting for a high-priority thread has been completed;-the priority of a thread has been increased; • In any of the above cases, windows must decide whether to continue running the current thread or be preemptible. • User-state threads can also seize the core State threads. That is, the thread Execution Mode does not affect scheduling. Only the priority of the thread is the deciding factor. • When a thread is preemptible, it is placed in the header of its priority ready queue, as shown in: • quantum end • after a thread uses the CPU time allocated for it, windows determines whether to lower its priority and whether to schedule a new thread to be executed on the processing machine. • If the priority of a thread is reduced, Windows needs to find a more appropriate thread for execution. • If the thread's priority is not decreased, but there are other threads in the ready queue with the same priority, Windows selects the first thread in the queue for execution, put the thread at the end of the queue, as shown in ). If there is no ready thread with the same priority, the thread will get a new time slice. Termination • when the execution of a thread ends, it enters the final state from the execution state, and the thread will also be deleted from the thread list of the process to which it belongs, the related data structure will also be released, and the scheduler will select another thread from the ready queue for execution. • If no execution thread is available on the CPU at this time, the scheduler will schedule idle thread execution. In Windows, we can see that the priority of the idle thread is 0. In fact, the idle thread has no priority because it is scheduled to run only when there are no other threads in the system. In addition, note that each Windows system has only one thread with a zero priority -- the zero page thread.

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.