• Last Updated : 27 Aug, 2021

Prerequisite : CPU Scheduling

What is Scheduling with Deadline ?
The goal of a Scheduling problem is to schedule the tasks such that the maximum total profit is obtained. This algorithm for scheduling with a deadline is different from scheduling without a deadline because task completion here is associated with profit. In order to make a profit, the jobs have lets to be completed before the deadline. Otherwise, the job completion does not count or earn profit at all. The objective of this problem is to construct a feasible sequence that gives the maximum profit.

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.

A sequence is feasible if all the jobs end by their deadline. A set of jobs is called a feasible set if at least one sequence is possible. The sequence that is associated with the maximum profit is called the optimal sequence and the elements that constitute the sequence comprise the optimal set of jobs.

Example
Consider the items and profit shown in the following table and let’s find the optimal set of jobs that can be scheduled so that the profit is maximized.

The table depicts jobs with deadline and the profit

The objective of the given problem is to find a feasible set of solutions. Let us apply the greedy approach.

• Initially solution = NULL, then job 1 is added, so solution = {1}. It can be observed that job 2 is possible when added as task {2, 1} but not possible when added as a task {1,2}.
Why?  Let’s check for that situation,
• Consider the sequence {1,2}. Scheduling of task 1 is possible. However, scheduling task 2 is not possible as its deadline is just 1 unit, which is already spent in waiting time. On the other hand {2,1,} is feasible as the deadline of job 2 is not violated and that of job 1 is 2 units. Therefore, after job 2 is processed, job 1 can very well be accommodated. Similarly, one can observe that scheduling of job sequences {1,4}, {2,4}, {3,4}, {1,2,3},{2,3,4} and {1,2,3,4} are not possible.

All possible sequences are –

The maximum profit is associated with the sequence {4,1}. Therefore, the optimal order is {4,1}. One can also observe that while {4,1} is optimal, the sequence {1,4} is not possible as deadline conditions are violated.

Algorithm :

```Step1: Sort the jobs in a non-increasing order by profit
Step2: Solution = null
Step3: For all the task do the following:
select the next job;
EEND if
Step4: If all the instances are solved then exit.```

Formally, the algorithm for a scheduling problem with a deadline is as follows :

```Algorithm Schedule_with_deadline
%%Input: A set of jobs 1 to n with service item
%%Output: An optimal schedule

Begin
s= sorted array of jobs based on profit in non-decreasing order
i=1
schedule=NULL
while(i <= n) do                        %%for all jobs do
select the next job i from S        %% selection procedure
if(Scheduling job is feasible) then    %%Feasibility check
solution = schedule U job i of S
i=i+1
End while
return(solution)
End```

It can be observed that this algorithm first sorts the jobs based on profit, and then selects the jobs one by one for scheduling if the deadline considerations are not violated. This process is repeated for all the jobs, and finally, algorithms return the solution.

There is a principle that exists to check the feasibility of job sequences, this principle states that the sequence of jobs is feasible if its scheduling according to non-decreasing deadlines is feasible.

• Therefore, a sequence{1,2,3} is feasible as the jobs are scheduled based on non-decreasing deadlines.
• On the other hand, the sequence {1,2,3,4} is not feasible.
• The reason is that though there are 4 jobs, this sequence implies that all the jobs should be completed before 3-time units.
• As each job takes at least 1-time unit, the sequence is not feasible.
Thus one can check the feasibility of the scheduling of jobs using this observation as this is time-saving during competitive exams.
My Personal Notes arrow_drop_up