📝 Linux Scheduling (75 MCQs)
📖 From Operating System • 6. CPU Scheduling • 75 questions available
What is Linux Scheduling?
Definition:
Linux uses the Completely Fair Scheduler (CFS) implementing proportional share via virtual runtime , maintaining red-black trees sorted by to select the leftmost node with O(log n) complexity.
Example:
A nice=-5 process accumulates vruntime slower than a nice=10 process, receiving more CPU shares while still guaranteeing eventual execution for all runnable tasks.
Reason:
CFS replaces complex priority queues with a mathematically fair model that naturally handles varying loads without tuning, providing good interactivity and throughput for general-purpose desktop/server workloads.
📝 All Linux Scheduling MCQs
Q1. Which scheduler became the default Linux scheduling algorithm in release 2.6.23 of the kernel?
📖 Explanation: The Completely Fair Scheduler (CFS) became the default Linux scheduler in kernel version 2.6.23. It replaced the O(1) scheduler, which had been introduced in Version 2.5 to address the limitations of the traditional UNIX scheduler for SMP systems.
Q2. What was the primary limitation of the traditional UNIX scheduler that led to its replacement in Linux Version 2.5?
📖 Explanation: The traditional UNIX scheduling algorithm was not designed for SMP systems, so it did not adequately support multiprocessor systems. This limitation, along with poor performance with a large number of runnable processes, led to the development of the O(1) scheduler.
Q3. What does the O(1) designation mean for the Linux scheduler introduced in Version 2.5?
📖 Explanation: The O(1) designation indicates that the scheduler runs in constant time (O(1) complexity). This means the time to select the next task to run does not depend on the number of tasks in the system, providing scalability for systems with many runnable processes.
Q4. Which scheduler provided increased support for SMP systems including processor affinity and load balancing?
📖 Explanation: The O(1) scheduler, introduced in Version 2.5, provided increased support for SMP systems. It included features like processor affinity (keeping processes on the same processor) and load balancing between processors, which were essential for efficient multiprocessor operation.
Q5. What was the main drawback of the O(1) scheduler in practice?
📖 Explanation: Although the O(1) scheduler delivered excellent performance on SMP systems, it led to poor response times for interactive processes common on desktop systems. This drawback was a key factor in the development of the Completely Fair Scheduler (CFS).
Q6. On what principle is the Linux scheduling system based?
📖 Explanation: Linux scheduling is based on scheduling classes. Each class is assigned a specific priority, and the kernel can accommodate different scheduling algorithms based on the needs of the system and its processes by using different scheduling classes.
Q7. How many scheduling classes do standard Linux kernels implement?
📖 Explanation: Standard Linux kernels implement two scheduling classes: (1) a default scheduling class using the CFS scheduling algorithm and (2) a real-time scheduling class. New scheduling classes can be added as needed.
Q8. How does the CFS scheduler determine the proportion of CPU processing time for each task?
📖 Explanation: CFS assigns a proportion of CPU processing time to each task based on its nice value. The nice value ranges from −20 to +19, with lower nice values indicating higher relative priority and thus a higher proportion of CPU time.
Q9. What is the range of nice values in the Linux CFS scheduler?
📖 Explanation: Nice values range from −20 to +19 in the Linux CFS scheduler. A numerically lower nice value indicates a higher relative priority. The default nice value is 0. This range provides 40 distinct priority levels for normal (non-real-time) tasks.
Q10. What is the default nice value for a task in the Linux CFS scheduler?
📖 Explanation: The default nice value for a task is 0. A task can increase its nice value (e.g., from 0 to +10) to be 'nice' to other tasks by lowering its relative priority. The default value represents a neutral priority level.
Q11. What is targeted latency in the CFS scheduler?
📖 Explanation: Targeted latency is an interval of time during which every runnable task should run at least once. CFS allocates proportions of CPU time from the value of targeted latency, ensuring fairness by giving each task a chance to run within this interval.
Q12. What is the purpose of the vruntime variable in the CFS scheduler?
📖 Explanation: The vruntime variable records how long each task has run. It is associated with a decay factor based on the task's priority: lower-priority tasks have higher rates of decay, meaning their vruntime increases faster than higher-priority tasks for the same actual run time.
Q13. How does the CFS scheduler decide which task to run next?
📖 Explanation: CFS selects the task with the smallest vruntime value. This ensures that tasks that have received less CPU time (lower vruntime) are given priority, providing fairness. This is a key aspect of the CFS algorithm's goal of providing 'completely fair' scheduling.
Q14. For a task with a default priority (nice value 0), what is the relationship between vruntime and actual physical run time?
📖 Explanation: For tasks with normal priority (nice value 0), virtual run time is identical to actual physical run time. This means that if such a task runs for 200 milliseconds, its vruntime will also be 200 milliseconds. This provides a baseline for comparison with tasks of other priorities.
Q15. If a lower-priority task runs for 200 milliseconds, how will its vruntime compare to a normal priority task that also ran for 200 milliseconds?
📖 Explanation: If a lower-priority task runs for 200 milliseconds, its vruntime will be higher than 200 milliseconds. This is because lower-priority tasks have higher rates of decay, meaning their vruntime increases faster than higher-priority tasks for the same amount of actual CPU time.
Q16. If a higher-priority task runs for 200 milliseconds, how will its vruntime compare to a normal priority task that also ran for 200 milliseconds?
📖 Explanation: If a higher-priority task runs for 200 milliseconds, its vruntime will be less than 200 milliseconds. Higher-priority tasks have lower rates of decay, meaning their vruntime increases more slowly than lower-priority tasks for the same amount of actual CPU time.
Q17. What data structure does the CFS scheduler use to store runnable tasks?
📖 Explanation: CFS places each runnable task in a red-black tree, which is a balanced binary search tree. The key for this tree is based on the value of vruntime. This data structure allows efficient retrieval of the task with the smallest vruntime value.
Q18. In the CFS red-black tree, what does the leftmost node represent?
📖 Explanation: In the CFS red-black tree, the leftmost node has the smallest key value. Since the key is based on vruntime, the leftmost node represents the task with the smallest vruntime, which means it has received the least CPU time and thus has the highest priority for being selected next.
Q19. How does the Linux scheduler optimize the selection of the next task from the red-black tree?
📖 Explanation: The Linux scheduler caches the leftmost node in the variable rb_leftmost. This optimization means that determining which task to run next requires only retrieving the cached value, rather than performing an O(lg N) tree traversal. This makes the selection process extremely efficient.
Q20. What is the time complexity of navigating to the leftmost node in a balanced red-black tree?
📖 Explanation: Navigating a balanced red-black tree to discover the leftmost node requires O(lg N) operations, where N is the number of nodes in the tree. However, Linux optimizes this by caching the leftmost node, making the effective selection O(1).
Q21. What happens to a task in the CFS red-black tree when it becomes blocked (e.g., waiting for I/O)?
📖 Explanation: When a task becomes not runnable (for example, if it is blocked while waiting for I/O), it is removed from the red-black tree. Only runnable tasks are stored in the tree. When the task becomes runnable again, it is reinserted into the tree.
Q22. In the CFS scheduler, how do I/O-bound tasks typically compare to CPU-bound tasks in terms of vruntime?
📖 Explanation: I/O-bound tasks typically run for short periods and then block for I/O. As a result, they accumulate less actual CPU time than CPU-bound tasks. Therefore, their vruntime tends to be lower, giving them higher priority. This allows I/O-bound tasks to preempt CPU-bound tasks when they become ready.
Q23. In the CFS scheduler, what happens when an I/O-bound task becomes eligible to run while a CPU-bound task is executing?
📖 Explanation: When an I/O-bound task becomes eligible to run (for example, when I/O it was waiting for becomes available), it typically has a lower vruntime than the CPU-bound task. Therefore, it will have higher priority and will preempt the CPU-bound task. This ensures good responsiveness for interactive and I/O-intensive workloads.
Q24. What real-time scheduling policies does Linux support based on the POSIX standard?
📖 Explanation: Linux implements real-time scheduling using the POSIX standard, supporting SCHED_FIFO (first-in, first-out) and SCHED_RR (round-robin) real-time policies. These real-time policies run at a higher priority than normal (non-real-time) tasks.
Q25. What is the priority range for real-time tasks in Linux?
📖 Explanation: Real-time tasks in Linux are assigned static priorities within the range of 0 to 99. In this range, numerically lower values indicate higher relative priorities. These priorities are higher than those assigned to normal (non-real-time) tasks.
Q26. What is the priority range for normal (non-real-time) tasks in Linux?
📖 Explanation: Normal (non-real-time) tasks in Linux are assigned priorities from 100 to 139. This range is separate from the real-time priority range (0-99). Within this range, numerically lower values indicate higher relative priorities, with 100 being the highest normal priority.
Q27. How do nice values map to normal task priorities in Linux?
📖 Explanation: In Linux, nice values map to normal task priorities as follows: a nice value of −20 (the highest priority) maps to priority 100, and a nice value of +19 (the lowest priority) maps to priority 139. This provides a direct mapping between the nice value and the scheduler priority.
Q28. In the global Linux priority scheme, how do real-time task priorities compare to normal task priorities?
📖 Explanation: In the global priority scheme, real-time tasks (priorities 0-99) run at a higher priority than normal tasks (priorities 100-139). This ensures that real-time tasks receive preferential treatment and can preempt normal tasks when needed.
Q29. What is the range of priorities for real-time tasks in Linux?
📖 Explanation: Real-time tasks in Linux are assigned static priorities within the range of 0 to 99. This range provides 100 distinct priority levels for real-time tasks, allowing fine-grained control over the priority of time-critical applications.
Q30. What is the relationship between nice values and task priorities in Linux?
📖 Explanation: In Linux, numerically lower nice values indicate higher relative priorities. For example, a task with a nice value of −20 has the highest priority among normal tasks, while a task with a nice value of +19 has the lowest priority. This is why increasing the nice value is described as being 'nice' to other tasks.
Q31. What is the term 'nice' derived from in the context of Linux scheduling?
📖 Explanation: The term 'nice' comes from the idea that if a task increases its nice value (e.g., from 0 to +10), it is being 'nice' to other tasks in the system by lowering its relative priority. This allows other tasks to receive more CPU time.
Q32. What is the default scheduling policy for threads created in the POSIX real-time scheduling API example?
📖 Explanation: In the POSIX real-time scheduling API example, the scheduling policy is set to SCHED_FIFO using the pthread_attr_setschedpolicy function. This demonstrates how to set real-time scheduling policies for threads in a POSIX-compliant system.
Q33. Which function is used in the POSIX real-time scheduling API to get the current scheduling policy?
📖 Explanation: The pthread_attr_getschedpolicy function is used to get the current scheduling policy from a thread attributes object. The example shows this function being used to retrieve and then print the current policy (SCHED_OTHER, SCHED_RR, or SCHED_FIFO).
Q34. In the CFS scheduler, what happens to targeted latency when the number of active tasks exceeds a certain threshold?
📖 Explanation: Targeted latency can increase if the number of active tasks in the system grows beyond a certain threshold. This ensures that each task still gets a reasonable time slice even when many tasks are active, preventing any single task from being starved for too long.
Q35. What is the key difference between CFS and the O(1) scheduler regarding interactive process response?
📖 Explanation: CFS was designed to address the poor response times for interactive processes that were a drawback of the O(1) scheduler. CFS achieves this by using virtual run time and prioritizing tasks that have received less CPU time, which typically includes I/O-bound interactive tasks.
Q36. What does SCHED_FIFO stand for in Linux real-time scheduling?
📖 Explanation: SCHED_FIFO stands for First In, First Out, which is a real-time scheduling policy where a thread runs until it either completes or is blocked by a higher-priority thread. This is one of the POSIX real-time scheduling policies supported by Linux.
Q37. What does SCHED_RR stand for in Linux real-time scheduling?
📖 Explanation: SCHED_RR stands for Round Robin, which is a real-time scheduling policy where threads of the same priority share CPU time in a round-robin fashion. This is the other POSIX real-time scheduling policy supported by Linux, alongside SCHED_FIFO.
Q38. In the global priority scheme of Linux, which has higher priority: a real-time task or a normal task?
📖 Explanation: In the global priority scheme, real-time tasks have higher priority than normal tasks. Real-time tasks are assigned priorities in the range 0-99, while normal tasks are assigned priorities in the range 100-139. Since numerically lower values indicate higher priority, real-time tasks always preempt normal tasks.
Q39. What is the key feature of the CFS scheduler that allows it to be 'completely fair'?
📖 Explanation: CFS achieves fairness by using virtual run time (vruntime). Tasks that have received less CPU time have smaller vruntime values and are therefore given priority. This ensures that over time, all tasks receive a fair proportion of CPU time proportional to their nice values.
Q40. What is the minimum priority for a real-time task in Linux?
📖 Explanation: In Linux, real-time tasks are assigned priorities in the range of 0 to 99, where numerically lower values indicate higher relative priorities. Therefore, the minimum priority (lowest priority) for a real-time task is 99, and the highest priority is 0.
Q41. What is the maximum priority for a real-time task in Linux?
📖 Explanation: In Linux, real-time tasks are assigned priorities in the range of 0 to 99. Since numerically lower values indicate higher relative priorities, priority 0 represents the highest priority for a real-time task. This allows the most time-critical tasks to be assigned the highest priority.
Q42. What is the minimum priority for a normal task in Linux?
📖 Explanation: Normal tasks in Linux are assigned priorities in the range of 100 to 139, where numerically lower values indicate higher relative priorities. Therefore, the minimum priority (lowest priority) for a normal task is 139, and the highest priority is 100.
Q43. What is the maximum priority for a normal task in Linux?
📖 Explanation: Normal tasks in Linux are assigned priorities in the range of 100 to 139. Since numerically lower values indicate higher relative priorities, priority 100 represents the highest priority for a normal task. This is the highest priority a non-real-time task can receive.
Q44. How does the CFS scheduler treat a task that has been given less processing time?
📖 Explanation: In the CFS red-black tree, tasks that have been given less processing time (smaller values of vruntime) are toward the left side of the tree. Since the leftmost node has the smallest vruntime, these tasks are given priority for execution, ensuring fairness.
Q45. How does the CFS scheduler treat a task that has been given more processing time?
📖 Explanation: In the CFS red-black tree, tasks that have been given more processing time (larger values of vruntime) are toward the right side of the tree. These tasks have lower priority for execution, allowing tasks with less CPU time to run first.
Q46. What is the purpose of the decay factor associated with vruntime in CFS?
📖 Explanation: The decay factor ensures that lower-priority tasks have higher rates of decay, meaning their vruntime increases faster than higher-priority tasks for the same amount of actual CPU time. This ensures that lower-priority tasks are effectively penalized, receiving less CPU time over the long term.
Q47. In the CFS scheduler, what is the relationship between a task's nice value and its vruntime decay rate?
📖 Explanation: Tasks with higher nice values (lower priority) have higher rates of decay. This means their vruntime increases faster for the same amount of actual CPU time, causing them to be scheduled less frequently. Conversely, tasks with lower nice values (higher priority) have lower decay rates.
Q48. What is the key advantage of using a red-black tree for task management in CFS?
📖 Explanation: A red-black tree is a balanced binary search tree that provides efficient insertion and retrieval operations (O(lg N)). This is important for the scheduler, which frequently adds and removes tasks from the run queue while needing to quickly find the task with the smallest vruntime.
Q49. What happens to a task's vruntime when it is preempted?
📖 Explanation: When a task is preempted, its vruntime value is saved. When the task resumes execution, it continues from its saved vruntime value. This ensures that the task's accumulated CPU time is tracked correctly across preemptions, maintaining fairness in the scheduling algorithm.
Q50. What is the purpose of the POSIX real-time scheduling API example shown in Figure 6.20?
📖 Explanation: The POSIX real-time scheduling API example demonstrates how to create threads with specific scheduling policies (SCHED_FIFO, SCHED_RR, or SCHED_OTHER). It shows the use of pthread_attr_setschedpolicy and pthread_attr_getschedpolicy to set and get scheduling policies for threads.
Q51. What scheduling policy is set in the POSIX real-time scheduling API example?
📖 Explanation: In the example, the scheduling policy is set to SCHED_FIFO using the pthread_attr_setschedpolicy function. This demonstrates how to set a real-time FIFO scheduling policy for threads in a POSIX-compliant system.
Q52. What is the range of normal task priorities that corresponds to nice values?
📖 Explanation: Normal tasks are assigned priorities from 100 to 139 based on their nice values. A nice value of −20 maps to priority 100 (highest priority), and a nice value of +19 maps to priority 139 (lowest priority). This provides a direct mapping between the nice value and the scheduler priority.
Q53. What does the variable rb_leftmost represent in the CFS scheduler?
📖 Explanation: The rb_leftmost variable caches the leftmost node of the red-black tree. In the CFS scheduler, the leftmost node has the smallest vruntime value, which represents the task with the highest priority for execution. Caching this value allows the scheduler to select the next task in O(1) time.
Q54. What happens to targeted latency when there are few active tasks in the system?
📖 Explanation: When there are few active tasks, targeted latency remains at its default or minimum value. It only increases when the number of active tasks exceeds a certain threshold, ensuring that each task still gets a reasonable time slice even with many tasks.
Q55. What is the key insight behind the CFS scheduler's approach to fair scheduling?
📖 Explanation: The CFS scheduler aims to provide each task with a proportional share of CPU time based on its nice value. This is achieved through the vruntime mechanism, where tasks accumulate virtual run time at rates determined by their priority, ensuring that over time, each task receives its fair share of CPU time.
Q56. What is the significance of the CFS scheduler being 'completely fair'?
📖 Explanation: CFS is 'completely fair' in the sense that it provides proportional CPU time to tasks based on their nice values. Tasks with higher priority (lower nice values) receive proportionally more CPU time than tasks with lower priority, but all tasks receive their fair share based on their allocated proportion.
Q57. What is the main reason the O(1) scheduler was replaced by CFS?
📖 Explanation: The O(1) scheduler was replaced because it led to poor response times for interactive processes common on desktop systems. The CFS scheduler was designed to provide better interactivity and fairness by using a more sophisticated vruntime-based approach.
Q58. How does the CFS scheduler ensure that a CPU-bound task does not starve I/O-bound tasks?
📖 Explanation: CFS ensures that I/O-bound tasks (which typically run for short periods and then block) receive higher priority because they accumulate less vruntime than CPU-bound tasks. This allows I/O-bound tasks to preempt CPU-bound tasks, ensuring good responsiveness for interactive and I/O-intensive workloads.
Q59. What is the range of priorities for all tasks in the Linux global priority scheme?
📖 Explanation: In the Linux global priority scheme, real-time tasks are assigned priorities in the range 0-99, and normal tasks are assigned priorities in the range 100-139. Therefore, the complete range of priorities is 0 to 139, where numerically lower values indicate higher relative priorities.
Q60. What is the highest priority value in the Linux global priority scheme?
📖 Explanation: In the Linux global priority scheme, numerically lower values indicate higher relative priorities. Therefore, the highest priority value is 0, which is assigned to the highest-priority real-time tasks. This allows the most time-critical tasks to receive the highest priority.
Q61. What is the lowest priority value in the Linux global priority scheme?
📖 Explanation: In the Linux global priority scheme, numerically lower values indicate higher relative priorities. Therefore, the lowest priority value is 139, which is assigned to the lowest-priority normal tasks (nice value +19). This ensures that even the lowest-priority tasks have a defined priority level.
Q62. What is the main difference between SCHED_FIFO and SCHED_RR in Linux real-time scheduling?
📖 Explanation: The main difference is that SCHED_FIFO (First In, First Out) allows a thread to run until it completes or is blocked by a higher-priority thread, while SCHED_RR (Round Robin) allows threads of the same priority to share CPU time in a round-robin fashion, ensuring fairness among same-priority real-time tasks.
Q63. What is the purpose of the red-black tree in the CFS scheduler?
📖 Explanation: The red-black tree is used to store runnable tasks, with the key being the vruntime value. The tree is balanced, allowing efficient insertion and retrieval of tasks. The leftmost node of the tree has the smallest vruntime, which represents the task with the highest priority for execution.
Q64. What is the key insight behind the O(1) scheduler's design?
📖 Explanation: The key insight behind the O(1) scheduler is that it runs in constant time (O(1)) regardless of the number of tasks in the system. This is achieved through the use of multiple run queues and bitmaps that allow the scheduler to quickly find the highest-priority task.
Q65. What was the primary goal of the CFS scheduler when it was introduced in Linux 2.6.23?
📖 Explanation: The primary goal of the CFS scheduler was to provide better response times for interactive processes, which was a known drawback of the O(1) scheduler. CFS achieves this by using a vruntime-based approach that gives priority to tasks that have received less CPU time, which typically includes interactive I/O-bound tasks.
Q66. How does the CFS scheduler handle a task with a nice value of +19?
📖 Explanation: A task with a nice value of +19 has the lowest priority among normal tasks. It maps to priority 139 in the global priority scheme, which is the lowest priority level for normal tasks. This task will receive the smallest proportion of CPU time among normal tasks.
Q67. How does the CFS scheduler handle a task with a nice value of −20?
📖 Explanation: A task with a nice value of −20 has the highest priority among normal tasks. It maps to priority 100 in the global priority scheme, which is the highest priority level for normal tasks. This task will receive the largest proportion of CPU time among normal tasks.
Q68. What is the purpose of the pthread_attr_init function in the POSIX real-time scheduling API example?
📖 Explanation: The pthread_attr_init function is used to initialize a thread attributes object with default values. This is the first step in the example, setting up the attributes object that will later be modified to set the scheduling policy and used to create threads.
Q69. What is the purpose of the pthread_create function in the POSIX real-time scheduling API example?
📖 Explanation: The pthread_create function is used to create new threads. In the example, it creates NUM_THREADS threads, each using the thread attributes object that has been configured with the SCHED_FIFO scheduling policy. The runner function is specified as the entry point for each thread.
Q70. What is the purpose of the pthread_join function in the POSIX real-time scheduling API example?
📖 Explanation: The pthread_join function is used to wait for threads to complete. In the example, it joins each of the created threads, blocking until each thread finishes execution. This ensures that the main thread waits for all worker threads to complete before exiting.
Q71. What does the runner function do in the POSIX real-time scheduling API example?
📖 Explanation: The runner function is the entry point for each thread created in the example. It performs some work (indicated by the comment) and then calls pthread_exit to terminate the thread. This demonstrates the structure of a thread function in a POSIX program.
Q72. What is the key advantage of using scheduling classes in Linux?
📖 Explanation: Scheduling classes allow the kernel to accommodate different scheduling algorithms based on the needs of the system and its processes. For example, the scheduling criteria for a Linux server may be different from those for a mobile device, and scheduling classes provide the flexibility to support these different requirements.
Q73. What is the relationship between a task's vruntime and its priority in the CFS scheduler?
📖 Explanation: In the CFS scheduler, the task with the smallest vruntime value has the highest priority. This is because tasks with smaller vruntime have received less CPU time and are therefore given priority to ensure fairness. The scheduler selects the task with the smallest vruntime to run next.
Q74. What is the purpose of the pthread_attr_setschedpolicy function in the POSIX real-time scheduling API example?
📖 Explanation: The pthread_attr_setschedpolicy function is used to set the scheduling policy in a thread attributes object. In the example, it sets the policy to SCHED_FIFO, which will be used when threads are created with this attributes object. This demonstrates how to configure threads for real-time scheduling.
Q75. What is the primary reason for adding new scheduling classes to the Linux kernel?
📖 Explanation: New scheduling classes can be added to the Linux kernel to accommodate different scheduling algorithms based on the needs of the system and its processes. This flexibility allows Linux to adapt to various workloads and system types, from servers to mobile devices.