Answers
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!
Press 'ENTER' to start...\n", num_lines, data); getchar(); printf("==============================EEEEEE=========\n" : srtf(pid, arrival_time, burst_time, num_lines);
2 is running 2 is running 2 is running 2 is running 2 is running 2 is running 2 is finished. 5 is running 5 is running 5 is running 5 is running 5 is running 5 is running 5 is finished. 6 is running 6 is running 6 is running 6 is running 6 is running
Average cpu usage Average waiting time Average response time Average turnaround time : 100.00 : 10.50 S : 9.00 s : 17.33 S