Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » scheduling algorithm simulator
Closed Thread
Old 07-27-2004, 11:06 AM   #1 (permalink)
 
Newb Techie

Join Date: Jul 2004

Posts: 4

niea_7

Default scheduling algorithm simulator

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.
niea_7 is offline  
Old 07-28-2004, 08:47 AM   #2 (permalink)
 
Newb Techie

Join Date: Jul 2004

Posts: 4

niea_7

Default

nobody wanna help??? i really dont know how to do it...pls some1
niea_7 is offline  
Old 07-29-2004, 05:14 PM   #3 (permalink)
 
True Techie

Join Date: Apr 2004

Posts: 168

nak-1

Default

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?
__________________
--------------------

Nak

Is it just me, or does something smell suspicious about all this?
nak-1 is offline  
Old 07-31-2004, 02:03 AM   #4 (permalink)
 
Newb Techie

Join Date: Jul 2004

Posts: 4

niea_7

Default

thanks for helping nak-1.. i solve my problem already...thanks a lot.
niea_7 is offline  
Old 11-09-2005, 01:52 AM   #5 (permalink)
 
Newb Techie

Join Date: Nov 2005

Posts: 1

mubay

Default 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)
mubay is offline  
Old 11-09-2005, 01:45 PM   #6 (permalink)
 
Ultra Techie

Join Date: Jul 2005

Posts: 530

TheHeadFL

Send a message via AIM to TheHeadFL
Default

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....
__________________
Desktop machine: 2 x Opteron 246, Asus K8N-DL, 2GB PC3200 ECC Reg., XFX GeForce 6600GT, 74gb WD Raptor, 2 x 19\" LCDs, Windows XP x64
Server machine: Intel P4 3.0GHz 2MB EM64T, ECS i865pe, 1GB PC3200, 36gb WD Raptor, Windows Server 2003
Laptop: Dell Inspiron 9100 (Intel P4 3.2GHz 1MB Prescott, i865pe, 512MB PC3200, Mobility Radeon 9700, DVD+R/DL Burner), Windows XP
Linux: P3 450Mhz, 386MB ram, Slackware 10.1 (Running mySQL/Apache)
TheHeadFL is offline  
Old 11-13-2005, 03:37 AM   #7 (permalink)
 
Newb Techie

Join Date: Oct 2005

Posts: 8

forumforme

Default

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[i].pid);
printf("\nEnter the execution time %d : ",i); scanf("%d",&pro[i].exec_time);
pro[i].waiting_time=0;
pro[i].rem_time=pro[i].exec_time;
}
for(i=1;i<=n;i++)
sum=sum+pro[i].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[i].pid,pro[i].exec_time,pro[i].waiting_time,
(pro[i].waiting_time+pro[i].exec_time));
avg_time=avg_time+pro[i].waiting_time;
avg_turn=avg_turn+pro[i].waiting_time+pro[i].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[i]);
printf("\nEnter the execution time for process %d : ",i);
scanf("%d",&exec_time[i]);
printf("\nEnter the priority value for process %d : ",i);
scanf("%d",&priority[i]);

}
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[i]=exec_time[i-1]+waiting_time[i-1];
turn_around_time[i]=waiting_time[i]+exec_time[i];
avg_waiting_time=avg_waiting_time+waiting_time[i];
}
avg_waiting_time=avg_waiting_time/no_of_process;

printf("\nProcess_id\tWaiting_time\tTurn_around_ti me");
for(i=1;i<=no_of_process;i++)
{
printf("\n%s\t\t%d\t\t%d",process_id[i],waiting_time[i],turn_around_time[i]);
avg_turn=avg_turn+turn_around_time[i];
}
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[i].arr_time,pro[i].pname,&pro[i].exec_time,
&pro[i].priority);
pro[i].rem_time=pro[i].exec_time;
pro[i].waiting_time=0;
pro[i].flag1=0;
pro[i].flag2=0;
first[i]=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[i].exec_time;
while(1)
{
exec=exec+1;
for(i=1;i<=5;i++)
if(pro[i].flag1==1)
temp1=temp1+1;
if(temp1==5) break;
else
temp1=0;
for(i=1;i<=5;i++)
{
if(pro[i].flag1==1) continue;
if(pro[i].arr_time<=exec)
pro[i].flag2=1;
}
for(i=1;i<=5;i++)
if(pro[i].flag2==1)
{
max_pri=pro[i].priority;
break;
}

for(i=1;i<=5;i++)
{
if(pro[i].flag2==0) continue;
if(pro[i].priority<=max_pri)
max_pri=pro[i].priority;
}
temp2=0;
for(i=1;i<=5;i++)
{
if(max_pri==pro[i].priority && pro[i].flag2==1 && temp2!=1)
{
pro[i].flag2=1;
temp2=1;
}
else
pro[i].flag2=0;
}
temp2=0;
for(i=1;i<=5;i++)
{
switch(pro[i].flag2)
{
case 1:
printf("\n%d %s",exec,pro[i].pname);
pro[i].rem_time=pro[i].rem_time-1;
if(pro[i].rem_time==0)
pro[i].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[i].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[i].arr_time,pro[i].pname,pro[i].exec_time,
pro[i].waiting_time,
(pro[i].waiting_time+pro[i].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[i]);
printf("\nEnter the execution time for process %d : ",i);
scanf("%d",&exec_time[i]);

}
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[i]=exec_time[i-1]+waiting_time[i-1];
turn_around_time[i]=waiting_time[i]+exec_time[i];
avg_waiting_time=avg_waiting_time+waiting_time[i];
avg_turn=avg_turn+turn_around_time[i];
}

avg_waiting_time=avg_waiting_time/no_of_process;
avg_turn=avg_turn/no_of_process;
printf("\nProcess_id\tWaiting_time\tTurn_around_ti me");
for(i=1;i<=no_of_process;i++)
{
printf("\n%s\t\t%d\t\t%d",process_id[i],waiting_time[i],turn_around_time[i]);
}
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[i].arr_time,pro[i].pname,&pro[i].exec_time);
pro[i].rem_time=pro[i].exec_time;
pro[i].waiting_time=0;
pro[i].flag1=0;
pro[i].flag2=0;
first[i]=0;
}

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

for(i=1;i<=5;i++)
{
if(pro[i].flag2==0) continue;
if(pro[i].rem_time<=min_rem)
min_rem=pro[i].rem_time;
}
for(i=1;i<=5;i++)
{
if(min_rem==pro[i].rem_time && pro[i].flag2==1)
pro[i].flag2=1;
else
pro[i].flag2=0;
}
for(i=1;i<=5;i++)
{
switch(pro[i].flag2)
{
case 1:
printf("\n%d %s",exec,pro[i].pname);
pro[i].rem_time=pro[i].rem_time-1;
if(pro[i].rem_time==0)
pro[i].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[i].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[i].arr_time,pro[i].pname,pro[i].exec_time,
pro[i].waiting_time,
(pro[i].waiting_time+pro[i].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[i]);
printf("\nEnter the execution time for process %d : ",i);
scanf("%d",&exec_time[i]);
}
waiting_time[1]=0;
turn_around_time[1]=waiting_time[1]+exec_time[1];
for(i=2;i<=no_of_process;i++)
{
waiting_time[i]=exec_time[i-1]+waiting_time[i-1];
turn_around_time[i]=waiting_time[i]+exec_time[i];
avg_waiting_time=avg_waiting_time+waiting_time[i];
avg_turn=avg_turn+turn_around_time[i];
}
avg_waiting_time=avg_waiting_time/no_of_process;
avg_turn=avg_turn/no_of_process;
printf("\nProcess_id\tWaiting_time\tTurn_around_ti me");
for(i=1;i<=no_of_process;i++)
{
printf("\n%s\t\t%d\t\t%d",process_id[i],waiting_time[i],turn_around_time[i]);
}
printf("\nAverage waiting time : %5.2f",avg_waiting_time);
printf("\nAverage turn around time : %5.2f",avg_turn);

return 0;
}
forumforme is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On