Related Articles

Foreground-Background Scheduling

• Last Updated : 01 Apr, 2021

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:

1. Step-1:
2. Step-2:
Foreground tasks are given the highest priority and background tasks are given the lowest priority.
3. Step-3:
While scheduling foreground tasks, at all scheduling points highest priority is considered.
4. Step-4:
5. Step-5:
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.
Hence,

`Avg. CPU utilization for Ti is Ei/Pi `

`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) ) `

Example:
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.

• Periodic tasks are given the highest priority.
• It preempts the execution of an ongoing tasks for incoming higher priority tasks.

• Background tasks may go in starvation.
• Sometimes preemption may create havoc.

Examples:

1. Consider we have 3 tasks T1, T2, T3 in the foreground and T4 task in the background.

Compute the completion time for the T4 when its processing time requirement is 200 ms to complete.

(e1 / p1) + (e2 / p2) + (e3 / p3)

(5/20) + (10/25) +(15/50)

(25 + 40 + 30) / 100

95/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)

5/100

And at last, we have to divide the eB by the above value

200/(5/100)

200*100/5

20000/5

4000

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:

Fig: Overhead in Foreground Background Scheduling

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

4347.82

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.

My Personal Notes arrow_drop_up