The scheduling is basically divided into 2 categories First is Clock-Driven Scheduling and one is Event-driven scheduling. The Clock-Driven scheduling algorithms are those algorithms in which interrupts that are received by a clock helps to determine the scheduling point and these are FCFS, Round Robin, etc. In an Event-Driven algorithm, the events help to determine the scheduling point. The Event-Driven is further divided into 3 types first is Simple priority-based, second is Rate Monotonic Analysis (RMA) and the third is Earliest Deadline First (EDF).
Foreground-Background Scheduling lies under the first type of the Event-Driven algorithm i.e. Simple priority-based. In this algorithm, 2 waiting lists are maintained one is Foreground and another one is Background. At first, every process enters on the Foreground list for its execution and stays there for a small period of time for its execution until the threshold time limit is reached, after this if the process is incomplete then that process enters in Background waiting list where it will be executed only when the foreground list is empty and in Background waiting list Round Robin scheduling algorithm is used to execute the process.
So, according to the above procedure, The priority of the Foreground list is higher than the Background list and the threshold time of the Foreground is shorter than the Background list. Usually in the Foreground list process is allowed to use 2 quanta after that it will send to Background and in the background, the process will get 4 quanta to complete all its work and if the process is still incomplete then it gets back to the end of the Background list and wait for its turn to get 4 quanta again and this process gets repeat itself until the process completes its work. This algorithm helps in those cases where it is necessary to execute the process just after its creation.
Working of Foreground-Background Scheduler :
Foreground-Background Scheduler follows the following steps for scheduling real-time tasks:
All the real-time tasks are categorized as foreground and background tasks. Foreground tasks include periodic tasks and background tasks include sporadic and aperiodic tasks.
Foreground tasks are given the highest priority and background tasks are given the lowest priority.
While scheduling foreground tasks, at all scheduling points highest priority is considered.
When the foreground tasks are not ready for scheduling, then background tasks are scheduled.
Among the background tasks, the same scheduling rules are followed as in foreground tasks.
Completion Time for Foreground Tasks :
As earlier mentioned, foreground tasks include the periodic tasks therefore their completion time is the same as their absolute deadline.
Completion Time for Background Tasks :
When all the foreground tasks all scheduled only after that background tasks are scheduled. When any foreground task is being executed, background tasks await.
Let Task Ti is the foreground task, and Ei is the amount of processing time required over every Pi period.
Avg. CPU utilization for Ti is Ei/Pi
If there are n periodic tasks (foreground tasks) i, e.
T1, T2, T3, ..., Tn
Then Total avg. CPU utilization for foreground tasks,
= (E1/P1) + (E2/P2) + ... + (En/Pn)
Therefore, the avg. time available for execution of background tasks in every unit of time is,
1 - (E1/P1) + (E2/P2) + ... + (En/Pn)
Let Tb is the background task, and Eb is the required processing time, then
Completion Time for background task (CTb),
= Eb / ( 1 - (E1/P1) + (E2/P2) + ... + (En/Pn) )
In a real-time system, tasks are scheduled using foreground-background scheduling algorithm, Tf is only foreground task with Ef = 100 ms and Pf = 200 ms, there is one background task Tb whose Eb = 500 ms.
The Completion Time for the background task
CTb = Eb / (1 - Ef/Pf) = 500 / (1 - 100/200) = 1000 ms
Therefore, the background task will take 1000 ms to complete.
Advantages of Foreground-Background Scheduling:
- Periodic tasks are given the highest priority.
- It preempts the execution of an ongoing tasks for incoming higher priority tasks.
Disadvantages of Foreground-Background Scheduling:
- Background tasks may go in starvation.
- Sometimes preemption may create havoc.
1. Consider we have 3 tasks T1, T2, T3 in the foreground and T4 task in the background.
Task Number ei (in ms) pi (in ms) t1 5 20 t2 10 25 t3 15 50
Compute the completion time for the T4 when its processing time requirement is 200 ms to complete.
Solution: The first task is to find the summation of all the tasks in the Forehead. So, that we get the Total CPU utilization for forehead tasks
(e1 / p1) + (e2 / p2) + (e3 / p3)
(5/20) + (10/25) +(15/50)
(25 + 40 + 30) / 100
The second task is to subtract 1 with the summation. So, that we get to know that how much average time is available for the background task in every unit of time
1 – (95/100)
And at last, we have to divide the eB by the above value
So, the total completion time of the background task is 4000 ms.
2. Consider 2 tasks one is in foreground T1 and another one is in background TB. The completion time for the background task TB is 3000 ms and the processing time required for TB is 1500 ms. Compute the required processing time for T1 when p1 is 100 ms.
Solution: given: ctB=3000 ms , eB=1500 ms , p1=100 ms
ctB = eB / ( 1 – e1 / p1 )
3000 = 1500 / ( 1 – x / 100 )
2 = 1 / ( (100 – x ) / 100 )
2 = 100 / (100 – x)
200 – 2x = 100
2x = 100
x = 50
Required process time for T1 is 50 ms
Overhead during context switch between Foreground and background waiting list:
At first, the foreground task runs, it occurs an overhead of 2 ms as it is clear from the image. Overhead time is represented by the gray area, the list which is running at a time is represented by green color, and the list which gets preempted and waiting for its turn or idle is represented by white color. From the above figure, it is clear that from 2 to 52 foreground list is running background list is waiting. Only one list is allowed to run its tasks at a time such as at first foreground’s task run then it gets preempted after a particular time period for background tasks after the background time period gets completed, it gets preempted for the foreground, and so on. At the initial state, there is an overhead of 2 ms and after that foreground starts its work (shown in image), It is clear that the execution time of every foreground task is increased by two context switch times (one due to itself i.e. 0 to 2 ms and the other due to the background task running after each time it completes i.e. 52 to 54 ms). Thus, the context switch time is increased by 4 ms because in 1 context switch it takes 2 ms therefore in 2 context switches it takes 4 ms. So, the running time of the foreground list is increased and becomes 54 ms instead of 50 ms. Consider the period (pi) for the foreground task is 100 ms which means that after every 100 ms the background tasks get preempt and foreground tasks start running. Let’s take an example:
Example. Consider a task T1 in the foreground and its e1 is 50 ms and p1 is 100 ms and a task T2 in the background its e2 is 2000. Compute the completion time for background tasks when the overhead of every context switch is 2 ms.
Solution: From the above discussion, It is observed that e1 becomes 54 ms.
2000 / ( 1 – 54 / 100 )
2000 / ((100 – 54 ) / 100 )
200000 / 46
So, the completion time of the Background task is 4347.82 ms
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.