Analysis and program design based on the object-oriented operating system development platform (OSKit) (4)

Source: Internet
Author: User
Article title: analysis and program design based on the object-oriented operating system development platform (OSKit) (4 ). Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Author: Tang Haijing
  
Thread scheduling in thread analysis
  
This article is the fourth part of "analysis and program design based on the object-oriented operating system development platform (OSKIT)". The author will analyze thread scheduling.
Thread scheduling is one of the main contents in the kernel of the operating system. it is crucial to the execution efficiency of the entire operating system. OSKit also includes this content, and because the thread scheduling is more frequent, this part of OSKit is much more important than it is in UNIX.
  
4.1 thread scheduling algorithm analysis
  
4.1.1 General description of the thread scheduling algorithm
In a time-sharing system, the kernel allocates a CPU time for each thread. this time is called a time slice. when this time passes, the kernel will schedule another thread to change it to the execution state. This is the so-called time slice rotation method.
  
Similar to thread scheduling in UNIX, the OSKit scheduler also uses an algorithm called multi-level feedback cyclic scheduling, this algorithm is one of the most commonly used scheduling programs in the operating system. The core idea is: the kernel divides the thread into a time slice and feeds the thread back to one of several priority queues. A thread may need to pass multiple "feedback loops" multiple times before it ends ". When the kernel performs context switching and recovery, the scheduler must ensure that the thread continues to run from where it was suspended. Otherwise, this scheduling algorithm cannot be used.
  
Therefore, I think it is necessary to reiterate the context switching and restoration of the thread. First, the thread context is composed of the content of the user address space, the content of the hardware register, and the kernel data structure related to the thread. Strictly speaking, the thread context is composed of its user-level context, register context, and system-level context.
  
4.1.2 priority reversal method
OSKit adopts an excellent priority reversal algorithm in the operating system design. that is, when a thread with a lower priority occupies the mutex, another thread with a higher priority applies for the mutex, a thread with a higher priority is blocked by a thread with a lower priority.
  
In this case, the priority reversal algorithm is introduced. that is to say, when the preceding situation occurs, the thread scheduler will increase the priority of the thread with lower priority to the same as that of the thread with higher priority, this allows the thread occupying the mutex to execute quickly.
  
If there are more than two threads to be scheduled, the algorithm allows recursion. that is to say, the execution of the thread with lower priority needs another mutex, this mutex is occupied by another thread with lower priority. Therefore, the scheduler increases the priority of the thread with lower priority in turn, which is a recursive increase.
  
However, a deadlock may occur if we support recursive priority escalation. Therefore, we must introduce a deadlock check program, this greatly increases the core size and overhead of the operating system, but makes the operating system more complete and can process real-time threads.
  
4.2 pthread/pthread_scheduler.c
This source code file includes a complete thread scheduling mechanism. all OSKit thread scheduling algorithms are concentrated in this file. by reading the following function analysis, it will help you understand what functions OSKit uses to implement thread scheduling and give you a deeper understanding of the concepts described above.
  
4.2.1 clear the thread queue waiting for scheduling
Note: In order to let everyone really understand the role of this function, I would like to explain what is a thread waiting queue first. Because the scheduler also appears as a thread in the operating system and can only schedule one thread at a time, many threads may wait for scheduling but cannot accept scheduling, the function provided by OSKit makes up for this defect. it links all the threads waiting for scheduling in the form of a linked list. this is called the thread queue waiting for scheduling. Obviously, clearing it is actually a pointer to release the linked list.
Static inline int posix_runq_empty (void)
  
4.2.2 obtain the priority of the thread with the highest priority in the waiting queue
Note: When the scheduling thread completes the work at hand and needs to schedule the next thread, it will face a problem: which thread should be scheduled for scheduling? This function provided by OSKit can return the thread with the highest priority in the waiting queue for scheduling by the scheduling thread.
Static inline int posix_runq_maxprio (void) return PRIORITY_MAX-(prio-1 );
  
4.2.3 get the pointer pointing to the next thread to be executed
Note: when a thread is in the execution state, the scheduler does not know where the thread is suspended, we can call this function provided by OSKit to find the pointer to the thread to be executed.
Static inline int posix_runq_onrunq (pthread_thread_t * pthread)
  
4.2.4 append a thread to the end of the thread execution queue
Note: add the thread with the lowest priority or the thread with the last execution to the end of the thread execution queue for other reasons.
Static inline void posix_runq_insert_tail (pthread_thread_t * pthread)
  
4.2.5 append a thread to the queue header of the thread's execution queue
Note: add the thread with the highest priority or the thread that needs to be executed to the beginning of the thread execution queue for some reason.
Static inline void posix_runq_insert_head (pthread_thread_t * pthread)
  
4.2.6 wait for the thread with the highest priority in the queue to submit the queue for execution
Note: When the operating system needs to wait for a thread in the thread execution queue, we should naturally start from scratch and call this function to implement the function of executing the thread.
Static inline pthread_thread_t * posix_runq_dequeue (void)
Note: The pointer pointing to the next waiting thread is returned.
  
4.2.7 delete a dedicated resource thread from the waiting queue
Note: to enable the operating system to run under the optimal conditions, OSKit prohibits a thread from occupying system resources for a long time. if such time is found, you can call this function to delete the threads with exclusive resources from the execution queue to ensure fair and reasonable thread scheduling.
Static inline void posix_runq_remove (pthread_thread_t * pthread)
  
4.2.8 scheduling algorithm used at this time
Note: As mentioned in the previous chapter, the scheduling algorithm of a thread is assigned to it during initialization, but it can be changed, OSKit calls the following function to change the scheduling algorithm after the thread is created.
  
Int posix_sched_schedules (int policy)
If (policy = SCHED_RR | policy = SCHED_FIFO)
Return 1;
Return 0;
  
  
  
  
  
4.2.9 change a thread to a running state
Note: As we all know, threads generally have three states in the system. OSKit calls this function to implement the function of converting threads to execution states.
Int posix_sched_setrunnable (pthread_thread_t * pthread)
  
4.2.10 terminate the current scheduling algorithm of the thread
Note: This is a function call that changes the scheduling algorithm after a thread is created. its function is to stop the current scheduling algorithm of the thread.
  
Void posix_sched_disassociate (pthread_thread_t * pthread)
{
If (posix_runq_onrunq (pthread )){
/** On the scheduler queue, so its not running .*/
Posix_runq_remove (pthread );
}
}
  
  
  
  
  
4.2.11 create scheduling parameters for a new thread
Note: the so-called scheduling parameters are the basis for the scheduler to determine whether to execute a thread. For example, statistics on the recent CPU usage of a user-state thread.
  
Void posix_sched_init_schedstate (pthread_thread_t * pthread, const struct
Sched_param * param)
  
  
  
  
  
4.2.12 changing the thread scheduling status
Note: if the scheduling parameter meets the scheduling conditions, the scheduler will call the OSKit function to change the scheduling status of the thread and convert it to the preparation scheduling state. The thread is locked and interrupted.
  
Int posix_sched_change_state (pthread_thread_t * pthread, const struct
Sched_param * param)
  
  
  
  
  
4.2.13 priority migration
Note: This is the specific implementation of the priority reversal method I mentioned above. OSKit calls it to dynamically change the thread priority.
Int posix_sched_priority_bump (pthread_thread_t * pthread, int newprio)
  
  
Note: this is only temporary and only applicable to real-time threads.
4.2.14 for some reason, the thread is sent back to the waiting queue
Note: when the thread is executed in the operating system, it is entirely possible for the user to cause the location to change when the thread returns the execution queue after it executes its time slice. Below are my annotations on the reasons for changes provided by OSKit.
  
Int posix_sched_dispatch (resched_flags_t reason, pthread_thread_t
* Pthread)
Switch (reason ){
Case RESCHED_USERYIELD: posix_runq_insert_tail (pthread );
/* If the thread is idle by the user, insert it to the end of the team */
Case RESCHED_YIELD: posix_runq_insert_head (pthread );
/* If the thread is idle by the user, insert it to the end of the team */
Case RESCHED_PREEMPT:
/* Analyze the thread based on its priority and scheduling algorithm */
If (pthread-> policy = SCHED_RR ){
If (-- pthread-> ticks = 0 ){
Posix_runq_insert_tail (pthread );
/* If the time slice forwarding scheduling only has one ticks, insert it to the end of the team */
Pthread-> ticks = SCHED_RR_INTERVAL;
}
Else
Posix_runq_insert_head (pthread );
/* Although it is a time slice rotation method, if there are more than one ticks, insert the opposite */
}
Else if (pthread-> policy = SCHED_FIFO)
Posix_runq_insert_head (pthread );
/* If it is a first-come first-served method, insert the opposite */
Case RESCHED_INTERNAL:/* Change the thread to idle state */
Default:
If (pthr
Related Article

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.