Problem statement

The aim of this session is to be able to simulate and compare the dynamics of a G/G/1 queue adopting a number of scheduling disciplines and where jobs have hard deadlines. Specifically, when a job arrives, it should be completed before a deadline that is specific to the job. The deadline is “hard” in the sense it must be respected, in other case he job becomes useless and is consequently removed from the system.

We consider the following scheduling disciplines:

  1. FCFS (First Come First Served): jobs are processed following the order of their arrival. If a job’s deadline expires while it is being processed, the job is removed from the system. The server does not know the service times in advance.

  2. EDF (Earliest Deadline First): jobs are processed according to the earliest deadline with preemption, i.e., the current job is interrupted if a new job with an earlier deadline arrives.

  3. EDF-NP (Earliest Deadline First Non-preemptive): As EDF but jobs are not interrupted once their processing has started.

  4. SRPT (Shortest Remaining Processing Time): the job with the shortest remaining processing time is served first. Here, the jobs whose remaining processing times overflow their corresponding deadlines are not considered, i.e., removed from the system.

  5. SRT2D (Shortest Remaining Time to Deadline): the job with the shortest remaining time to deadline is served first. Note that “the remaining time to deadline” is always non-negative because jobs that cannot be completed by the deadline are removed from the system.

  6. SERT2D (Shortest Expected Remaining Time to Deadline): as SRT2D but instead of the remaining time to deadline, we consider the expected remaining time to deadline. The server does not know the service times in advance (but it knows their distribution function).

Step 0: Warm up

Discussion about scheduling disciplines

  1. FCFS :
  1. EDF :
  1. ED-NP :
  1. SRTP :
  1. SRT2D
  1. SERT2D

Step 1: Scheduling disciplines implementation

Assumptions

  • The arrival process is Poisson with rate \(\lambda\).
  • Service times are all independent and have the distribution of the random variable S. The random variable S is uniform over [0,10] with probability 0.85 and and uniform over [90,110] with probability 0.15.
  • The deadline of any job is \(x=5\) times larger than its service time.
  • In the G/G/1 queue, it is assumed that the server processes jobs with speed one.
  • The system load \(\rho\) is 0.9 - recall that \(\rho=\lambda/\esperance[S]\).

Main code

Here is the main R function that performs the simulation and print the results.

knitr::opts_chunk$set(echo = TRUE)

# This function performs the required simulation.
# As input, it takes the job service times (Service), their expected value (ES), the job deadlines (relative to the absolute job arrival time), and the system load
run_simulation <- function(Service, ES, Deadline, rho){
    debug=0;
    scheduling_disciplines = c("FCFS",
                               "EDF",
                               "EDF-NP",
                               "SRPT",
                               "SRT2D",
                               "SERT2D");
    num_arrival_rates=1;
    lambda = rho/ES;
    
    # Matrix of average response times for all disciplines and arrival rates
    Results <- matrix(0, nrow = length(scheduling_disciplines), ncol = 2);
    rownames(Results) <- scheduling_disciplines;
    colnames(Results) <- c("RT", "MDR"); # performance metrics
    
    # Poisson process coming in
    Interarrival = rexp(N, rate = lambda); # Exponentially distributed interarrival times
    Arrival= cumsum(Interarrival); # Arrival times
    AbsDeadline = Arrival+Deadline;
    
    for (scheduling_discipline in scheduling_disciplines)
    {
        t = 0;                            # simulation time
        Remaining = rep(N, x=NA);         # Remaining service times of each job; Stores remaining service times for jobs in the queue.
        ExpectedRemaining = rep(N, x=ES);         # Expected Remaining service times of each job; Stores remaining service times for jobs in the queue.
        Completion = rep(N, x=NA);        # Completion time of each job, Stores completion times.
        CurrentTask = NA;                 # Tracks the currently running job.
        NextArrival = 1;                  # Index for the next arriving job.
        AvgJobs = 0;                      # Tracks the average number of jobs in the system.
        
        while (TRUE) {
          
            if (debug==1){
                print("*********************");
                cat("InterArrival:  ", Interarrival, '\n');
                cat("Arrival:     ",Arrival,'\n');
                cat("AbsDeadline: ",AbsDeadline,'\n');
                cat("Service:     ",Service,'\n');
                cat("Remaining:   ",Remaining,'\n');
            }
          
            # Determine next event : continue running the same job or choose another
            dtA = NA; dtC = NA;
            
            if(length(Arrival[Arrival>t])>0) { # if an arrival exists after t
                dtA = head(Arrival[Arrival>t],n=1)-t ; # time to next arrival
            }
            if(!is.na(CurrentTask)) { # if a task is running
                dtC = Remaining[CurrentTask]; # time to next completion
            }
            if(is.na(dtA) & is.na(dtC)) {
                break;
            } 
        
            dt = min(dtA,dtC,na.rm=TRUE);
        
            # update system variables
            t = t + dt;
            AvgJobs=AvgJobs+dt*sum(!is.na(Remaining));
            
            # Job arrival
            if((NextArrival <=N) & (Arrival[NextArrival] == t)) {
                Remaining[NextArrival] = Service[NextArrival];
                NextArrival = NextArrival + 1;
            }
        
            # Job departure
            if(!is.na(CurrentTask)) {
                Remaining[CurrentTask] = Remaining[CurrentTask] - dt;
                ExpectedRemaining[CurrentTask] = ExpectedRemaining[CurrentTask] - dt;
                if(Remaining[CurrentTask] == 0) {
                    # CurrentTask completed
                    Completion[CurrentTask] = t;
                    Remaining[CurrentTask] = NA;
                    CurrentTask = NA;
                } else {
                    # CurrentTask rejected
                    if(Remaining[CurrentTask] + t > AbsDeadline[CurrentTask]) {
                      Completion[CurrentTask] = NA;
                      Remaining[CurrentTask] = NA;
                      CurrentTask = NA;
                    }
                }
            }
            
            
            # Scheduling discipline: Let's find the new task to process
            WaitingList=(1:NextArrival)[!is.na(Remaining)];
            
            if (debug==1){
                print(paste("Simulation time: ", t));
                print(paste("Different of jobs arrived: ", NextArrival));
                cat("WaitingList:", WaitingList, "\n");
            }
            if(length(WaitingList)>0) {
        
              if (scheduling_discipline=="FCFS"){
                CurrentTask = head(WaitingList,n=1);
              }
              
              if (scheduling_discipline=="EDF"){
                  CurrentTask = WaitingList[which.min(AbsDeadline[WaitingList])];
              }
        
              if (scheduling_discipline=="EDF-NP"){
                  if (is.na(CurrentTask)) {
                    CurrentTask = WaitingList[which.min(AbsDeadline[WaitingList])];
                  }
              }

              if (scheduling_discipline=="SRPT"){
                    CurrentTask = which.min(Remaining);
              }
                
              if (scheduling_discipline=="SRT2D"){
                    CurrentTask = WaitingList[which.min(AbsDeadline[WaitingList]-(t+Remaining[WaitingList]))];
              }
                
              if (scheduling_discipline=="SERT2D"){
                 CurrentTask = WaitingList[which.min(AbsDeadline[WaitingList]-(t+ExpectedRemaining[WaitingList]))];
              }

            }
    
            if (debug==1){
                print(paste("Current Task = ", CurrentTask))
                #    readline(prompt="Press [enter] to proceed")
            }
        
      } # while


## Step 2: Performance measures implementation 

      #MDR = sum(is.na(Completion))/length(Arrival);
      MDR = mean(is.na(Completion))
      ResponseTime = mean(Completion - Arrival, na.rm = TRUE)
      
      #Average nulber of job in the system
      AvgJobs = AvgJobs / (tail(Completion[!is.na(Completion)], n = 1) - Arrival[1])
  
      if (debug>=0){
          cat(paste(" Little law verification: ", round(AvgJobs,digits = 4), "=N=lambda*R=", 
                    round(lambda*ResponseTime,digits = 4),"\n"));
      }        
        
      # Simulation completed.
      Results[scheduling_discipline,1]=ResponseTime;
      Results[scheduling_discipline,2]=MDR;
        
      cat(scheduling_discipline," completed.\n");
    
    } # loop scheduling discipline
    
    cat("\nResults:\n")
    print(Results)
    
} # end function

Step 3 : Code correcteness with SRT2D

#knitr::opts_chunk$set(echo = TRUE)

set.seed(10)  # Set seed for reproducibility

debug <- 1  # Debug mode (1 = on, 0 = off)
N <- 10  # Number of jobs to simulate

# Generate Service times with 85% between 0-10 and 15% between 90-110
Service <- sample(c(runif(N * 0.85, min = 0, max = 10), runif(N * 0.15, min = 90, max = 110)), N, replace = TRUE)

x <- 5  # Constant multiplier for deadlines
Deadline <- Service * x  # Relative deadlines

rho <- 0.9  # Load
ES <- 0.85 * ((10 + 0) / 2) + 0.15 * ((90 + 110) / 2)  # Expected service time
lambda <- rho / ES  # Arrival rate

# Generate interarrival times and arrival times
Interarrival <- rexp(N, rate = lambda)
Arrival <- cumsum(Interarrival)
AbsDeadline <- Arrival + Deadline  # Absolute deadlines

# Initialize tracking variables
Remaining <- rep(NA, N)
ExpectedRemaining <- rep(NA, N)
Completion <- rep(NA, N)
WaitingList <- numeric(0)
t <- 0  # Current time
NextArrival <- 1
CurrentTask <- NA
AvgJobs <- 0
ResponseTime <- 0  # Placeholder for Little's Law verification

while (TRUE) {
    if (debug == 1) {
        print("*********************")
        cat("InterArrival:  ", Interarrival, '\n')
        cat("Arrival:     ", Arrival, '\n')
        cat("AbsDeadline: ", AbsDeadline, '\n')
        cat("Service:     ", Service, '\n')
        cat("Remaining:   ", Remaining, '\n')
        cat("Current Task =", CurrentTask ,'\n')
    }
    
    # Determine next event
    dtA <- if (length(Arrival[Arrival > t]) > 0) head(Arrival[Arrival > t], n = 1) - t else NA
    dtC <- if (!is.na(CurrentTask)) Remaining[CurrentTask] else NA
    
    if (is.na(dtA) & is.na(dtC)) break
    dt <- min(dtA, dtC, na.rm = TRUE)
    t <- t + dt
    AvgJobs <- AvgJobs + dt * sum(!is.na(Remaining))
    
    # Handle job arrival
    if ((NextArrival <= N) & (Arrival[NextArrival] == t)) {
        Remaining[NextArrival] <- Service[NextArrival]
        ExpectedRemaining[NextArrival] <- Service[NextArrival]
        WaitingList <- c(WaitingList, NextArrival)
        NextArrival <- NextArrival + 1
    }
    
    # Handle job completion
    if (!is.na(CurrentTask)) {
        Remaining[CurrentTask] <- Remaining[CurrentTask] - dt
        ExpectedRemaining[CurrentTask] <- ExpectedRemaining[CurrentTask] - dt
        
        if (Remaining[CurrentTask] == 0) {
            Completion[CurrentTask] <- t
            Remaining[CurrentTask] <- NA
            CurrentTask <- NA
        } else if (Remaining[CurrentTask] + t > AbsDeadline[CurrentTask]) {
            Completion[CurrentTask] <- NA  # Task rejected
            Remaining[CurrentTask] <- NA
            CurrentTask <- NA
        }
    }
    
    # Print current queue
    cat("Updated Completion Times: ", Completion, "\n")
    cat("Waiting List: ", WaitingList, "\n\n")
    
    # Select next task using Earliest Deadline First (EDF)
    if (length(WaitingList) > 0) {
        valid_tasks <- WaitingList[!is.na(Remaining[WaitingList])]
        if (length(valid_tasks) > 0) {
            CurrentTask <- valid_tasks[which.min(AbsDeadline[valid_tasks] - (t + Remaining[valid_tasks]))]
        }
    }
    
    #if (debug == 1) {
    #    cat(paste("Current Task =", CurrentTask))
    #}
}
## [1] "*********************"
## InterArrival:   11.583 23.88579 1.522762 54.94488 37.31759 6.265876 13.80236 7.420285 17.00593 29.86529 
## Arrival:      11.583 35.46879 36.99156 91.93644 129.254 135.5199 149.3223 156.7426 173.7485 203.6138 
## AbsDeadline:  32.92839 49.08405 50.71808 107.2749 142.8693 149.1352 163.0488 168.0144 187.475 214.8856 
## Service:      4.269077 2.723051 2.745305 3.067685 2.723051 2.723051 2.745305 2.254366 2.745305 2.254366 
## Remaining:    NA NA NA NA NA NA NA NA NA NA 
## Current Task = NA 
## Updated Completion Times:  NA NA NA NA NA NA NA NA NA NA 
## Waiting List:  1 
## 
## [1] "*********************"
## InterArrival:   11.583 23.88579 1.522762 54.94488 37.31759 6.265876 13.80236 7.420285 17.00593 29.86529 
## Arrival:      11.583 35.46879 36.99156 91.93644 129.254 135.5199 149.3223 156.7426 173.7485 203.6138 
## AbsDeadline:  32.92839 49.08405 50.71808 107.2749 142.8693 149.1352 163.0488 168.0144 187.475 214.8856 
## Service:      4.269077 2.723051 2.745305 3.067685 2.723051 2.723051 2.745305 2.254366 2.745305 2.254366 
## Remaining:    4.269077 NA NA NA NA NA NA NA NA NA 
## Current Task = 1 
## Updated Completion Times:  15.85208 NA NA NA NA NA NA NA NA NA 
## Waiting List:  1 
## 
## [1] "*********************"
## InterArrival:   11.583 23.88579 1.522762 54.94488 37.31759 6.265876 13.80236 7.420285 17.00593 29.86529 
## Arrival:      11.583 35.46879 36.99156 91.93644 129.254 135.5199 149.3223 156.7426 173.7485 203.6138 
## AbsDeadline:  32.92839 49.08405 50.71808 107.2749 142.8693 149.1352 163.0488 168.0144 187.475 214.8856 
## Service:      4.269077 2.723051 2.745305 3.067685 2.723051 2.723051 2.745305 2.254366 2.745305 2.254366 
## Remaining:    NA NA NA NA NA NA NA NA NA NA 
## Current Task = NA 
## Updated Completion Times:  15.85208 NA NA NA NA NA NA NA NA NA 
## Waiting List:  1 2 
## 
## [1] "*********************"
## InterArrival:   11.583 23.88579 1.522762 54.94488 37.31759 6.265876 13.80236 7.420285 17.00593 29.86529 
## Arrival:      11.583 35.46879 36.99156 91.93644 129.254 135.5199 149.3223 156.7426 173.7485 203.6138 
## AbsDeadline:  32.92839 49.08405 50.71808 107.2749 142.8693 149.1352 163.0488 168.0144 187.475 214.8856 
## Service:      4.269077 2.723051 2.745305 3.067685 2.723051 2.723051 2.745305 2.254366 2.745305 2.254366 
## Remaining:    NA 2.723051 NA NA NA NA NA NA NA NA 
## Current Task = 2 
## Updated Completion Times:  15.85208 NA NA NA NA NA NA NA NA NA 
## Waiting List:  1 2 3 
## 
## [1] "*********************"
## InterArrival:   11.583 23.88579 1.522762 54.94488 37.31759 6.265876 13.80236 7.420285 17.00593 29.86529 
## Arrival:      11.583 35.46879 36.99156 91.93644 129.254 135.5199 149.3223 156.7426 173.7485 203.6138 
## AbsDeadline:  32.92839 49.08405 50.71808 107.2749 142.8693 149.1352 163.0488 168.0144 187.475 214.8856 
## Service:      4.269077 2.723051 2.745305 3.067685 2.723051 2.723051 2.745305 2.254366 2.745305 2.254366 
## Remaining:    NA 1.200288 2.745305 NA NA NA NA NA NA NA 
## Current Task = 2 
## Updated Completion Times:  15.85208 38.19184 NA NA NA NA NA NA NA NA 
## Waiting List:  1 2 3 
## 
## [1] "*********************"
## InterArrival:   11.583 23.88579 1.522762 54.94488 37.31759 6.265876 13.80236 7.420285 17.00593 29.86529 
## Arrival:      11.583 35.46879 36.99156 91.93644 129.254 135.5199 149.3223 156.7426 173.7485 203.6138 
## AbsDeadline:  32.92839 49.08405 50.71808 107.2749 142.8693 149.1352 163.0488 168.0144 187.475 214.8856 
## Service:      4.269077 2.723051 2.745305 3.067685 2.723051 2.723051 2.745305 2.254366 2.745305 2.254366 
## Remaining:    NA NA 2.745305 NA NA NA NA NA NA NA 
## Current Task = 3 
## Updated Completion Times:  15.85208 38.19184 40.93715 NA NA NA NA NA NA NA 
## Waiting List:  1 2 3 
## 
## [1] "*********************"
## InterArrival:   11.583 23.88579 1.522762 54.94488 37.31759 6.265876 13.80236 7.420285 17.00593 29.86529 
## Arrival:      11.583 35.46879 36.99156 91.93644 129.254 135.5199 149.3223 156.7426 173.7485 203.6138 
## AbsDeadline:  32.92839 49.08405 50.71808 107.2749 142.8693 149.1352 163.0488 168.0144 187.475 214.8856 
## Service:      4.269077 2.723051 2.745305 3.067685 2.723051 2.723051 2.745305 2.254366 2.745305 2.254366 
## Remaining:    NA NA NA NA NA NA NA NA NA NA 
## Current Task = NA 
## Updated Completion Times:  15.85208 38.19184 40.93715 NA NA NA NA NA NA NA 
## Waiting List:  1 2 3 4 
## 
## [1] "*********************"
## InterArrival:   11.583 23.88579 1.522762 54.94488 37.31759 6.265876 13.80236 7.420285 17.00593 29.86529 
## Arrival:      11.583 35.46879 36.99156 91.93644 129.254 135.5199 149.3223 156.7426 173.7485 203.6138 
## AbsDeadline:  32.92839 49.08405 50.71808 107.2749 142.8693 149.1352 163.0488 168.0144 187.475 214.8856 
## Service:      4.269077 2.723051 2.745305 3.067685 2.723051 2.723051 2.745305 2.254366 2.745305 2.254366 
## Remaining:    NA NA NA 3.067685 NA NA NA NA NA NA 
## Current Task = 4 
## Updated Completion Times:  15.85208 38.19184 40.93715 95.00412 NA NA NA NA NA NA 
## Waiting List:  1 2 3 4 
## 
## [1] "*********************"
## InterArrival:   11.583 23.88579 1.522762 54.94488 37.31759 6.265876 13.80236 7.420285 17.00593 29.86529 
## Arrival:      11.583 35.46879 36.99156 91.93644 129.254 135.5199 149.3223 156.7426 173.7485 203.6138 
## AbsDeadline:  32.92839 49.08405 50.71808 107.2749 142.8693 149.1352 163.0488 168.0144 187.475 214.8856 
## Service:      4.269077 2.723051 2.745305 3.067685 2.723051 2.723051 2.745305 2.254366 2.745305 2.254366 
## Remaining:    NA NA NA NA NA NA NA NA NA NA 
## Current Task = NA 
## Updated Completion Times:  15.85208 38.19184 40.93715 95.00412 NA NA NA NA NA NA 
## Waiting List:  1 2 3 4 5 
## 
## [1] "*********************"
## InterArrival:   11.583 23.88579 1.522762 54.94488 37.31759 6.265876 13.80236 7.420285 17.00593 29.86529 
## Arrival:      11.583 35.46879 36.99156 91.93644 129.254 135.5199 149.3223 156.7426 173.7485 203.6138 
## AbsDeadline:  32.92839 49.08405 50.71808 107.2749 142.8693 149.1352 163.0488 168.0144 187.475 214.8856 
## Service:      4.269077 2.723051 2.745305 3.067685 2.723051 2.723051 2.745305 2.254366 2.745305 2.254366 
## Remaining:    NA NA NA NA 2.723051 NA NA NA NA NA 
## Current Task = 5 
## Updated Completion Times:  15.85208 38.19184 40.93715 95.00412 131.9771 NA NA NA NA NA 
## Waiting List:  1 2 3 4 5 
## 
## [1] "*********************"
## InterArrival:   11.583 23.88579 1.522762 54.94488 37.31759 6.265876 13.80236 7.420285 17.00593 29.86529 
## Arrival:      11.583 35.46879 36.99156 91.93644 129.254 135.5199 149.3223 156.7426 173.7485 203.6138 
## AbsDeadline:  32.92839 49.08405 50.71808 107.2749 142.8693 149.1352 163.0488 168.0144 187.475 214.8856 
## Service:      4.269077 2.723051 2.745305 3.067685 2.723051 2.723051 2.745305 2.254366 2.745305 2.254366 
## Remaining:    NA NA NA NA NA NA NA NA NA NA 
## Current Task = NA 
## Updated Completion Times:  15.85208 38.19184 40.93715 95.00412 131.9771 NA NA NA NA NA 
## Waiting List:  1 2 3 4 5 6 
## 
## [1] "*********************"
## InterArrival:   11.583 23.88579 1.522762 54.94488 37.31759 6.265876 13.80236 7.420285 17.00593 29.86529 
## Arrival:      11.583 35.46879 36.99156 91.93644 129.254 135.5199 149.3223 156.7426 173.7485 203.6138 
## AbsDeadline:  32.92839 49.08405 50.71808 107.2749 142.8693 149.1352 163.0488 168.0144 187.475 214.8856 
## Service:      4.269077 2.723051 2.745305 3.067685 2.723051 2.723051 2.745305 2.254366 2.745305 2.254366 
## Remaining:    NA NA NA NA NA 2.723051 NA NA NA NA 
## Current Task = 6 
## Updated Completion Times:  15.85208 38.19184 40.93715 95.00412 131.9771 138.243 NA NA NA NA 
## Waiting List:  1 2 3 4 5 6 
## 
## [1] "*********************"
## InterArrival:   11.583 23.88579 1.522762 54.94488 37.31759 6.265876 13.80236 7.420285 17.00593 29.86529 
## Arrival:      11.583 35.46879 36.99156 91.93644 129.254 135.5199 149.3223 156.7426 173.7485 203.6138 
## AbsDeadline:  32.92839 49.08405 50.71808 107.2749 142.8693 149.1352 163.0488 168.0144 187.475 214.8856 
## Service:      4.269077 2.723051 2.745305 3.067685 2.723051 2.723051 2.745305 2.254366 2.745305 2.254366 
## Remaining:    NA NA NA NA NA NA NA NA NA NA 
## Current Task = NA 
## Updated Completion Times:  15.85208 38.19184 40.93715 95.00412 131.9771 138.243 NA NA NA NA 
## Waiting List:  1 2 3 4 5 6 7 
## 
## [1] "*********************"
## InterArrival:   11.583 23.88579 1.522762 54.94488 37.31759 6.265876 13.80236 7.420285 17.00593 29.86529 
## Arrival:      11.583 35.46879 36.99156 91.93644 129.254 135.5199 149.3223 156.7426 173.7485 203.6138 
## AbsDeadline:  32.92839 49.08405 50.71808 107.2749 142.8693 149.1352 163.0488 168.0144 187.475 214.8856 
## Service:      4.269077 2.723051 2.745305 3.067685 2.723051 2.723051 2.745305 2.254366 2.745305 2.254366 
## Remaining:    NA NA NA NA NA NA 2.745305 NA NA NA 
## Current Task = 7 
## Updated Completion Times:  15.85208 38.19184 40.93715 95.00412 131.9771 138.243 152.0676 NA NA NA 
## Waiting List:  1 2 3 4 5 6 7 
## 
## [1] "*********************"
## InterArrival:   11.583 23.88579 1.522762 54.94488 37.31759 6.265876 13.80236 7.420285 17.00593 29.86529 
## Arrival:      11.583 35.46879 36.99156 91.93644 129.254 135.5199 149.3223 156.7426 173.7485 203.6138 
## AbsDeadline:  32.92839 49.08405 50.71808 107.2749 142.8693 149.1352 163.0488 168.0144 187.475 214.8856 
## Service:      4.269077 2.723051 2.745305 3.067685 2.723051 2.723051 2.745305 2.254366 2.745305 2.254366 
## Remaining:    NA NA NA NA NA NA NA NA NA NA 
## Current Task = NA 
## Updated Completion Times:  15.85208 38.19184 40.93715 95.00412 131.9771 138.243 152.0676 NA NA NA 
## Waiting List:  1 2 3 4 5 6 7 8 
## 
## [1] "*********************"
## InterArrival:   11.583 23.88579 1.522762 54.94488 37.31759 6.265876 13.80236 7.420285 17.00593 29.86529 
## Arrival:      11.583 35.46879 36.99156 91.93644 129.254 135.5199 149.3223 156.7426 173.7485 203.6138 
## AbsDeadline:  32.92839 49.08405 50.71808 107.2749 142.8693 149.1352 163.0488 168.0144 187.475 214.8856 
## Service:      4.269077 2.723051 2.745305 3.067685 2.723051 2.723051 2.745305 2.254366 2.745305 2.254366 
## Remaining:    NA NA NA NA NA NA NA 2.254366 NA NA 
## Current Task = 8 
## Updated Completion Times:  15.85208 38.19184 40.93715 95.00412 131.9771 138.243 152.0676 158.9969 NA NA 
## Waiting List:  1 2 3 4 5 6 7 8 
## 
## [1] "*********************"
## InterArrival:   11.583 23.88579 1.522762 54.94488 37.31759 6.265876 13.80236 7.420285 17.00593 29.86529 
## Arrival:      11.583 35.46879 36.99156 91.93644 129.254 135.5199 149.3223 156.7426 173.7485 203.6138 
## AbsDeadline:  32.92839 49.08405 50.71808 107.2749 142.8693 149.1352 163.0488 168.0144 187.475 214.8856 
## Service:      4.269077 2.723051 2.745305 3.067685 2.723051 2.723051 2.745305 2.254366 2.745305 2.254366 
## Remaining:    NA NA NA NA NA NA NA NA NA NA 
## Current Task = NA 
## Updated Completion Times:  15.85208 38.19184 40.93715 95.00412 131.9771 138.243 152.0676 158.9969 NA NA 
## Waiting List:  1 2 3 4 5 6 7 8 9 
## 
## [1] "*********************"
## InterArrival:   11.583 23.88579 1.522762 54.94488 37.31759 6.265876 13.80236 7.420285 17.00593 29.86529 
## Arrival:      11.583 35.46879 36.99156 91.93644 129.254 135.5199 149.3223 156.7426 173.7485 203.6138 
## AbsDeadline:  32.92839 49.08405 50.71808 107.2749 142.8693 149.1352 163.0488 168.0144 187.475 214.8856 
## Service:      4.269077 2.723051 2.745305 3.067685 2.723051 2.723051 2.745305 2.254366 2.745305 2.254366 
## Remaining:    NA NA NA NA NA NA NA NA 2.745305 NA 
## Current Task = 9 
## Updated Completion Times:  15.85208 38.19184 40.93715 95.00412 131.9771 138.243 152.0676 158.9969 176.4938 NA 
## Waiting List:  1 2 3 4 5 6 7 8 9 
## 
## [1] "*********************"
## InterArrival:   11.583 23.88579 1.522762 54.94488 37.31759 6.265876 13.80236 7.420285 17.00593 29.86529 
## Arrival:      11.583 35.46879 36.99156 91.93644 129.254 135.5199 149.3223 156.7426 173.7485 203.6138 
## AbsDeadline:  32.92839 49.08405 50.71808 107.2749 142.8693 149.1352 163.0488 168.0144 187.475 214.8856 
## Service:      4.269077 2.723051 2.745305 3.067685 2.723051 2.723051 2.745305 2.254366 2.745305 2.254366 
## Remaining:    NA NA NA NA NA NA NA NA NA NA 
## Current Task = NA 
## Updated Completion Times:  15.85208 38.19184 40.93715 95.00412 131.9771 138.243 152.0676 158.9969 176.4938 NA 
## Waiting List:  1 2 3 4 5 6 7 8 9 10 
## 
## [1] "*********************"
## InterArrival:   11.583 23.88579 1.522762 54.94488 37.31759 6.265876 13.80236 7.420285 17.00593 29.86529 
## Arrival:      11.583 35.46879 36.99156 91.93644 129.254 135.5199 149.3223 156.7426 173.7485 203.6138 
## AbsDeadline:  32.92839 49.08405 50.71808 107.2749 142.8693 149.1352 163.0488 168.0144 187.475 214.8856 
## Service:      4.269077 2.723051 2.745305 3.067685 2.723051 2.723051 2.745305 2.254366 2.745305 2.254366 
## Remaining:    NA NA NA NA NA NA NA NA NA 2.254366 
## Current Task = 10 
## Updated Completion Times:  15.85208 38.19184 40.93715 95.00412 131.9771 138.243 152.0676 158.9969 176.4938 205.8681 
## Waiting List:  1 2 3 4 5 6 7 8 9 10 
## 
## [1] "*********************"
## InterArrival:   11.583 23.88579 1.522762 54.94488 37.31759 6.265876 13.80236 7.420285 17.00593 29.86529 
## Arrival:      11.583 35.46879 36.99156 91.93644 129.254 135.5199 149.3223 156.7426 173.7485 203.6138 
## AbsDeadline:  32.92839 49.08405 50.71808 107.2749 142.8693 149.1352 163.0488 168.0144 187.475 214.8856 
## Service:      4.269077 2.723051 2.745305 3.067685 2.723051 2.723051 2.745305 2.254366 2.745305 2.254366 
## Remaining:    NA NA NA NA NA NA NA NA NA NA 
## Current Task = NA
# Compute Average Number of Jobs in the System
if (any(!is.na(Completion))) {
    ResponseTime <- mean(Completion - Arrival, na.rm = TRUE)
    AvgJobs <- AvgJobs / (tail(Completion[!is.na(Completion)], n = 1) - Arrival[1])
    cat(paste("Little's Law Verification:", round(AvgJobs, 4), "= N = lambda * R =", round(lambda * ResponseTime, 4), "\n"))
} else {
    cat("No completed jobs to verify Little's Law.\n")
}
## Little's Law Verification: 0.1516 = N = lambda * R = 0.1377

Step 4: Performance evaluation

#knitr::opts_chunk$set(echo = TRUE)

set.seed(15); # Set seed for reproducibility

N = 1e4; # number of jobs to simulate;

#mu=1; # Service rate
Service=sample(c(runif(N*0.85, min = 0, max = 10), runif(N*0.15, min = 90, max = 110)), N, replace = TRUE); # Service times
#x=runif(N,3,7);  
x=5;
Deadline=Service*x; # Relative deadlines

rho=0.9; # load
ES = 0.85*((10+0)/2) + 0.15*((90+110)/2);  # Experance of our random variable S
run_simulation(Service, ES, Deadline, rho) 
##  Little law verification:  7.0777 =N=lambda*R= 6.7719 
## FCFS  completed.
##  Little law verification:  2.0655 =N=lambda*R= 1.8543 
## EDF  completed.
##  Little law verification:  3.3526 =N=lambda*R= 3.2116 
## EDF-NP  completed.
##  Little law verification:  1.9445 =N=lambda*R= 1.4547 
## SRPT  completed.
##  Little law verification:  2.3319 =N=lambda*R= 2.0204 
## SRT2D  completed.
##  Little law verification:  2.2612 =N=lambda*R= 1.9382 
## SERT2D  completed.
## 
## Results:
##               RT    MDR
## FCFS   144.84411 0.1348
## EDF     39.66084 0.0112
## EDF-NP  68.69344 0.1055
## SRPT    31.11344 0.0151
## SRT2D   43.21363 0.0206
## SERT2D  41.45542 0.0164

Comments

  • FCFS : Worst RT, highest MDR—poor choice for time-sensitive tasks.
  • EDF : Best MDR (lowest number of missed deadlines), meaning it effectively prioritizes urgent jobs.
  • EDF-NP : Performs worse than EDF due to lack of preemption but still better than FCFS.
  • SRPT : Best response time, meaning jobs are completed quickly. However, slightly higher MDR than EDF.
  • SRT2D : A balanced approach but does not outperform EDF in MDR or SRPT in RT.
  • SERT2D : Performs similarly to SRT2D, with marginally better MDR.

If the goal is minimizing missed deadlines, EDF is the best scheduling discipline. If the goal is minimizing response time, SRPT is the best choice.