Linux Scheduler (2024)

Linux Scheduler

The PCB on linus is combined with the kernel stack, into a contiguousblock of 8K (two 4K pages), with the kernel stack from top growingdownwards, and the PCB a fixed structed aligned along the low memoryboundary.

The Linux schedulers recognizes three scheduling classes: real-timeround-robin and fifo classes, and a time-share other class.The policy field of the the task_struct contains the integerdefined in include/linux/sched.h line 108 (linux 2.4.22).

Real-time run first based on the rt_priority value in thePCB. Else the algorithm for selection is based on the result of the goodnessfunction in sys/kernel/sched.c (line 144). the task_struct.counter is added to the nice value, with bonus pointsfor CPU and memory affinity. The counter is the number of ticks leftin the qunatum. Once everyone has eaten their quantum, an epoch completesand new quantum are distributed.

The scheduler was major overhauled from 2.4 to 2.5. The file to seeis kernel/sched.c. At first glance, the changes seem larger thanthey were. It seems to me now that just some fairly standard data structure techniques were used, and in hindsight it is odd that the originalscheduler didn't use these techniques.But there are some other changes whose impact might be more subtle.Google O(1) Scheduler Linux for more information.

Exhibits, from linux 2.4.22

The loop on line 606 of schedule is the heart of scheduling theSCHED_OTHER task. On line 578 is the scheduling of SCHED_RR tasks.Note position at end of run queue and the reloading of the count.On line 622 wis the end of epoch calculation. Note that nice isadded twice. It makes the quota longer (count) and count is a part of goodness, but also goodness adds the nice value again.

Is that odd?

Goals of Scheduling

Scheduling can be seen as either:

  1. The OS objective of efficient use of resource. In particular, theCPU Burst/IO Burst model of process execution means that a process willbe waiting much of the time. Interleaving several processes will keepthe CPU busy. This is a gain in efficiency for free.
  2. The OS objective of support process abstractions. First, the processabstraction hides the CPU resource allocation. If a process expects toexecute, it is the scheduler that must provide CPU cycles to fulfill thisexpectation.
  3. Second, the process abstraction might include a seemingly simultaneousadvancement of processes. This is the essence of time-sharing, or ofinteractive processes, depending on how things are defined. The schedulermust allocate CPU is such a way the the interleaving of cycle allocationsamong processes is transparent to the user.
  4. Or the OS objective of isolation and protection. A process shouldnot be able to hog the CPU.

Preemption

Preemption is the removal of the CPU resource from a process except at certain natural pauses in the process' progress.In order to provide transparent support for the simultaneous progressionmodel of processes, preemption is necessary. Let's say that thenatural pauses of a process is an I/O wait, or a deliberate wait of some sort (death of child, wait for a signal), or process exit.The the additional preemption points used by most kernels are:

  • Return from a system call.
  • Return from a hardware interrupt (unrelated to the process).
  • A dedicated preemption interrupt.
The dedicated preemption interrupt is a clock which forces a preemptiondecision on a regular basis regardless of the process' behavior.From another viewpoint, this third case can be merged with thesecond, since the preemption interrupt will be a hardware interruptdriven by a clock device, but we have kept it separate because itseems to represent the Big Step in the decision to implement a fullyrobust preemption.

Preemption can further be considered as to whether it can occur in when the process is in kernel mode, or only in user mode. TraditionalUnix kernels did not preempt the process when running un kernel mode. The kernel shared data structures across processes, hence kernelpreemption would introduce issues of data structure consistency andsynchronization.Instead, in Unix, the preemption event occuring in kernel modewas handled on return from the system call which placed toprocess in kernel mode.

Scheduling Qualities

I am going to go out on a limb and say some original things.There are there big aspects to scheduling:

  • CPU Utilization. Scheduling should keep the CPU busy.
  • Overall utilization. CPU Utilization is trivial, but thepropoer overlap so that jobs are not kept waiting is important.In the boot they categorize this as total wait time.
  • Latency. Scheduling might want to shorten the time betweenan event and the time the process handling the event runs.
  • Progress. Scheduling might want to make sure that everyprocess makes some amount of progress.
There are some other measures that can be employed. For Batchprocesses, one can measure the number of jobs finished overa time period (through-put); or he time from start to finish ofa job (turn-around). These have real-life analogs. The expresslane in a supermarket has higher through-put. Why is that good?It should also have better turn-around. The first class linefor airplanes does not have utilization but it has the bestturn-around. Why is that good?

Processes have various needs and the scheduler should match those needs.Interactive processes need low latency. Hopefully they are easilysatisfied in terms of progress (quick but small repsonses to events).

Finally, satisfaction of those needs can be measured either byaverage, best or worst case results, or by a consistency value.

Linux Scheduler (2024)

FAQs

What is the nice value in Linux scheduler? ›

The nice value determines the priority of the process. The higher the value, the lower the priority--the "nicer" the process is to other processes. The default nice value is 0 on Linux workstations. The SZ field displays the size of the process in memory.

What is the current Linux scheduler? ›

The Linux 6.6 kernel introduces the new Earliest Eligible Virtual Deadline First (EEVDF) scheduler that will replace the existing Completely Fair Scheduler (CFS) that has been the primary CPU scheduler since 2007.

Is Linux scheduler better than Windows? ›

Linux scheduler:

Linux handles this rather better. It actively prefers to keep threads on the same core for as long as there are no scheduling conflicts on that core. So a single-threaded workload on Linux will usually stay on the same core for several seconds at a time, if not longer.

Is the Linux scheduling policy adequate for hard real time systems? ›

Linux is not a hard real-time operating system, as it does not guarantee that a task will meet strict deadlines. The kernel can suspend a task when its time slice has completed, and it can remain suspended for an arbitrarily long time (for example, when an interrupt is getting serviced).

How to check nice value in Linux? ›

The nice value is used by the system to calculate the current priority of a running process. Use the ps command with the -l flag to view a command's nice value. The nice value appears under the NI heading in the ps command output. Note: The csh command contains a built-in command named nice.

What is the nice limit in Linux? ›

The range of the nice value is +19 (low priority) to -20 (high priority). Attempts to set a nice value outside the range are clamped to the range. Traditionally, only a privileged process could lower the nice value (i.e., set a higher priority).

What is the most common scheduler used in Linux? ›

Cron Jobs: Perhaps the most common method for automating tasks in Linux is through cron jobs. Cron is a time-based job scheduler in Unix-like operating systems.

What is ideal fair scheduling? ›

In other words, if there are N processes present in the system or present in the ready queue and waiting to be scheduled, then each process will receive 100 by N percentage of the CPU time. So, this is the ideal fairness. Let us take a small theoretical example for this ideal fair scheduling.

What is the default scheduler in Linux? ›

SCHED_OTHER is the standard Linux time-sharing scheduler that is intended for all threads that do not require the special real-time mechanisms. The thread to run is chosen from the static priority 0 list based on a dynamic priority that is determined only inside this list.

Which is the best scheduler in OS? ›

Short-term schedulers

So the short-term schedulers select one process from the ready queue, put it in the running queue, and allocate a processor(CPU) to it. They are also known as the dispatcher who decides which process will execute next. They are faster than long-term schedulers.

Why do developers prefer Linux over Windows? ›

Unlike Windows, which often requires frequent updates and reboots, Linux systems can run for extended periods without performance degradation. The lightweight nature of many Linux distributions ensures minimal resource consumption, allowing developers to utilize more computing power for their applications.

Why you should use Linux instead of Windows? ›

5 Compelling Reasons to Consider Linux Over Windows 🐧💻
  • System Stability: No More Blue Screens 🛡️
  • Customization: Your OS, Your Way 🎨
  • Security: A Fort Knox for Your Data 🔒
  • Privacy: Your Data Stays Yours 🛡️
  • Cost-Effectiveness: Keep Your Wallet Happy 💰
Feb 25, 2024

How does NASA use Linux? ›

Human-in-the-loop (HITL) simulation groups at NASA and the Air Force Research Lab have been using Linux as a real-time operating system (RTOS) for over a decade. More recently, SpaceX has revealed that it is using Linux as an RTOS for its Falcon launch vehicles and Dragon capsules.

Does Linux use completely fair scheduler? ›

Linux takes a modular approach to processor scheduling in that different algorithms can be used to schedule different process types. A scheduling class specifies which scheduling policy applies to which type of process. Completely fair scheduling (CFS), which became part of the Linux 2.6.

How is scheduling done in Linux? ›

The Scheduling Algorithm
  1. The Linux scheduling algorithm works by dividing the CPU time into epochs . ...
  2. Each process has a base time quantum , which is the time-quantum value assigned by the scheduler to the process if it has exhausted its quantum in the previous epoch.

What is the nice value of a processor in Linux? ›

The nice value is an integer, usually in the range -20 to 20, and represents an upper limit on a process' dynamic priority. The higher the nice number, the lower that limit.

What is nice 19 in Linux? ›

Linux uses separate priority ranges for normal and real time tasks. For normal tasks a priority range (or nice value) of -20 to +19 is used. Lower nice corresponds to higher priority. In other words the task is being less nicer to other tasks in the system.

What is a nice running process in Linux? ›

In Linux, the Nice command is used to prioritize the execution of processes. It allows users to control the priority levels of processes, determining how much system resources a particular process should receive. By assigning a "niceness" value to a process, users can influence its priority and resource allocation.

What is nice number in Linux C? ›

nice() adds the value of increment to the nice value of the calling process. A process's nice value is a nonnegative number for which a more positive value results in a lower CPU priority. A maximum nice value of 2*{NZERO}-1 and a minimum value of zero are imposed by the system.

Top Articles
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 5751

Rating: 4.8 / 5 (78 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.