Vous êtes sur la page 1sur 5

Scheduling

1. What is a scheduler? a. Scheduler is a subsystem in kernel that schedules processes to run in CPU. b. Scheduler divides the finite resource of processor time among runnable processes. 2. What is multitasking? a. Capability of a OS to run multiple tasks concurrently 3. What is cooperative multitasking? a. In cooperative multitasking a process does not stop running until it voluntarily decides to do so. 4. What is preemptive multitasking? a. In preemptive multitasking scheduler decides when a process is to cease running and a when a process is to resume. 5. Linux supports Symmetrical multi-processing (SMP) 6. Linux is Preemptive Multitasking operating System. 7. What is an I/O bound process? a. An I/O bound process which spends much of its time on submitting and waiting on I/O requests. 8. What is a processor bound process? a. A processor bound process spends much of its time on executing code. 9. Linux favors I/O bound processes over Processor bound processes. 10. What are runqueues? a. The runqueue is the list of runnable processes on a given processor. 11. What is the core data structure of a linux scheduler? a. runqueue 12. Runqueues contain per-processor scheduling information. 13. Linux provides how many runqueues per processor? a. One 14. Each runnable process is exactly on one runqueue 15. Before a runques can be manipulated it must be locked. 16. What synchronization mechanism is used to lock a runqueue? a. Spinlock 17. To avoid deadlock, code that wants to lock multiple runques needs always to obtain the locks in the ascending order of runqueue address. 18. Nested locks need to be obtained in the same order. 19. Each runqueue contains two priority arrays. 20. For each processor linux scheduler maintains an active array and an expired array. 21. Active array contains all the tasks in the associated runqueue that have time slice left. 22. Expired array contains all the tasks in associated runqueue that have exhausted their time slice. 23. Priority arrays in linux provide O(1) scheduling. 24. Each priority array contains one queue of runnable processes per priority level.

25. Priority array contain a priority bitmap used to efficiently discover highest priority runnable task in the sub system. 26. Priority array bitmap is an array of 160 bits. 27. Linux implements find first set algorithm to quickly search the bitmap. 28. sched_find_first_bit() is called to find the first set bit in the priority array bitmap. 29. When a task of a given priority level becomes runnable the corresponding bit in the bitmap is set to one. 30. MAX_PRIO defines the number of priority levels in the system. 31. What is the default number of priority levels in linux? a. 140 32. In a given priority tasks are run in round robin. 33. Priority array contains nr_active counter that gives number of runnable tasks in the given priority. 34. What is the purpose of schedule() function? a. Function of scheduler is to pick the next task to run and switching to it 35. schedule() function is called explicitly by kernel code that wants to sleep and when is task is to be preempted. 36. How scheduler works in linux? a. Schedule() function when called does the following i. First active priority is searched to find the first set bit This bit corresponds to the highest priority thread that is runnable ii. Next the scheduler selects the first task in the list at that priority. This is the highest priority runnable task on the system 37. What is nice value? a. Nice is the initial static priority value given to a process 38. Where is nice value stored? a. Nice value is stored in stiatic_prio member of task_struct 39. What is the range of Nice value? a. -20 to 19 19 is the lowest priority -20 is of highest priority 40. Dynamic priority in linux is calculated as a function of static priority of task and its interactivity. 41. What is the method that calculates the dynamic priority of task? a. effective_prio() 42. How dynamic priority of a task is calculated? a. effective_prio() computes the dynamic priority of task. i. It begins with the tasks nice value and computes a bonus or penalty in the range -5 to +5 based on interactivity. ii. Interactivity of a task is determined by a metric, sleep_avg, that how long the task sleeps. sleep_avg is member of task_struct. If a ask spends more time in sleeping than running then it is I/O bound If a task spends more time in running than sleeping then it is processor bound

When a task becomes runnable after sleeping, sleep_avg is incremented by how long it slept, until the value reaches MAX_SLEEP_AVG. For every timer tick the task runs, sleep_avg is decremented until it reaches zero. Sleep_avg ranges from 0 to MAX_SLEEP_AVG Default value of MAX_SLEEP_AVG is 10 milliseconds 43. How timeslice is computed? a. Timeslice is computed based on dynamic priority i. When a process is first created , the new child and parent split the parents remaining time slice. ii. After a tasks timeslice is exhausted it is recalculated based on tasks dynamic priority The function task_timeslice() returns a new time slice for the given task a. Calculation simple scaling of timeslices in to a range of timeslices b. The higher a tasks priority more timeslice it receives per round of execution c. The maximum timeslice given to the highest priority task is MAX_TIMESLICE. Default value is 200miiliseconds. d. The lowest priority task receives at least the minimum timeslice, MIN_TIMESLICE. Default value is 10 milliseconds. e. Tasks with the default priority (Nice value zero and no interactivity bonus or penalty) receives a timeslice of 100 milliseconds. iii. If a task is sufficiently interactive, when it exhausts its timeslice, it will not be inserted back into expired array. Instead it is reinserted back into active array and will be scheduled in round robin way. iv. When tasks in active array exhausts their time slice they are moved from active array to expired array. When all tasks are exhausted and moved into expired array, the two arrays are switched: Active array becomes expired array and active array becomes active. These arrays are accessed via, pointer; hence switching active and expired arrays is achieved by swapping pointers. This swap is the key feature of new O(1) scheduler. 44. When does a process got to sleep, give examples? a. A process goes to sleep when they are blocked, waiting for an event to occur. Some examples are as below. i. Event can be specified amount of time ii. More data from a file I/O iii. Another hardware interrupt iv. A process can involuntarily sleep while trying to acquire a contended semaphore 45. How sleeping and waking up work?

46.

47.

48. 49.

50.

51.

52.

53.

a. When a task is blocked it marks itself as sleeping, puts itself on a wait queue, removes itself from a runqueue and calls schedule() to select a new task to execute. b. When an even associated with a sleeping task for which it is blocked is completed, the task is set to runnable, removed from the wait queue and is added to runqueue. How a process does add itself to a wait queue? a. Create a wait queue entry via DECLARE_WAITQUEUE() b. Add itself to a wait queue via add_wait_queue() c. Change the process state to TASK_INTERRUPTIBLE or TASK_UNITERRUPTIBLE. d. Test if the condition is true, if it is, there is no need to sleep. If it is not true, call schedule() e. When the task awakes, it will again check if the condition is true, if it is, it will exit the loop. Otherwise it will again call schedule() and repeat. f. When the condition is true, the task can set itself to TASK_RUNNING and removes itself from wait queue via remove_wait_queue () How a process wakes from sleep? a. Waking of a task is handled via wake_up (). It wakes up all tasks waiting on the given wait queue. b. It calls try_to_wake_up (), which sets the tasks state to TASK_RUNNING c. It calls activate_task to add task to a runqueue. d. It sets need_resched, if the woken up task has higher priority than currently running task What is context switching? a. Context switching is the switching from one runnable task to another. How context switching occurs? a. When a new process has been selected to run it does the following i. Calls switch_mm () to switch the virtual memory mapping from the previous processs to that of new process. ii. Calls switch_to () to switch the processor state from the previous processs to the new process. This involves saving stack information and the process registers. How kernel does know that it has to schedule ()? a. Kernel checks the need_resched flag whether a reschedule needs to be performed. i. need_resched flag is set in two ways by schedule_tick when a process runs out of timeslice by try_to_wake_up () when a process that has higher priority than the currently running process is awakened. When user preemption does occur? a. When returning to user space from a system call b. When returning to user space from interrupt handler. When kernel preemption does occur? a. When returning to kernel space from an interrupt handler b. When kernel code becomes preemptible again c. If a task in the kernel explicitly calls schedule () d. If a task in the kernel blocks. It results in a call to schedule () What are the scheduling policies that are supported in linux?

54.

55. 56.

57.

58.

a. Linux supports real time and non real time scheduling policies i. Real time scheduling policies, implements static priority SCHED_FIFO SCHED_RR ii. Non real time scheduling policies, implements dynamic priority SCHED_OTHER Is linux a real time OS? a. No i. Real time scheduling policies in linux provide soft real time behaviour. Soft real time refers to notion that kernel tries to schedule application within timing deadlines Hard real time systems are guaranteed to meet any scheduling requirements within certain limits. Real time priority range in linux is 1 to MAX_RT_PRIO a. Default value of MAX_RT_PRIO is 100. What is processor affinity in linux? a. Processor affinity is the scheduling of a process on a given processor or set of processors. b. Linux tries to provide soft or natural affinity by attempting to keep processes on the same processor c. But user can set hard affinity. Hard affinity is stored as a bitmask in the tasks task_struct as cpus_allowed. i. Linux scheduler enforces hard processor affinity What is yielding? a. If currently running process can explicitly voluntarily yield the processor to another process then it is called yielding. i. Linux provides sched_yield () to explicitly yield the process to other waiting processes. Scheduler related system calls.

Scheduler related system calls


System Call nice () sched_setscheduler () sched_getscheduler () sched_setparam () sched_getparam () sched_get_priority_max () sched_get_priority_min () sched_rr_get_interval () sched_setaffinity () sched_getaffinity () sched_yield () Description Set a processs nice value Set a processs scheduling policy Get a processs scheduling policy Set a processs real time priority Get a processs real time priority Get the maximum real time priority Get the minimum real time priority Get a processs timeslice value Set a processs processor affinity Get a processs processor affinity Temporarily yield the processor