scheduling algorithm simulator

Status
Not open for further replies.

niea_7

Beta member
Messages
4
helo guys...can some1 pls help me with this problem? That would be a great help..thanks...

Develop a PROGRAM to simulate a computer using the different scheduling algorithms - FCFS, SJN, SRT, Round Robin, [using a time quantum of 3 sec], given the following information:


Process Arrival Time Burst time
P1 0 10
P2 1 1
P3 2 2
P4 3 1
P5 4 5


a. Compute the waiting time and turnaround time for every job and for each of the scheduling algorithms.
b. Also compute the average waiting time and average turnaround time for each of the scheduling algorithms and determine which one gives the best results.

Show the experience that can be learned by changing the quantum and see the effect on Average Turnaround time and Average weighting time and present the results.
 
FCFS - First Come First Served - process them in the order that they arrive.

SJN - Shortest Job Next - Process the shortest job first if two arrive at the same time. (It will start with P1 but all the others will arrive whilst the first is processing so they will be scheduled according to the last parameter.)

SRT - Shortest Remaining Time - If a job is running and one arrives with a shorter time to completion than the current process will take, start the new process and hold the current one.

Round Robin - Jobs that arrive are placed in a queue, when a job reaches the top of the queue it is given one quantum of run time and then is put to the back of the queue. This process repeats until the queue is empty.

Any help?
 
o_O

sry i have actually the same problem, can u say where did u get the answer or in what lenguage did u make the code ? plase, very apresiated!!! (sry about my bad english)
 
Well, I'm not familiar with the notation "Burst time", I have always been given that problem as "Arrival Time" and "Time units to complete".

Basically, the first thing I would do is figure out all the timings on paper first...

The definitions of all those types of scheduling were already posted, so you shouldn't have much trouble....
 
round robin
--------------
#include<stdio.h>
int main();
struct
{
char pid[4];
int waiting_time;
int exec_time;
int rem_time;
}pro[10];
int main()
{
int i,j,k,l,n;
int sum=0,q;
float avg_turn=0.0;
float avg_time=0.0;
printf("\nEnter the no. of processes : "); scanf("%d",&n);
printf("\nEnter the time stamp : "); scanf("%d",&q);
for(i=1;i<=n;i++)
{
printf("\nEnter the process id %d : ",i); scanf("%s",pro.pid);
printf("\nEnter the execution time %d : ",i); scanf("%d",&pro.exec_time);
pro.waiting_time=0;
pro.rem_time=pro.exec_time;
}
for(i=1;i<=n;i++)
sum=sum+pro.exec_time;
for(i=0;i<=sum-1;)
{
for(j=1;j<=n;j++)
{
for(k=1;k<=q;k++)
{
if(pro[j].rem_time==0) break;
pro[j].rem_time=pro[j].rem_time-1;
for(l=1;l<=n;l++)
{
if(l==j) continue;
if(pro[l].rem_time==0) continue;
pro[l].waiting_time=pro[l].waiting_time+1;
}
i=i+1;
}
}
}
printf("\nProcess id\tExecution time\tWaiting_time\tTurn around time");
for(i=1;i<=n;i++)
{
printf("\n%s\t\t%d\t\t%d\t\t%d",pro.pid,pro.exec_time,pro.waiting_time,
(pro.waiting_time+pro.exec_time));
avg_time=avg_time+pro.waiting_time;
avg_turn=avg_turn+pro.waiting_time+pro.exec_time;
}
avg_time=avg_time/(float)n;
avg_turn=avg_turn/(float)n;
printf("\nAverage waiting time = %f",avg_time);
printf("\nAverage turn around time = %f",avg_turn);

return 0;
}





priority without preemption
----------------------------------------
#include<stdio.h>
int main()
{
int exec_time[20];
char process_id[20][3];
int waiting_time[20];
int priority[20];
int turn_around_time[20];
int no_of_process;
float avg_waiting_time=0.0,avg_turn=0.0;
int i,j;
int temp;
char temp1[3];
printf("\nEnter the number of processes : ");
scanf("%d",&no_of_process);
for(i=1;i<=no_of_process;i++)
{
printf("\nEnter the process id for process %d : ",i);
scanf("%s",process_id);
printf("\nEnter the execution time for process %d : ",i);
scanf("%d",&exec_time);
printf("\nEnter the priority value for process %d : ",i);
scanf("%d",&priority);

}
for(i=1;i<=(no_of_process-1);i++)
for(j=1;j<=(no_of_process-1);j++)
if(priority[j]>priority[j+1])
{
temp=exec_time[j];
exec_time[j]=exec_time[j+1];
exec_time[j+1]=temp;
strcpy(temp1,process_id[j]);
strcpy(process_id[j],process_id[j+1]);
strcpy(process_id[j+1],temp1);
temp=priority[j];
priority[j]=priority[j+1];
priority[j+1]=temp;
}
waiting_time[1]=0;
turn_around_time[1]=waiting_time[1]+exec_time[1];
for(i=2;i<=no_of_process;i++)
{
waiting_time=exec_time[i-1]+waiting_time[i-1];
turn_around_time=waiting_time+exec_time;
avg_waiting_time=avg_waiting_time+waiting_time;
}
avg_waiting_time=avg_waiting_time/no_of_process;

printf("\nProcess_id\tWaiting_time\tTurn_around_time");
for(i=1;i<=no_of_process;i++)
{
printf("\n%s\t\t%d\t\t%d",process_id,waiting_time,turn_around_time);
avg_turn=avg_turn+turn_around_time;
}
avg_turn=avg_turn/no_of_process;
printf("\nAverage waiting time : %5.2f",avg_waiting_time);
printf("\nAverage turn around time : %5.2f",avg_turn);

return 0;
}

priority with pre
---------------------
#include<stdio.h>
#include<string.h>
#include<conio.h>
struct process
{
int arr_time;
char pname[5];
int exec_time;
int waiting_time;
int rem_time;
int priority;
int flag1;
int flag2;
}pro[5],temps;

int main()
{
int exec=-1,sum=0;
int first[5];
int i,j,temp1,temp2,max_pri;
clrscr();
printf("\nArrival time\tProcess name\tExecution time\tPriority\n");
for(i=1;i<=5;i++)
{
scanf("%d %s %d %d",&pro.arr_time,pro.pname,&pro.exec_time,
&pro.priority);
pro.rem_time=pro.exec_time;
pro.waiting_time=0;
pro.flag1=0;
pro.flag2=0;
first=0;
}
for(i=1;i<=5-1;i++)
for(j=1;j<=5-1;j++)
if(pro[j].arr_time>pro[j+1].arr_time)
{
temps=pro[j];
pro[j]=pro[j+1];
pro[j+1]=temps;
}

exec=-1;
temp1=0;
for(i=1;i<=5;i++)
sum=sum+pro.exec_time;
while(1)
{
exec=exec+1;
for(i=1;i<=5;i++)
if(pro.flag1==1)
temp1=temp1+1;
if(temp1==5) break;
else
temp1=0;
for(i=1;i<=5;i++)
{
if(pro.flag1==1) continue;
if(pro.arr_time<=exec)
pro.flag2=1;
}
for(i=1;i<=5;i++)
if(pro.flag2==1)
{
max_pri=pro.priority;
break;
}

for(i=1;i<=5;i++)
{
if(pro.flag2==0) continue;
if(pro.priority<=max_pri)
max_pri=pro.priority;
}
temp2=0;
for(i=1;i<=5;i++)
{
if(max_pri==pro.priority && pro.flag2==1 && temp2!=1)
{
pro.flag2=1;
temp2=1;
}
else
pro.flag2=0;
}
temp2=0;
for(i=1;i<=5;i++)
{
switch(pro.flag2)
{
case 1:
printf("\n%d %s",exec,pro.pname);
pro.rem_time=pro.rem_time-1;
if(pro.rem_time==0)
pro.flag1=1;

for(j=1;j<=5;j++)
{
if(pro[j].flag1==1 || i==j)
continue;
else
{
if(pro[j].arr_time>exec) ;
else
pro[j].waiting_time=pro[j].waiting_time+1;
}
}
break;
case 0:
break;
}
}
for(i=1;i<=5;i++)
pro.flag2=0;
}

printf("\nArrival time\tProcess name\tExecution time\tWaiting time\tTurn around time");
for(i=1;i<=5;i++)
{
printf("\n%d\t\t%s\t\t%d\t\t%d\t\t%d",pro.arr_time,pro.pname,pro.exec_time,
pro.waiting_time,
(pro.waiting_time+pro.exec_time));
}


return 0;
}



SJFS without pre
-----------------
#include<stdio.h>
int main()
{
int exec_time[20];
char process_id[20][3];
int waiting_time[20];
int turn_around_time[20];
int no_of_process;
float avg_waiting_time=0.0;
float avg_turn=0.0;
int i,j;
int temp;
char temp1[3];
printf("\nEnter the number of processes : ");
scanf("%d",&no_of_process);
for(i=1;i<=no_of_process;i++)
{
printf("\nEnter the process id for process %d : ",i);
scanf("%s",process_id);
printf("\nEnter the execution time for process %d : ",i);
scanf("%d",&exec_time);

}
for(i=1;i<=(no_of_process-1);i++)
for(j=1;j<=(no_of_process-1);j++)
if(exec_time[j]>exec_time[j+1])
{
temp=exec_time[j];
exec_time[j]=exec_time[j+1];
exec_time[j+1]=temp;
strcpy(temp1,process_id[j]);
strcpy(process_id[j],process_id[j+1]);
strcpy(process_id[j+1],temp1);
}
waiting_time[1]=0;
turn_around_time[1]=waiting_time[1]+exec_time[1];
for(i=2;i<=no_of_process;i++)
{
waiting_time=exec_time[i-1]+waiting_time[i-1];
turn_around_time=waiting_time+exec_time;
avg_waiting_time=avg_waiting_time+waiting_time;
avg_turn=avg_turn+turn_around_time;
}

avg_waiting_time=avg_waiting_time/no_of_process;
avg_turn=avg_turn/no_of_process;
printf("\nProcess_id\tWaiting_time\tTurn_around_time");
for(i=1;i<=no_of_process;i++)
{
printf("\n%s\t\t%d\t\t%d",process_id,waiting_time,turn_around_time);
}
printf("\nAverage waiting time : %5.2f",avg_waiting_time);
printf("\nAverage turn around time : %5.2f",avg_turn);

return 0;
}

SJFS with pre
-----------------
#include<stdio.h>
#include<string.h>
#include<conio.h>
struct process
{
int arr_time;
char pname[5];
int exec_time;
int waiting_time;
int rem_time;
int flag1;
int flag2;
}pro[5];

int main()
{
int exec=-1,sum=0;
int first[5];
int i,j,temp1,temp2,min_rem;
clrscr();
printf("\nArrival time\tProcess name\tExecution time\n");
for(i=1;i<=5;i++)
{
scanf("%d %s %d",&pro.arr_time,pro.pname,&pro.exec_time);
pro.rem_time=pro.exec_time;
pro.waiting_time=0;
pro.flag1=0;
pro.flag2=0;
first=0;
}

exec=-1;
temp1=0;
for(i=1;i<=5;i++)
sum=sum+pro.exec_time;
while(1)
{
exec=exec+1;
for(i=1;i<=5;i++)
if(pro.flag1==1)
temp1=temp1+1;
if(temp1==5) break;
else
temp1=0;
for(i=1;i<=5;i++)
{
if(pro.flag1==1) continue;
if(pro.arr_time<=exec)
pro.flag2=1;
}
for(i=1;i<=5;i++)
if(pro.flag2==1)
{
min_rem=pro.rem_time;
break;
}

for(i=1;i<=5;i++)
{
if(pro.flag2==0) continue;
if(pro.rem_time<=min_rem)
min_rem=pro.rem_time;
}
for(i=1;i<=5;i++)
{
if(min_rem==pro.rem_time && pro.flag2==1)
pro.flag2=1;
else
pro.flag2=0;
}
for(i=1;i<=5;i++)
{
switch(pro.flag2)
{
case 1:
printf("\n%d %s",exec,pro.pname);
pro.rem_time=pro.rem_time-1;
if(pro.rem_time==0)
pro.flag1=1;

for(j=1;j<=5;j++)
{
if(pro[j].flag1==1 || i==j)
continue;
else
{
if(pro[j].arr_time>exec) ;
else
pro[j].waiting_time=pro[j].waiting_time+1;
}
}
break;
case 0:
break;
}
}
for(i=1;i<=5;i++)
pro.flag2=0;
}

printf("\nArrival time\tProcess name\tExecution time\tWaiting time\tTurn around time");
for(i=1;i<=5;i++)
{
printf("\n%d\t\t%s\t\t%d\t\t%d\t\t%d",pro.arr_time,pro.pname,pro.exec_time,
pro.waiting_time,
(pro.waiting_time+pro.exec_time));
}


return 0;
}



FCFS
----
#include<stdio.h>
int main()
{
int exec_time[20];
char process_id[20][3];
int waiting_time[20];
int turn_around_time[20];
int no_of_process;
float avg_waiting_time=0.0;
float avg_turn=0.0;
int i;
printf("\nEnter the number of processes : ");
scanf("%d",&no_of_process);
for(i=1;i<=no_of_process;i++)
{
printf("\nEnter the process id for process %d : ",i);
scanf("%s",process_id);
printf("\nEnter the execution time for process %d : ",i);
scanf("%d",&exec_time);
}
waiting_time[1]=0;
turn_around_time[1]=waiting_time[1]+exec_time[1];
for(i=2;i<=no_of_process;i++)
{
waiting_time=exec_time[i-1]+waiting_time[i-1];
turn_around_time=waiting_time+exec_time;
avg_waiting_time=avg_waiting_time+waiting_time;
avg_turn=avg_turn+turn_around_time;
}
avg_waiting_time=avg_waiting_time/no_of_process;
avg_turn=avg_turn/no_of_process;
printf("\nProcess_id\tWaiting_time\tTurn_around_time");
for(i=1;i<=no_of_process;i++)
{
printf("\n%s\t\t%d\t\t%d",process_id,waiting_time,turn_around_time);
}
printf("\nAverage waiting time : %5.2f",avg_waiting_time);
printf("\nAverage turn around time : %5.2f",avg_turn);

return 0;
}
 
Status
Not open for further replies.
Back
Top Bottom