4.3 Thread Scheduling
When a process is scheduled to be executed, it is the thread that utilizes the processor. If the process has only one thread, it is the primary thread assigned to a processor. If a process has multiple threads and there are multiple processors, all of the threads are assigned to a processor. Threads compete for processor usage either with all the threads from active processes in the system or just the threads from a single process. The threads are placed in the ready queues sorted by their priority value. The threads in the queue with the same priority are scheduled to processors according to a scheduling policy. When there are not enough processors to go around, then a thread with a higher priority can preempt an executing thread. If the newly active thread is of the same process as the preempted thread, then the context switch is between threads. If the newly active thread is of another process, a process context switch occurs and then the thread context switch is performed.
Table 4-3. Settable Properties for the Thread Attribute Object
Settable Thread Attributes |
Functions |
Description |
---|---|---|
detachstate |
int pthread_attr_setdetachstate (pthread_attr_t *attr,int detachstate); |
The detachstate attribute controls whether the newly created thread is detachable. If detached, the thread's flow of control cannot be joined to any thread. |
guardsize |
int pthread_attr_setguardsize(pthread_attr_t *attr,size_t guardsize); |
The guardsize attribute controls the size of the guard area for the newly created thread's stack. It creates a buffer zone the size of guardsize at the overflow end of the stack. |
inheritsched |
int pthread_attr_setinheritsched(pthread_attr_t *attr,int inheritsched); |
The inheritsched attribute determines how the scheduling attributes of the newly created thread will be set. It determines whether the new thread's scheduling attributes are inherited from the creating thread or set by an attribute object. |
param |
int pthread_attr_setschedparam(pthread_attr_t *restrict attr,const struct sched_param *restrict param); |
The param attribute is a structure that can be used to set the priority of the newly created thread. |
schedpolicy |
int pthread_attr_setschedpolicy(pthread_attr_t *attr,int policy); |
The schedpolicy determines the scheduling policy of the newly created thread. |
contentionscope |
int pthread_attr_setscope(pthread_attr_t *attr,int contentionscope); |
The contentionscope attribute determines which set of threads the newly created thread will compete with for processor usage. A process scope means the thread will compete with the set of threads of the same process; system scope means the thread will compete with system-wide threads (this includes threads from other processes). |
stackaddr stacksize |
int pthread_attr_setstack(pthread_attr_t *attr,void *stackaddr,size_t stacksize); |
The stackaddr and stacksize attributes determine the base address and minimum size in bytes of the stack for the newly created thread. |
stackaddr |
int pthread_attr_setstackaddr(pthread_attr_t *attr,void *stackaddr); |
The stackaddr attribute determines the base address of the stack for the newly created thread. |
stacksize |
int pthread_attr_setstacksize(pthread_attr_t *attr,size_t stacksize); |
The stacksize attribute determines the minimum size in bytes of the stack for the newly created thread. |
4.3.1 Thread States
Threads have the same states and transitions mentioned in Chapter 3 that processes have. Figure 4-4 is a duplication of the state diagram 3.4 from Chapter 3. To review, there are four commonly implemented states: runnable, running (active), stopped, and sleeping (blocked). A thread state is the mode or condition a thread is in at any given time. A thread is in a runnable state when it is ready for execution. All runnable threads are placed in a ready queue with other threads with the same priority that are ready to be executed. When a thread is selected and assigned to a processor, the thread is in the running state. A thread is preempted once it has executed for its time slice or when a thread of higher priority becomes runnable. The thread is then placed back into the ready queue. A thread is in the sleeping state if it is waiting for an event to occur or I/O request to complete. A thread is stopped when it receives a signal to stop executing. It remains in that state until it receives a signal to continue. Once the signal is received, the thread moves from the stopped to a runnable state. As the thread moves from one state to another, it undergoes a state transition that signals some event has occurred. When a thread changes from the runnable to the running state it is because the system has selected that thread to runthe thread has been dispatched. A thread is preempted if its makes an I/O request or some other request of the kernel or for some external reason.
Figure 4-4. Thread states and transitions.
One thread can determine the state of an entire process. The state of a process with one thread is synonymous with the state of its primary thread. If the primary thread is sleeping, the process is sleeping. If the primary thread is running, the process is running. For a process that has multiple threads, all threads of the process would have to be in a sleeping or stopped state in order to consider the whole process sleeping or stopped. On the other hand, if one thread is active (runnable or running) then the process is considered active.
4.3.2 Scheduling and Thread Contention Scope
The contention scope of the thread determines which set of threads a thread will compete with for processor usage. If a thread has process scope, it will only compete with the threads of the same process for processor usage. If the thread has system scope, it will compete with its peers and with threads of other processes for processor usage. For example, in Figure 4-5, there are two processes in a multiprocessor environment of three processors. Process A has four threads and Process B has three threads. Process A has three threads that have process scope and one thread with system scope. Process B has two threads with process scope and one thread with system scope. Process A's threads with process scope competes for processor A and Process B's threads with process scope compete for processor C. Process A and B's threads with system scope compete for processor B.
Figure 4-5. Scheduling with process and system scope threads in a multiprocessor environment.
Threads should have system scope when modeling true real-time behavior in your application.
4.3.3 Scheduling Policy and Priority
The scheduling policy and priority of the process belong to the primary thread. Each thread can have its own scheduling policy and priority separate from the primary thread. Threads have an integer priority value that has a maximum and minimum value. A priority scheme is used to determine which thread is assigned the processor. Each thread has a priority and the thread with the highest priority is executed before the threads of lower priority. When threads are prioritized, tasks that require immediate execution or response from the system are allotted the processor time it requires. In a preemptive operating system, executing threads are preempted if a thread of higher priority and the same contention scope is available. For example, in Figure 4-5, threads with process scope compete for the processor with threads of the same process that also have process scope. Process A has two threads with priority 3 in which one is assigned the processor. Once the thread with priority 2 becomes runnable, the active thread is preempted and the processor is given to the thread with higher priority. Yet, in Process B, it has two process scope threads that have priority 1, a higher priority than 2. One thread is assigned the processor. Although the other thread with priority 1 is runnable, it does not preempt Process A's thread with priority 2. The thread with system scope and a lower priority is not preempted by any of the threads of Process A or B. They only compete for processor usage with other threads that have system scope.
As discussed in Chapter 3, the ready queues are organized as a sorted list in which each element is a priority level. Each priority level in the list is a queue of threads with the same priority level. All threads of the same priority level are assigned to the processor using a scheduling policy: FIFO, round-robin, or other. With the FIFO (First-In, First-Out) scheduling policy, when the time slice expires the thread is placed at the head of the queue of its priority level. Therefore, the thread will run until it completes execution, it sleeps, or it receives a signal to stop. When a sleeping thread is awakened, it is placed at the end of the queue of its priority level. Round-robin scheduling is similar to FIFO scheduling except the thread is placed at the end of the queue when the time slice expires and the processor is given to the next thread in the queue.
The round-robin scheduling policy considers all threads to be of equal priority and each thread is given the processor for only a time slice. Task executions are interweaved. For example, a program that searches files for specified keywords is divided into two threads. One thread, thread 1, searches for all files with a specified file extension and places the path of the file into a container. Another thread, thread 2, extracts the name of the files from the container, searches each file for the specified keywords, then writes the name of the files that contains all the keywords to a file. If the threads used a round-robin scheduling policy with a single processor, thread 1 would use its time slice to find files and insert the paths into the container. Thread 2 would use its time slice to extract file names and then perform the keyword search. In a perfect world, this interweaves the execution of threads 1 and 2. But thread 2 may execute first when there are no files in the container or thread 1 may only get as far as finding a file, the time slice expiring before it had a chance to insert the file name into the container. This situation requires synchronization, which will be discussed briefly later in this chapter and in Chapter 5. The FIFO scheduling policy allows each thread to run until execution is complete. Using the same example, thread 1 would have time to locate all the files and insert the paths into the container. Thread 2 would then extract the filenames and perform its keyword search on each file. In a perfect world, this would be the end of the program. But thread 2 may be assigned to a processor before thread 1 and there would be no files in the container to search. Thread 1 would then execute, locate, and insert file names into the container but no keyword search would be performed. The program would fail. With FIFO scheduling, there is no interweaving of the execution of these tasks. A thread assigned to a processor dominates the processor until it completes execution. This scheduling policy can be used for applications where a set of threads need to complete as soon as possible. The "other" scheduling policy can be user-defined customization of a scheduling policy. For example, the FIFO scheduling policy can be customized to allow random unblocking of threads.
4.3.3.1 Changing Thread Priority
The priorities of threads should be changed in order to speed up the execution of threads on which other threads depend. They should not be changed in order for a specific thread to get more processor time. This will affect the overall performance of the system. High-priority class threads receive more processor time than threads of a lower class because they are executed more frequently. Threads of higher priority will dominate the processor, preventing other threads of lower priority valuable processor time. This is called starvation. Systems that use dynamic priority mechanisms respond to this situation by assigning priorities that last for short periods of time. The system adjusts the priority of threads in order for threads of lower priority execution time. This will improve the overall performance of the system.
The temptation to ensure that a process or specific thread runs to completion is to give it the highest priority but this will affect the overall performance of the system. Such threads may preempt communications over networks, causing the loss of data. Threads that control the user interface may be drastically affected, causing the keyboard, mouse, and screen response to be sluggish. Some systems prevent user processes and threads from having a higher priority than system processes. Otherwise, system processes or threads would be prevented from responding to critical system changes. Generally speaking, most user processes and threads fall in the category of normal or regular priority.