CODE IN C TO GET THIS RESULT
CPU SCHEDULING:
proj2 input.1 SRTF
OUTPUT
Schdeuling algorithm: SRTF
Total 6 tasks are read from "input.1". press 'enter' to
start...
==================================================================
<system time 0> process 2 is running
<system time 1> process 2 is running
<system time 2> process 2 is running
<system time 3> process 3 is running
<system time 4> process 3 is running
<system time 5> process 3 is running
<system time 6> process 3 is running
<system time 7> process 3 is running
<system time 8> process 3 is finished.......
<system time 8> process 4 is running
<system time 9> process 4 is running
<system time 10> process 4 is running
<system time 11> process 4 is running
<system time 12> process 4 is finished.......
<system time 12> process 2 is running
<system time 13> process 2 is running
<system time 14> process 2 is running
<system time 15> process 2 is running
<system time 16> process 2 is running
<system time 17> process 2 is running
<system time 18> process 2 is finished.......
<system time 18> process 5 is running
<system time 19> process 5 is running
<system time 20> process 5 is running
<system time 21> process 5 is running
<system time 22> process 5 is running
<system time 23> process 5 is running
<system time 24> process 5 is finished.......
<system time 24> process 6 is running
<system time 25> process 6 is running
<system time 26> process 6 is running
<system time 27> process 6 is running
<system time 28> process 6 is running
<system time 29> process 6 is running
<system time 30> process 6 is running
<system time 31> process 6 is finished.......
<system time 31> process 1 is running
<system time 32> process 1 is running
<system time 33> process 1 is running
<system time 34> process 1 is running
<system time 35> process 1 is running
<system time 36> process 1 is running
<system time 37> process 1 is running
<system time 38> process 1 is running
<system time 39> process 1 is running
<system time 40> process 1 is running
<system time 41> process 1 is finished.......
<system time 41> All processes finish
....................
============================================================
Avarage cpu usage : 100.00 %
Avarage waiting time : 10.50
Avarage response time : 9.00
Avarage turnaround time: 17.33
============================================================
Program Files:
srtf_scheduler.c
// including necessary libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdbool.h>
/*
Function to perform Shortest remaining time scheduling
*/
void srtf(int *pid, int *arrival, int *burst, int num_lines);
// function to get number of lines
int get_num_lines(char *file);
// fuction to populate arrays
void populate_arrays(char *filename, int *pid, int *arrival_time,
int *burst_time);
// helper functions
void insert(int queue[], int val, int size);
int delete(int queue[]);
void display(int queue[], int val);
int front = - 1;
int rear = - 1;
int main(int argc, char *argv[]){
char *data;
char *scheduler;
char *quantum;
int num_lines;
// reading command line args
if (argc == 3){
scheduler = argv[2];
data = argv[1];
// check if the argument is valid one
if (strcmp(scheduler, "SRTF") == 0){
num_lines = get_num_lines(data);
int pid[num_lines];
int arrival_time[num_lines];
int burst_time[num_lines];
populate_arrays(data, pid, arrival_time, burst_time);
// printing results
printf("\nScheduling algorithm: SRTF\n");
printf("Total %d tasks read from \"%s\".. press 'ENTER' to
start...\n", num_lines,data);
getchar();
printf("==============================================\n");
srtf(pid,arrival_time,burst_time,num_lines);
}
else{
// print usage to stdout
printf("Usage: proj2 input_file SRTF\n");
}
}
else{
printf("Invalid format.\n");
printf("Usage: proj2 input_file SRTF \n");
}
}
void srtf(int *pid, int *arrival, int *burst, int num_lines){
/*
Function to perform Shortest remaining time scheduling
*/
int systime = 0;
int wasted_time = 0;
bool proc_running = false;
int proc_completed = 0;
int elapsed_time = 0;
float waiting_time = 0;
float response_time = 0;
float turnaround_time = 0;
int remaining_time[num_lines];
int wait_time[num_lines];
bool first_run[num_lines];
for (int i=0; i<num_lines;i++){
remaining_time[i] = -1;
first_run[i] = true;
wait_time[i] = 0;
}
int min_remaining_time = INT_MAX;
int curr_pid = -1;
while (proc_completed < num_lines){
proc_running = false;
for (int i=0; i<num_lines; i++){
if (systime >= arrival[i]){
if (remaining_time[i] != 0){
if(pid[i] != curr_pid){
if (remaining_time[i] == -1){
remaining_time[i] = burst[i];
}
}
}
}
}
int curr_index;
min_remaining_time = INT_MAX;
for (int i=0; i<num_lines; i++){
if (remaining_time[i] > 0){
if (remaining_time[i] < min_remaining_time){
min_remaining_time = remaining_time[i];
curr_pid = pid[i];
curr_index = i;
proc_running = true;
}
}
}
if (first_run[curr_index] == true){
first_run[curr_index] = false;
response_time += systime-arrival[curr_index];
}
if (proc_running == false){
wasted_time++;
printf("<system time %3d > no process is running\n",
systime);
}
else{
printf("<system time %3d > process %3d is running\n",
systime, curr_pid);
remaining_time[curr_index]--;
}
for (int i=0; i<num_lines; i++){
if (curr_pid != pid[i] && remaining_time[i] > 0
&& systime >= arrival[i]){
wait_time[i]++;
}
}
systime++;
if (remaining_time[curr_index] == 0 &&
proc_running==true){
printf("<system time %3d > process %3d is finished.......\n",
systime, curr_pid);
turnaround_time += (systime - arrival[curr_index]);
}
int completed = 0;
for (int i =0; i<num_lines; i++){
if (remaining_time[i] == 0){
completed++;
}
}
proc_completed = completed;
}
printf("<system time %3d > All processes
finished.............\n\n", systime);
for (int i=0; i<num_lines; i++){
waiting_time += wait_time[i];
}
printf("==============================================\n");
printf("Average cpu usage :
%0.2f\n",(((float)systime-wasted_time)/systime)*100);
printf("Average waiting time : %0.2f s\n",
waiting_time/num_lines);
printf("Average response time : %0.2f s\n",
response_time/num_lines);
printf("Average turnaround time : %0.2f s\n",
turnaround_time/num_lines);
printf("==============================================\n");
}
void populate_arrays(char *filename, int *pid, int *arrival, int
*burst){
char line[256];
FILE *file = fopen(filename, "r");
int i = 0;
while (fgets(line, sizeof(line), file)){
sscanf(line,"%d %d %d", &pid[i], &arrival[i],
&burst[i]);
i++;
}
}
int get_num_lines(char *filename){
int num_lines = 0;
FILE *file = fopen(filename, "r");
char line[256];
while (fgets(line, sizeof(line), file)){
num_lines++;
}
return num_lines;
}
void insert(int queue[], int item,int size)
{
if ((front == 0 && rear == size - 1) || (front == rear +
1))
{
printf("queue is full");
return;
}
else if (rear == -1)
{
rear++;
front++;
}
else if (rear == size-1 && front > 0)
{
rear = 0;
}
else
{
rear++;
}
queue[rear] = item;
}
void display(int queue[], int size)
{
int i;
printf("\n");
if (front > rear)
{
for (i = front; i < size; i++)
{
printf("%d ", queue[i]);
}
for (i = 0; i <= rear; i++)
printf("%d ", queue[i]);
}
else
{
for (i = front; i <= rear; i++)
printf("%d ", queue[i]);
}
printf("\n");
}
int delete(int queue[])
{
int next;
if (front == -1)
{
}
else if (front == rear)
{
next = queue[front];
front = -1;
rear = -1;
}
else
{
next = queue[front];
front++;
}
return next;
}
input.1
1 0 10
2 0 9
3 3 5
4 7 4
5 10 6
6 10 7
output:
clang -o proj2 srtf.c
./proj2 input.1 SRTF
Scheduling algorithm: SRTF
Total 6 tasks read from "input.1".. press 'ENTER' to start...
==============================================
<system time 0 > process 2 is running
<system time 1 > process 2 is running
<system time 2 > process 2 is running
<system time 3 > process 3 is running
<system time 4 > process 3 is running
<system time 5 > process 3 is running
<system time 6 > process 3 is running
<system time 7 > process 3 is running
<system time 8 > process 3 is finished.......
<system time 8 > process 4 is running
<system time 9 > process 4 is running
<system time 10 > process 4 is running
<system time 11 > process 4 is running
<system time 12 > process 4 is finished.......
<system time 12 > process 2 is running
<system time 13 > process 2 is running
<system time 14 > process 2 is running
<system time 15 > process 2 is running
<system time 16 > process 2 is running
<system time 17 > process 2 is running
<system time 18 > process 2 is finished.......
<system time 18 > process 5 is running
<system time 19 > process 5 is running
<system time 20 > process 5 is running
<system time 21 > process 5 is running
<system time 22 > process 5 is running
<system time 23 > process 5 is running
<system time 24 > process 5 is finished.......
<system time 24 > process 6 is running
<system time 25 > process 6 is running
<system time 26 > process 6 is running
<system time 27 > process 6 is running
<system time 28 > process 6 is running
<system time 29 > process 6 is running
<system time 30 > process 6 is running
<system time 31 > process 6 is finished.......
<system time 31 > process 1 is running
<system time 32 > process 1 is running
<system time 33 > process 1 is running
<system time 34 > process 1 is running
<system time 35 > process 1 is running
<system time 36 > process 1 is running
<system time 37 > process 1 is running
<system time 38 > process 1 is running
<system time 39 > process 1 is running
<system time 40 > process 1 is running
<system time 41 > process 1 is finished.......
<system time 41 > All processes finished.............
==============================================
Average cpu usage : 100.00
Average waiting time : 10.50 s
Average response time : 9.00 s
Average turnaround time : 17.33 s
==============================================
Incase you need other scheduling algorithms:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdbool.h>
void fcfs(int *pid, int *arrival_time, int *burst_time, int num_lines, int total_time);
void get_fcfs_order(int *pid,int num_lines, int *arrival, int *order);
void srtf(int *pid, int *arrival, int *burst, int num_lines);
void rr(int *pid, int *arrival, int *burst, int num_lines, int quantum);
int get_num_lines(char *file);
void populate_arrays(char *filename, int *pid, int *arrival_time, int *burst_time);
void insert(int queue[], int val, int size);
int delete(int queue[]);
void display(int queue[], int val);
int front = - 1;
int rear = - 1;
int main(int argc, char *argv[]){
char *data;
char *scheduler;
char *quantum;
int num_lines;
if (argc == 3){
scheduler = argv[2];
data = argv[1];
if (strcmp(scheduler, "SRTF") == 0){
num_lines = get_num_lines(data);
int pid[num_lines];
int arrival_time[num_lines];
int burst_time[num_lines];
populate_arrays(data, pid, arrival_time, burst_time);
printf("\nScheduling algorithm: SRTF\n");
printf("Total %d tasks read from \"%s\".\n", num_lines,data);
printf("==============================================\n");
srtf(pid,arrival_time,burst_time,num_lines);
}
else if (strcmp(scheduler, "FCFS") == 0){
num_lines = get_num_lines(data);
int pid[num_lines];
int arrival_time[num_lines];
int burst_time[num_lines];
int total_time = 0;
populate_arrays(data, pid, arrival_time, burst_time);
printf("\nScheduling algorithm: FCFS\n");
printf("Total %d tasks read from \"%s\".\n", num_lines,data);
printf("==============================================\n");
fcfs(pid, arrival_time, burst_time, num_lines, total_time);
}
else{
printf("Usage: proj2 input_file FCFS|RR|SRTF [quantum]\n");
}
}
else if (argc == 4){
scheduler = argv[2];
if (strcmp(scheduler, "RR") == 0){
int quant;
data = argv[1];
scheduler = argv[2];
quantum = argv[3];
quant = atoi(quantum);
num_lines = get_num_lines(data);
int pid[num_lines];
int arrival_time[num_lines];
int burst_time[num_lines];
populate_arrays(data, pid, arrival_time, burst_time);
printf("\nScheduling algorithm: RR\n");
printf("Total %d tasks read from \"%s\".\n", num_lines,data);
printf("==============================================\n");
rr(pid, arrival_time, burst_time, num_lines, quant);
}
else{
printf("Usage: proj2 input_file FCFS|RR|SRTF [quantum]\n");
}
}
else{
printf("Invalid format.\n");
printf("Usage: proj2 input_file FCFS|RR|SRTF [quantum]\n");
}
}
void get_fcfs_order(int *pid,int num_lines, int *arrival, int *order){
int min = INT_MAX;
int min_index;
int ctr = 0;
while (ctr < num_lines){
int min = INT_MAX;
for (int i=0; i<num_lines; i++){
if (arrival[i] < min){
min = arrival[i];
min_index = i;
}
}
order[ctr] = min_index;
arrival[min_index] = INT_MAX;
ctr++;
}
}
void fcfs(int *pid, int *arrival_orig, int *burst, int num_lines, int total_time){
int systime = 0;
int wasted_time = 0;
bool proc_running = false;
float waiting_time = 0;
float response_time = 0;
float turnaround_time = 0;
int min_arrival_time = INT_MAX;
int curr_pid = pid[0];
int proc_completed = 0;
int order[num_lines];
int arrival[num_lines];
for (int i=0; i < num_lines; i++){
arrival[i] = arrival_orig[i];
}
int curr_proc = 0;
int curr_index;
get_fcfs_order(pid,num_lines, arrival, order);
while (proc_completed < num_lines){
curr_index = order[curr_proc];
if (systime >= arrival_orig[curr_index]){
waiting_time += (systime - arrival_orig[curr_index]);
for (int i=0; i<burst[curr_index]; i++){
printf("<system time %3d > process %3d is running\n", systime, pid[curr_index]);
systime++;
}
printf("<system time %3d > process %3d is finished.......\n", systime, pid[curr_index]);
turnaround_time += (systime - arrival_orig[curr_index]);
curr_proc++;
}
else{
printf("no processes executing at time %d\n", systime);
systime++;
wasted_time++;
}
proc_completed++;
}
printf("<system time %3d > All processes finished.............\n\n", systime);
printf("==============================================\n");
printf("Average cpu usage : %.2f\n",(((float)systime-wasted_time)/systime)*100);
printf("Average waiting time : %.2f s\n", waiting_time/num_lines);
printf("Average response time : %.2f s\n", waiting_time/num_lines);
printf("Average turnaround time : %.2f s\n", turnaround_time/num_lines);
printf("==============================================\n");
}
void rr(int *pid, int *arrival, int *burst, int num_lines, int quantum){
int systime = 0;
int wasted_time = 0;
float waiting_time = 0;
float response_time = 0;
float turnaround_time = 0;
bool proc_running = false;
int proc_completed = 0;
int wait_time[num_lines];
bool first_run[num_lines];
int time_first_run[num_lines];
for (int i = 0; i < num_lines; i++){
wait_time[i] = 0;
first_run[i] = true;
}
int curr_pid;
while (proc_completed < num_lines){
proc_running = false;
for (int j=0; j<num_lines; j++){
if (systime >= arrival[j] && burst[j] > 0){
if (first_run[j]){
time_first_run[j] = systime;
response_time += systime;
first_run[j] = false;
}
proc_running = true;
curr_pid = pid[j];
for (int q=0; q<quantum; q++){
if (burst[j] == 0){
printf("<system time %3d > process %3d is finished......\n", systime, curr_pid);
systime++;
}
else{
burst[j]--;
printf("<system time %3d > process %3d is running.\n", systime, curr_pid);
systime++;
if (burst[j] == 0){
q = quantum;
int curr_turnaround = systime - time_first_run[j];
printf("<system time %3d > process %3d is finished......\n", systime, curr_pid);
turnaround_time += curr_turnaround;
}
}
for (int z=0; z<num_lines; z++){
if (z != j){
wait_time[j]++;
}
}
}
}
if (proc_running == false){
systime++;
wasted_time++;
}
if (burst[j] == 0){
proc_completed++;
burst[j] = -1;
}
}
}
for(int i=0; i<num_lines; i++){
waiting_time += wait_time[i];
}
printf("<system time %3d > All processes finished.............\n\n", systime);
printf("==============================================\n");
wasted_time = 0;
systime = 1;
printf("Average cpu usage : %.2f\n", (float)((systime-wasted_time)/systime)*100);
printf("Average waiting time : %.2f s\n", waiting_time/num_lines);
printf("Average response time : %.2f s\n", response_time/num_lines);
printf("Average turnaround time : %.2f s\n", turnaround_time/num_lines);
printf("==============================================\n");
}
void srtf(int *pid, int *arrival, int *burst, int num_lines){
int systime = 0;
int wasted_time = 0;
bool proc_running = false;
int proc_completed = 0;
int elapsed_time = 0;
float waiting_time = 0;
float response_time = 0;
float turnaround_time = 0;
int remaining_time[num_lines];
int wait_time[num_lines];
bool first_run[num_lines];
for (int i=0; i<num_lines;i++){
remaining_time[i] = -1;
first_run[i] = true;
wait_time[i] = 0;
}
int min_remaining_time = INT_MAX;
int curr_pid = -1;
while (proc_completed < num_lines){
proc_running = false;
for (int i=0; i<num_lines; i++){
if (systime >= arrival[i]){
if (remaining_time[i] != 0){
if(pid[i] != curr_pid){
if (remaining_time[i] == -1){
remaining_time[i] = burst[i];
}
}
}
}
}
int curr_index;
min_remaining_time = INT_MAX;
for (int i=0; i<num_lines; i++){
if (remaining_time[i] > 0){
if (remaining_time[i] < min_remaining_time){
min_remaining_time = remaining_time[i];
curr_pid = pid[i];
curr_index = i;
proc_running = true;
}
}
}
if (first_run[curr_index] == true){
first_run[curr_index] = false;
response_time += systime-arrival[curr_index];
}
if (proc_running == false){
wasted_time++;
printf("<system time %3d > no process is running\n", systime);
}
else{
printf("<system time %3d > process %3d is running\n", systime, curr_pid);
remaining_time[curr_index]--;
}
for (int i=0; i<num_lines; i++){
if (curr_pid != pid[i] && remaining_time[i] > 0 && systime >= arrival[i]){
wait_time[i]++;
}
}
systime++;
if (remaining_time[curr_index] == 0 && proc_running==true){
printf("<system time %3d > process %3d is finished.......\n", systime, curr_pid);
turnaround_time += (systime - arrival[curr_index]);
}
int completed = 0;
for (int i =0; i<num_lines; i++){
if (remaining_time[i] == 0){
completed++;
}
}
proc_completed = completed;
}
printf("<system time %3d > All processes finished.............\n\n", systime);
for (int i=0; i<num_lines; i++){
waiting_time += wait_time[i];
}
printf("==============================================\n");
printf("Average cpu usage : %.2f\n",(((float)systime-wasted_time)/systime)*100);
printf("Average waiting time : %.2f s\n", waiting_time/num_lines);
printf("Average response time : %.2f s\n", response_time/num_lines);
printf("Average turnaround time : %.2f s\n", turnaround_time/num_lines);
printf("==============================================\n");
}
void populate_arrays(char *filename, int *pid, int *arrival, int *burst){
char line[256];
FILE *file = fopen(filename, "r");
int i = 0;
while (fgets(line, sizeof(line), file)){
sscanf(line,"%d %d %d", &pid[i], &arrival[i], &burst[i]);
i++;
}
}
int get_num_lines(char *filename){
int num_lines = 0;
FILE *file = fopen(filename, "r");
char line[256];
while (fgets(line, sizeof(line), file)){
num_lines++;
}
return num_lines;
}
void insert(int queue[], int item,int size)
{
if ((front == 0 && rear == size - 1) || (front == rear + 1))
{
printf("queue is full");
return;
}
else if (rear == -1)
{
rear++;
front++;
}
else if (rear == size-1 && front > 0)
{
rear = 0;
}
else
{
rear++;
}
queue[rear] = item;
}
void display(int queue[], int size)
{
int i;
printf("\n");
if (front > rear)
{
for (i = front; i < size; i++)
{
printf("%d ", queue[i]);
}
for (i = 0; i <= rear; i++)
printf("%d ", queue[i]);
}
else
{
for (i = front; i <= rear; i++)
printf("%d ", queue[i]);
}
printf("\n");
}
int delete(int queue[])
{
int next;
if (front == -1)
{
}
else if (front == rear)
{
next = queue[front];
front = -1;
rear = -1;
}
else
{
next = queue[front];
front++;
}
return next;
}
Hope this helps!
Please let me know if any changes needed.
Thank you!
CODE IN C TO GET THIS RESULT CPU SCHEDULING: proj2 input.1 SRTF OUTPUT Schdeuling algorithm: SRTF...
scheduling program in C, please help
1 Objectives This programming project is to simulate a few CPU scheduling policies discussed in the class. You will write a C program to implement a simulator with different scheduling algorithms. The simulator selects a task to run from ready queue based on the scheduling algorithm. Since the project intends to simulate a CPU scheduler, so it does not require any actual process creation or execution. When a task is scheduled, the simulator will...
Implement the following 3 CPU scheduling algorithms Simulate and evaluate each with the set of eight processes below. Use any programming language. The program listing should be submitted with the report. FCFS (First Come First Serve) non-preemptive (partial results provided) SJF non-preemptive MLFQ Multilevel Feedback Queue (absolute priority in higher queues) Queue 1 uses RR scheduling with Tq = 5 Queue 2 uses RR scheduling with Tq = 10 Queue 3 uses FCFS All processes enter first...
Please help me create this CLI CPU Scheduling Simulator in java: First Come First Serve (FCFS) Round Robin (RR) Process information The process information will be read from an input file. The format is: pid arrival_time burst_time All of fields are integer type where: pid is a unique numeric process ID arrival_time is the time when the task arrives in the unit of milliseconds burst_time the is the CPU time requested by a task, in the unit of milliseconds The...
(d) Pre-emptive priority scheduling [4 marks]: Consider the following set of processes, with the length of the CPU-burst time given in milliseconds: Process Burst Priority Arrival Time P1 8 3 0 P2 3 4 1 P3 6 2 3 P4 3 1 5 P5 1 5 7 P6 3 8 14 P7 8 5 18 (d-1) Draw Gantt chart illustrating the execution of these processes using pre-emptive priority (a smaller priority number implies a higher priority) [2 marks] 00 01 ...
Consider the following set of processes, with the length of the CPU burst times given in milliseconds:a. Draw four Gantt charts illustrating the execution of the processes using FCFS, Preemptive SJF, a non-preemptive priority, and a RR (quantum=2) scheduling. (30 pts)Note: for the RR consider that the arriving time is 0 for all processesb. What is the average waiting time of each process for of the above scheduling algorithms? (10 pts)P1 8 2 0 P2 5 36P3 1 1 8...
Operating System Theory and Design
Write a program to simulate the operation of two of CPU scheduling methods. The program does the following: 1. Get the number of processes from the user. 2. Get the burst time of each process from the user 3. Assume that all processes arrive at "O" to the ready queue. 4. The program lets the user select one of the two methods to implement the o e oo implement the CPU scheduling. time. You can...
1.Six processes A, B, C, D, E, F are run under an operating system which uses priority scheduling. The arrangement is to decrement the priority of a process after it has run for a quantum. In the case of equal priorities, the process suspended for the longest time is chosen to run. The priorities assigned to the processes are 20, 11, 15, 12, 18, 14 respectively, and are all long CPU-bound processes. If the quantum is 100 ms, then how...
Consider the following 5 processes Process Arrival Time CPU Burst Time 0 3 2 6 P2 4 4 6 5 P4 2 Ps (a) Draw a timing diagram showing when each process executes under FIFO, SJF, and SRT (b) Determine the Average Turnaround Time (ATT) for each scheduling algorithm for the 5 processes
Compute turnaround time with preemptive priority scheduling. Also compute the average turnaround time. Show the Gantt chart and other details. Process CPU Burst time Arrival time Priority P1 25 5 5 P2 10 10 3 P3 15 15 4 P4 20 20 2 P5 30 25 1
Assume that you have four different processes with the following attributes: Process Arrival time. CPU Burst. I/O Burst Total CPU time A. 0 4 4 9 B 3 2 3 7 C 6 5 1 11 D 12 1 1 5 As we did in class, perform a scheduling simulation using these four processes according to the following algorithms: 1) First Come First Serve 2) Round Robin with time slice = 1 3) Round Robin with time slice = 3...