Description: Following C code runs a simulation to show how process scheduling in operating system is done using the job scheduling algorithm Shortest Job First. SJF is one of the many scheduling algorithms in operating system.
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
struct Node{
int p_id;
int arr_time;
int exe_time;
};
int main(){
int i,j,min,counter=100,no_of_processes;
struct Node *Queue,temp;
printf("\n--> Shortest Job First Scheduling <--\n");
printf("Enter no. of Processes:");
scanf("%d",&no_of_processes);
Queue=malloc(no_of_processes);
for(i=0;i<no_of_processes;i++){
printf("Give Process#%d id :\n",i+1);
scanf("%d",&Queue[i].p_id);
printf("Give Arrival Time (0~23):\n");
scanf("%d",&Queue[i].arr_time);
printf("Give Execution Time (in secs.):\n");
scanf("%d",&Queue[i].exe_time);
}
counter=0;
for(i=0;i<no_of_processes-1;i++){
min=i;
for(j=i+1;j<no_of_processes;j++)
if(Queue[j].exe_time<Queue[min].exe_time && Queue[j].arr_time<=counter)
min=j;
counter=counter+Queue[min].exe_time;
temp=Queue[i];
Queue[i]=Queue[min];
Queue[min]=temp;
}
for(i=0;i<no_of_processes;i++){
printf("\t--> PROCESS with Process ID#%d <--\n",Queue[i].p_id);
printf("\t--> PROCESS with Process ID#%d <--\n",Queue[i].p_id);
printf("This process arrived at %d and requires %d seconds for execution\n\n",Queue[i].arr_time,Queue[i].exe_time);
printf("Execution in Progress\n\n");
for(j=0;j<Queue[i].exe_time;j++){
sleep(1);
printf("*");
}
printf("\n\nPROCESS FINISHED\n\n");
}
return 0;
}
No comments
Post a Comment