QuestionQuestion

Assignment 4 – Simulation of CPU Scheduling Algorithms
Introduction

In this assignment, you will simulate various CPU scheduling algorithms. The efficiency of Operating System depends on two main factors:
• Algorithms used for scheduling; and
• The environment in which this algorithms are implemented.
One of the methods used in evaluation of CPU scheduling algorithms is simulation on historical logs or samples generated by a random number generator with certain parameters of processes in a given environment. This is more popular, and random number generators generate samples using queuing theory and probability theory.

Instructions
Design and implement a simulation program that will allow evaluation of following CPU scheduling algorithms:
• FCFS without preemption
• SJF without preemption
• Priority with preemption
• RR without preemption.
This program should include the above four algorithms, OR you can use one program for each scheduling algorithm.

Input data for simulation could be obtained by using SampleGenerator.java. Use the seven Java files in the Assignment 4 Sample Process Generator Java File Folder located on your Home Page. SampleGenerator asks to enter:
• Minimum priority (with 0 as the highest)
• Maximum CPU burst time in ms
• Average process arrival interval in ms
• Total simulation time in ms, and
• Output file name
It will generate samples (records of process number, arrival time, priority, and CPU burst time) and save them in the output file. SampleReader.java could be used to read samples, and SampleReaderTest.java contains sample codes how to read the samples from the sample file generated by SampleGenerator.
You need to generate samples for at least 3600000 ms (1 hr).
Your simulation program will generate the followings at every minute for in total 4 different algorithms:

• Average waiting time for the past 10 minutes
• Average turnaround time for the past 10 minutes
Algorithm could be:
// Ready queue is a sort of priority queue: arrival time for FCFS, priority for
Priority, CPU burst time for SJF
// and the entering time into the queue for RR
For (current = 0; current < SIM_TIME; current++) {
If there is no sample process
Read a sample process from the sample file;
If the arrival time of the sample process == current
Put the sample process into the priority queue;
If there is a running process
If the terminating time of the process == current
Remove the process from the CPU
Else
If RR && the timer expires // RR scheduling
Remove the process from the CPU
Put the process into the priority queue
If Preemptive // Preemptive scheduling
Get a sample process from the priority queue
Compare the priority
If the priority of the running process on the CPU is lower
Remove the process from the CPU
Put the process into the priority queue
Dispatch the new sample process to the CPU
Else
Put the sample process back into the priority queue
Else
If the priority queue is not empty
Get a sample process from the priority queue;
Dispatch the process to the CPU;

Again, the following Java files can be found in the Assignment 4 Sample Process Generator Java File Folder folder on the HomePage in your course management system:
• SampleGenerator.java
• SampleReader.java
• PriorityQueue.java
• QueueItem.java
• ProcessControlBlock.java – you can extend this class to keep some information for your simulation
• KeyboardIn.java
• SampleReaderTest.java public class SampleReaderT

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.io.*;
import java.util.*;

public class Assignment4 {

public static int RR_TIMER = 100;

private static int[] waitingTimes;
private static int[] turnaroundTimes;

private static int count = 0;

// enum to specify simulation type
public enum SimulationType {
    FCFS, SJF, Priority, RR
}

// method to get sample process from file
public static ProcessControlBlock getSampleProcess(SampleReader sr) {
      int process = sr.readProcess();
      int arrival = sr.readArrival();
      int priority = sr.readPriority();
      int burst = sr.readBurst();
      ProcessControlBlock sampleProcess = null;
      if (process >= 0 && arrival >= 0 && priority >= 0 && burst >= 0) {
          sampleProcess = new ProcessControlBlock(process, arrival, priority, burst);
          //System.out.println((count++) +") " + process + " " + arrival + " " + priority + " " + burst);
      }
      return sampleProcess;
}

// method to run simulation (supports all four simulation types)
public static void simulate(String file, int time, SimulationType type) {
      SampleReader sr = new SampleReader(file);
      PriorityQueue readyQueue = new PriorityQueue();
      ProcessControlBlock sampleProcess = null;
      ProcessControlBlock runningProcess = null;
      waitingTimes = new int[time];
      turnaroundTimes = new int[time];
      int runningTerminationTime = -1;
      int numP = 0;
      sampleProcess = getSampleProcess(sr);
      for (int current = 0; current < time; current++) {
          waitingTimes[current] = -1;
          turnaroundTimes[current] = -1;
          while (sampleProcess != null && sampleProcess.getArrivalTime() == current) {
             if (type == SimulationType.FCFS) {
                  readyQueue.putQueue(sampleProcess, sampleProcess.getArrivalTime());
             } else if (type == SimulationType.SJF) {
                  readyQueue.putQueue(sampleProcess, sampleProcess.getCpuBurstTime());
             } else if (type == SimulationType.Priority) {
                  readyQueue.putQueue(sampleProcess, sampleProcess.getPriority());
             } else if (type == SimulationType.RR) {
                  readyQueue.putQueue(sampleProcess, current);
             }
             sampleProcess = getSampleProcess(sr);
          }
          if (runningProcess != null) {
             if (runningTerminationTime == current) {
                  turnaroundTimes[current] = current - runningProcess.getArrivalTime();
                  //System.out.println("COMPLETE @ t=" + current + ": " + runningProcess.getProcessNo() + " " + runningProcess.getArrivalTime() + " " + runningProcess.getPriority() + " " + runningProcess.getCpuBurstTime());
                  //System.out.println("TURNAROUND @ t=" + current + ": " + turnaroundTimes[current]);
                  runningProcess = null;
                  runningTerminationTime...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

50% discount

Hours
Minutes
Seconds
$100.00 $50.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Operating Systems Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats