Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

real time scheduling in Linux

Tags:

This morning I read about Linux real time scheduling. As per the book 'Linux system programming by Robert Love', there are two main scheduling there. One is SCHED_FIFO, fifo and the second is SCHED_RR, the round robin. And I understood how a fifo and a rr algorithm works. But as we have the system call,

sched_setscheduler (pid_t pid, int policy, const struct sched_parem *sp) 

we can explicitly set the scheduling policy for our process. So in some case, two process running by root, can have different scheduling policy. As one process having SCHED_FIFO and another having SCHED_RR and with same priority. In that case, which process will be selected first? the FIFO classed process or the RR classed process? Why?

Consider this case. There are three process A,B,C. All are having same priority. A and B are RR classed processes and C is FIFO classed one. A and B are runnable (so both are running alternatively in some time intervel). And currently A is running. Now C becomes runnable. In this case, whether

1. A will preempt for C, or 2. A will run until its timeslice goes zero and let C run. Or 3. A will run until its timeslice goes zero and let B run.    a) here after B runs till its timeslice becomes zero and let C run or    b) after B runs till its timeslice becomes zero and let A run again (then C will starve untill A and B finishes) 
like image 967
theB Avatar asked Feb 21 '12 08:02

theB


People also ask

What is real-time process in Linux?

"Real time" (for a process) refers to the scheduling algorithm, or the thinking the kernel does when it decides which process gets to run. A real time process will preempt all other processes (of lesser scheduling weight) when an interrupt is received and it needs to run.

What is real-time process scheduling?

A real-time scheduling System is composed of the scheduler, clock and the processing hardware elements. In a real-time system, a process or task has schedulability; tasks are accepted by a real-time system and completed as specified by the task deadline depending on the characteristic of the scheduling algorithm.

What type of scheduling is used in Linux?

Linux uses a Completely Fair Scheduling (CFS) algorithm, which is an implementation of weighted fair queueing (WFQ). Imagine a single CPU system to start with: CFS time-slices the CPU among running threads.

What is real-time priority in Linux?

"Every real-time process is associated with a real-time priority, which is a value ranging from 1 (highest priority) to 99 (lowest priority). "


2 Answers

In realtime scheduling, FIFO and RR do not have exactly the same meaning they have in non-realtime scheduling. Processes are always selected in a FIFO- manner, however, the time quantum for SCHED_FIFO is not limited unlike the time quantum for SCHED_RR.

SCHED_FIFO processes do not preempt SCHED_RR processes of the same priority.

sched_setscheduler(2) - Linux man page

...

"A process's scheduling policy determines where it will be inserted into the list of processes with equal static priority and how it will move inside this list. All scheduling is preemptive: if a process with a higher static priority becomes ready to run, the currently running process will be preempted and returned to the wait list for its static priority level. The scheduling policy only determines the ordering within the list of runnable processes with equal static priority."

...

"A SCHED_FIFO process runs until either it is blocked by an I/O request, it is preempted by a higher priority process, or it calls sched_yield(2)."

...

"When a SCHED_FIFO process becomes runnable, it will be inserted at the end of the list for its priority."

...

"SCHED_RR: Round Robin scheduling

SCHED_RR is a simple enhancement of SCHED_FIFO. Everything described above for SCHED_FIFO also applies to SCHED_RR, except that each process is only allowed to run for a maximum time quantum. If a SCHED_RR process has been running for a time period equal to or longer than the time quantum, it will be put at the end of the list for its priority. A SCHED_RR process that has been preempted by a higher priority process and subsequently resumes execution as a running process will complete the unexpired portion of its round robin time quantum."

like image 138
svenfx Avatar answered Sep 23 '22 22:09

svenfx


man sched_setscheduler explains these scheduling policies in detail.

In this particular case because the two real-time processes have the same priority none of them will preempt the other. A SCHED_FIFO process runs until it blocks itself, SCHED_RR process runs until it blocks itself or its time quantum expires.

like image 38
Maxim Egorushkin Avatar answered Sep 25 '22 22:09

Maxim Egorushkin