sim lab manual(17 july)

41
Patel College of Science & Technology, Indore Patel College of Science & Technology, Indore Department Of Computer Science & Engineering Department Of Computer Science & Engineering LAB MANUAL LAB MANUAL Simulation and Modeling CS-703 CS-703

Upload: thesumit

Post on 07-Apr-2015

691 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: SIM Lab Manual(17 July)

Patel College of Science & Technology, IndorePatel College of Science & Technology, Indore

Department Of Computer Science & EngineeringDepartment Of Computer Science & Engineering

LAB MANUALLAB MANUAL

Simulation and ModelingCS-703CS-703

Patel College of Science & Technology, Indore

Department of Computer Science & Engineering.

Page 2: SIM Lab Manual(17 July)

LAB MANUAL

Faculty Name: Pritesh Jain Subject: Simulation and Modeling Year/Sem.: VII Subject Code: CS-703

S.No. List of Experiments

1. Simulate FCFS, CPU scheduling algorithm using queuing system.

2. Simulate SJF, CPU scheduling algorithm using queuing system.

3.Simulate ROUND ROBIN, CPU scheduling algorithm using queuing system.

4.Program to implement Priority CPU Scheduling algorithm.

5. Program to implement Disk Scheduling Algorithm – FCFS.

6.Simulate LOOK, DISK scheduling algorithm.

7.Simulate CLOOK, DISK scheduling algorithm.

8.Program to implement FIFO page replacement algorithm.

9.Program to implement LRU page replacement algorithm

10.Simulate a Manufacturing shop and write a program in GPSS.

Experiment No.1Experiment No.1

Page 3: SIM Lab Manual(17 July)

Aim : - Simulate FCFS, CPU scheduling algorithm using queuing system

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int no_of_process,i;

float avarege_waiting_time=0,avarege_turnaround_time=0;

float waiting_time[10],process_exe_time[10],turnaround_time[10];

printf("Enter the no. of processes");

scanf("%d",&no_of_process);

printf("Enter the execution time of all processes ");

for(i=0;i<no_of_process;i++)

{

scanf("%f",&process_exe_time[i]);

}

turnaround_time[0]=process_exe_time[0];

for(i=0;i<no_of_process;i++)

{

turnaround_time[i+1]=turnaround_time[i]+process_exe_time[i+1];

// }

//for(i=0;i<no_of_process;i++)

waiting_time[i]=turnaround_time[i]-process_exe_time[i];

Page 4: SIM Lab Manual(17 July)

}

printf("\n\n\n\nPROCESS\t\tTURNAROUND TIME\t\tWAITING TIME");

for(i=0;i<no_of_process;i++)

{

printf("\n %d \t\t %f \t\t%f",i+1,turnaround_time[i],waiting_time[i]);

avarege_turnaround_time=avarege_turnaround_time+turnaround_time[i];

avarege_waiting_time=avarege_waiting_time+waiting_time[i];

}

avarege_turnaround_time=avarege_turnaround_time/no_of_process;

avarege_waiting_time=avarege_waiting_time/no_of_process;

printf("\n\n AVAREGE \t%f\t\t%f ",avarege_turnaround_time,avarege_waiting_time);

//for(i=0;i<=no_of_process;i++)

//printf("the waiting time for all processes are");

getch();

}

Page 5: SIM Lab Manual(17 July)

Experiment No. 2Experiment No. 2

Aim : - Simulate SJF, CPU scheduling algorithm using queuing system.

#include<conio.h>

#include<iostream.h>

#include<graphics.h>

#include<dos.h>

//void arrange( int *burst,char *p);

void main()

{ int gdriver=DETECT,gmode;

int burst[5],wt[5],trt[5],poly[8];

int avg_wt,avg_trt,t_trt,t_wt;

int temp;

char temp1;

char p[5];

clrscr();

initgraph(&gdriver,&gmode,"c:\\tc\\bgi");

for(int i=0;i<5;i++)

{

cout<<"\nENTER THE PROCESS NAME\n";

cin>>p[i];

cout<<"\nENTER THE BURST TIME\n";

cin>>burst[i];

}

t_trt=0;

t_wt=0;

Page 6: SIM Lab Manual(17 July)

for(int j=0;j<5;j++)

{

for(int k=0;k<j;k++)

{

if(burst[k]>burst[k+1])

{

temp=burst[k];

burst[k]=burst[k+1];

burst[k+1]=temp;

temp1=p[k];

p[k]=p[k+1];

p[k+1]=temp1;

}

}

}

for(int k=0;k<5;k++)

{

cout<<burst[k]<<" ";

cout<<p[k]<<"\n";

}

wt[0]=0;

for(i=0;i<5;i++)

{

trt[i]=wt[i]+burst[i];

wt[i+1]=trt[i];

t_trt=t_trt+ trt[i];

Page 7: SIM Lab Manual(17 July)

t_wt=t_wt+wt[i];

}

avg_wt=t_wt/5;

avg_trt=t_trt/5;

cout<<"THE AVERAGE WAITING TIME IS "<<avg_wt<<"secs";

cout<<"\n THE AVERAGE TURN AROUND TIME IS "<<avg_trt<<"secs";

clrscr();

//rectangle(20,50,100,20);

rectangle(20,50,trt[4]*10+20,100);

//floodfill(20,50,4);

for(i=0;i<5;i++)

{

setcolor(i);

//line((trt[i]*10)+20,50,(trt[i]*10)+20,100);

rectangle((wt[i]*10)+20,50,(trt[i]*10)+20,100);

delay(1000);

}

getch();

closegraph();

}

Experiment No. 3Experiment No. 3

Aim : - Aim : - Simulate ROUND ROBIN, CPU scheduling algorithm using queuing system

#include<stdlib.h>

#include<stdio.h>

#include<conio.h>

Page 8: SIM Lab Manual(17 July)

#include<graphics.h> // delay

#include<iostream.h>

#include<dos.h>

void fcfs();

void getentry();

void rr();

int no_pro,i,pro_run;

float av_wait=0,av_turn=0;

float wait_time[10],pro_exe_time[10],turn_time[10];

void getentry()

{

printf("Enter the no. of processes");

scanf("%d",&no_pro);

pro_run=no_pro;//no of process those are running

printf("Enter the execution time of all processes ");

for(i=0;i<no_pro;i++)

{

scanf("%f",&pro_exe_time[i]);

}

}

void main()

{

clrscr();

/* request auto detection */

int gdriver = DETECT, gmode, errorcode;

Page 9: SIM Lab Manual(17 July)

initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

getentry();

delay(500);

// fcfs();

rr();

getch();

closegraph();

}

void rr()

{

int i=0;

int x1=0,x2=0,y1=0,y2=0;

x1=50;

y1=300;

y2=400;

x2=120;

if(i<pro_run)

{

while(pro_exe_time[i]>0)

{ cout<<"\npro "<<i<<"\t exe "<<pro_exe_time[i];

pro_exe_time[i]=pro_exe_time[i]-1;

delay(500);

Page 10: SIM Lab Manual(17 July)

//delay(process_exe_time[i]+1000);

setfillstyle(1,i+3);

//floodfill((x1+x2)/2,(y1+y2)/2,i+3);

// bar(process_exe_time[i]+100,100,100,process_exe_time[i]+100);

x2=x1+20;

bar(x1,y1,x2,y2);

//rectangle(x1+1,x2+1,y1+1,y2+1);

x1=x1+20;

if(pro_exe_time[i]==0)

{ int temp=i;

for(;temp<pro_run;temp++)

pro_exe_time[temp]=pro_exe_time[temp];

no_pro=no_pro-1;

pro_run=pro_run-1;

i--;

}

i++;

if(i>=pro_run)

i=0;

}

}

}

/*

Page 11: SIM Lab Manual(17 July)

void fcfs()

{

turnaround_time[0]=process_exe_time[0];

for(i=0;i<no_of_process;i++)

{

waiting_time[i]=turnaround_time[i];

turnaround_time[i+1]=turnaround_time[i]+process_exe_time[i+1];

}

printf("\n\n\n\nPROCESS\t\tTURNAROUND TIME\t\tWAITING TIME");

int x1=0,x2=0,y1=0,y2=0;

x1=50;

y1=300;

y2=400;

x2=120;

for(i=0;i<no_of_process;i++)

{ //outtextxy(x1,y1,"process");

for(int j=0;j<=process_exe_time[i];j++)

{

delay(500);

//delay(process_exe_time[i]+1000);

setfillstyle(1,i+3);

//floodfill((x1+x2)/2,(y1+y2)/2,i+3);

Page 12: SIM Lab Manual(17 July)

// bar(process_exe_time[i]+100,100,100,process_exe_time[i]+100);

x2=x1+20;

bar(x1,y1,x2,y2);

//rectangle(x1+1,x2+1,y1+1,y2+1);

x1=x1+20;

}

printf("\n %d \t\t %f \t\t%f",i+1,turnaround_time[i],waiting_time[i]);

avarege_turnaround_time=avarege_turnaround_time+turnaround_time[i];

avarege_waiting_time=avarege_waiting_time+waiting_time[i];

}

avarege_turnaround_time=avarege_turnaround_time/no_of_process;

avarege_waiting_time=avarege_waiting_time/no_of_process;

printf("\n\n AVAREGE \t%f\t\t%f ",avarege_turnaround_time,avarege_waiting_time);

}

*/

Experiment No. 4

Aim :- Program to implement Priority CPU Scheduling algorithm.

#include<iostream.h>

#include<conio.h>

int i,nop,bt[10],pri[10],pname[10];

Page 13: SIM Lab Manual(17 July)

void input();

void sort();

void output();

void main()

{

clrscr();

input();

sort();

output();

getch();

}

void output()

{

int sum=0,sum2=0,avg;

for(i=0;i<nop;i++)

{

sum2=sum+sum2;

cout<<"\nWaiting time for process "<<pname[i]<<" is "<<sum<<endl;

sum=sum+bt[i];

}

avg=sum2/nop;

cout<<"\nAverage waiting time = "<<avg<<endl;

sum=0; sum2=0;

for(i=0;i<nop;i++)

Page 14: SIM Lab Manual(17 July)

{

sum=sum+bt[i];

cout<<"\nTurnaround time for process "<<pname[i]<<" is "<<sum<<endl;

sum2=sum+sum2;

}//for

avg=sum2/nop;

cout<<"\nAverage Turnaround time = "<<avg<<endl;

}// output();

void sort()

{

for(i=0;i<nop;i++)

{

for(int j=0;j<nop-i-1; j++)

{

if(pri[j]>pri[j+1])

{

int temp=bt[j+1];

bt[j+1]=bt[j];

bt[j]=temp;

temp=pri[j+1];

pri[j+1]=pri[j];

pri[j]=temp;

temp=pname[j+1];

pname[j+1]=pname[j];

pname[j]=temp;

Page 15: SIM Lab Manual(17 July)

} // if

} // inner loop

} // outer loop

} // sort();

void input()

{

cout<<"Enter number of processes"; cin>>nop;

for(i=0; i<nop; i++)

{

pname[i]=i+1;

cout<<"Enter burst time for process p"<<i+1<<" ";

cin>>bt[i];

}

for(i=0; i<nop; i++)

{

cout<<"Enter priority for process p"<<i+1<<" ";

cin>>pri[i];

}

} // input function

Experiment No. 5

Aim :- Simulate FCFS, DISK scheduling algorithm.

Page 16: SIM Lab Manual(17 July)

#include<iostream.h>

#include<conio.h>

#include<graphics.h>

int n,hp[10],start;

int gdriver=DETECT,gmode;

void calculate();

void draw();

void main()

{

clrscr();

initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

closegraph();

calculate();

draw();

getch();

}

void draw()

{

int temp1=30;

for(int i=0;i<n;i++)

{

Page 17: SIM Lab Manual(17 July)

setcolor(i+2);

line(hp[i],temp1,hp[i+1],temp1+35);

temp1=temp1+35;

} // for

}

void calculate()

{

cout<<"Enter number of requests"; cin>>n;

int total=0;

cout<<"Enter head position for request 1 ";

cin>>hp[0];

int diff;

for(int i=1;i<n;i++)

{

cout<<"Enter head position for request "<<i+1<<" ";

cin>>hp[i];

diff=hp[i]-hp[i-1];

if(diff<0) { diff=-diff; }

total=total+diff;

}

start=hp[0];

cout<<"Total moment = "<<total;

cout<<"\n\nPress enter to continue";

getch();

initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

}

Page 18: SIM Lab Manual(17 July)

Experiment No. 6

Aim : - Simulate LOOK, DISK scheduling algorithm.

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<graphics.h>

main()

{

int gd=DETECT,gm,x,y,a1,x1,y1,a,b;

int req[30],head,i,k,move,n,ch,t1,t2,max,min,g[30],c,h;

initgraph(&gd,&gm,"z:\tcpp\bgi");

/*int req[30],head,i,k,move,n,ch,t1,t2,max,min,g[30],c,h;*/

clearviewport();

printf("(1) LOOK (2) CLOOK (3) FCFS (0) EXIT

Enter your choice:

");

scanf("%d",&ch);

while(ch)

{

printf("Enter the current head position: ");

scanf("%d",&head); h=head;

printf("Enter the number of requests: ");

Page 19: SIM Lab Manual(17 July)

scanf("%d",&n); k=0; move=0;

printf("Total number of cylinders: 200.

");

printf("Enter the requests (1 to 200):

");

req:

for(i=0;i<n;i++)

{

scanf("%d",&req[i]);

if(req[i]>200||req[i]<1)

{ printf("Enter requests only between 1-200."); goto req; }

}

switch(ch)

{

case 1:

outtextxy(270,10,"SEEK PATTERN FOR LOOK");

max=req[0]; min=req[0]; t2=1; c=0;

for(i=1;i<n;i++)

{

max=max>req[i]?max:req[i];

min=min<req[i]?min:req[i];

}

if(max<=head) t2=0;

while(k<n)

{

Page 20: SIM Lab Manual(17 July)

t1=0;

for(i=0;i<n;i++)

{

if(t2)

{

if(req[i]&&req[i]>=head)

{

if(req[i]<req[t1]) t1=i;

if(req[i]==max)

{ t2=0; g[c]=1; c++; }

}

}

else

{

if(req[i]&&req[i]<=head)

{

if(req[i]>req[t1]) t1=i;

if(req[i]==min)

{ t2=1; g[c]=200; c++; }

}

}

}/*for*/

printf("%d ",req[t1]);

g[c]=req[t1]; c++;

move+=abs(head-req[t1]);

Page 21: SIM Lab Manual(17 July)

req[t1]=0; k++;

head=req[t1];

if(head==max) t2=0;

}/*while*/

break;

case 2:

outtextxy(270,10,"SEEK PATTERN FOR CLOOK");

max=req[0]; min=req[0]; t2=1; c=0;

for(i=1;i<n;i++)

{

max=max>req[i]?max:req[i];

min=min<req[i]?min:req[i];

}

while(k<n)

{

t1=0;

if(!t2) head=min;

for(i=0;i<n;i++)

{

if(req[i]&&req[i]>=head)

{

if(req[i]<req[t1]) t1=i;

else if(req[i]==max)

{ head=1; g[c]=1; c++; }

}

Page 22: SIM Lab Manual(17 July)

}/*for*/

printf("%d ",req[t1]);

move+=abs(head-req[t1]);

head=req[t1];

g[c]=req[t1]; c++;

req[t1]=0; k++;

if(head==max) t2=0;

}/*while*/

break;

case 3:

outtextxy(270,10,"SEEK PATTERN FOR FCFS");

for(i=0,c=0;i<n;i++,c++)

{

printf("%d ",req[i]);

move+=abs(head-req[i]);

head=req[i];

g[c]=req[i];

}

break;

}/*switch*/

printf("Order of servicing:

");

printf("%d: ",h);

for(i=0;i<n;i++)

printf("%d ",g[i]);

Page 23: SIM Lab Manual(17 July)

printf("

Total Head Movements: %d

",move);

x=getmaxx();

y=getmaxy();

rectangle(0,20,x-5,y-5);

a1=(x-30)/10;

b=110; a=h+(3*a1); y1=125;

fillellipse(a,b,2,2);

for(i=0;i<n;i++)

{

int x1=g[i]+(3*a1);

fillellipse(x1,y1,2,2);

line(a,b,x1,y1);

a=x1; b=y1;

y1+=15;

}

getch();

clrscr(); clearviewport();

printf("

(1) LOOK (2) CLOOK (3) FCFS (0) EXIT

Page 24: SIM Lab Manual(17 July)

Enter your choice:

");

scanf("%d",&ch);

}/*while*/

getch();

}/*main*/

Experiment No. 7

Aim : - Simulate CLOOK, DISK scheduling algorithm.

Page 25: SIM Lab Manual(17 July)

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<graphics.h>

main()

{

int gd=DETECT,gm,x,y,a1,x1,y1,a,b;

int req[30],head,i,k,move,n,ch,t1,t2,max,min,g[30],c,h;

initgraph(&gd,&gm,"z:\tcpp\bgi");

/*int req[30],head,i,k,move,n,ch,t1,t2,max,min,g[30],c,h;*/

clearviewport();

printf("(1) LOOK (2) CLOOK (3) FCFS (0) EXIT

Enter your choice:

");

scanf("%d",&ch);

while(ch)

{

printf("Enter the current head position: ");

scanf("%d",&head); h=head;

printf("Enter the number of requests: ");

scanf("%d",&n); k=0; move=0;

printf("Total number of cylinders: 200.

");

printf("Enter the requests (1 to 200):

Page 26: SIM Lab Manual(17 July)

");

req:

for(i=0;i<n;i++)

{

scanf("%d",&req[i]);

if(req[i]>200||req[i]<1)

{ printf("Enter requests only between 1-200."); goto req; }

}

switch(ch)

{

case 1:

outtextxy(270,10,"SEEK PATTERN FOR LOOK");

max=req[0]; min=req[0]; t2=1; c=0;

for(i=1;i<n;i++)

{

max=max>req[i]?max:req[i];

min=min<req[i]?min:req[i];

}

if(max<=head) t2=0;

while(k<n)

{

t1=0;

for(i=0;i<n;i++)

{

if(t2)

Page 27: SIM Lab Manual(17 July)

{

if(req[i]&&req[i]>=head)

{

if(req[i]<req[t1]) t1=i;

if(req[i]==max)

{ t2=0; g[c]=1; c++; }

}

}

else

{

if(req[i]&&req[i]<=head)

{

if(req[i]>req[t1]) t1=i;

if(req[i]==min)

{ t2=1; g[c]=200; c++; }

}

}

}/*for*/

printf("%d ",req[t1]);

g[c]=req[t1]; c++;

move+=abs(head-req[t1]);

req[t1]=0; k++;

head=req[t1];

if(head==max) t2=0;

}/*while*/

Page 28: SIM Lab Manual(17 July)

break;

case 2:

outtextxy(270,10,"SEEK PATTERN FOR CLOOK");

max=req[0]; min=req[0]; t2=1; c=0;

for(i=1;i<n;i++)

{

max=max>req[i]?max:req[i];

min=min<req[i]?min:req[i];

}

while(k<n)

{

t1=0;

if(!t2) head=min;

for(i=0;i<n;i++)

{

if(req[i]&&req[i]>=head)

{

if(req[i]<req[t1]) t1=i;

else if(req[i]==max)

{ head=1; g[c]=1; c++; }

}

}/*for*/

printf("%d ",req[t1]);

move+=abs(head-req[t1]);

head=req[t1];

Page 29: SIM Lab Manual(17 July)

g[c]=req[t1]; c++;

req[t1]=0; k++;

if(head==max) t2=0;

}/*while*/

break;

case 3:

outtextxy(270,10,"SEEK PATTERN FOR FCFS");

for(i=0,c=0;i<n;i++,c++)

{

printf("%d ",req[i]);

move+=abs(head-req[i]);

head=req[i];

g[c]=req[i];

}

break;

}/*switch*/

printf("Order of servicing:

");

printf("%d: ",h);

for(i=0;i<n;i++)

printf("%d ",g[i]);

printf("

Total Head Movements: %d

",move);

x=getmaxx();

Page 30: SIM Lab Manual(17 July)

y=getmaxy();

rectangle(0,20,x-5,y-5);

a1=(x-30)/10;

b=110; a=h+(3*a1); y1=125;

fillellipse(a,b,2,2);

for(i=0;i<n;i++)

{

int x1=g[i]+(3*a1);

fillellipse(x1,y1,2,2);

line(a,b,x1,y1);

a=x1; b=y1;

y1+=15;

}

getch();

clrscr(); clearviewport();

printf("

(1) LOOK (2) CLOOK (3) FCFS (0) EXIT

Enter your choice:

");

scanf("%d",&ch);

}/*while*/

Page 31: SIM Lab Manual(17 July)

getch();

}/*main*/

Experiment No. 8

Aim : - Program to implement FIFO page replacement algorithm.

Page 32: SIM Lab Manual(17 July)

#include<stdlib.h>#include<stdio.h>#include<conio.h>#define max 100#define min 10

int ref[max],count,frame[min],n;

void input(){system("CLS");int i,temp;count=0;printf("\n\n\tEnter the number of page frames : ");scanf("%d",&n);printf("\n\n\tEnter the reference string (-1 for end) : ");scanf("%d",&temp);while(temp != -1){ref[count++]=temp;scanf("%d",&temp);}}

void main()

{

int x;

//freopen("in.cpp","r",stdin);

while(1)

{

system("CLS");

printf("\n\t1. Input ");

printf("\n\t2. FIFO Algorithm");

printf("\n\t0. Exit."); scanf("%d",&x);switch(x){case 1:input();break;

Page 33: SIM Lab Manual(17 July)

case 0:exit(0);}}}

Experiment No. 9

Aim : - Program to implement LRU page replacement algorithm. #include<stdlib.h>

Page 34: SIM Lab Manual(17 July)

#include<stdio.h>#include<conio.h>#define max 100#define min 10

int ref[max],count,frame[min],n;

void LRU()

{

int i,j,k,stack[min],top=0,fault=0;

system("CLS");

for(i=0;i<count;i++)

{

if(top<n)

stack[top++]=ref[i],fault++;

else

{

for(j=0;j<n;j++)

if(stack[j]==ref[i])

break;

if(j<n)

{

for(k=j;k<n-1;k++)

stack[k]=stack[k+1];

stack[k]=ref[i];

}

else

{

for(k=0;k<n-1;k++)

stack[k]=stack[k+1];

stack[k]=ref[i];

fault++;

}

}

printf("\n\nAfter inserting %d the stack status is : ",ref[i]);

for(j=0;j<top;j++)

printf("%d ",stack[j]);

getch();

Page 35: SIM Lab Manual(17 July)

}//for i

printf("\n\n\tEnd to inserting the reference string.");

printf("\n\n\tTotal page fault is %d.",fault);

printf("\n\n\tPress any key to continue.");

getch();

}

void main()

{

int x;

//freopen("in.cpp","r",stdin);

while(1)

{

system("CLS");

printf("\n\t1. Input ");

printf("\n\t4. LRU (Least Recently Used) Algorithm");

printf("\n\t0. Exit.");

printf("\n\n\tEnter your choice.");

scanf("%d",&x);

switch(x)

{

case 1:

input();

break;

case 4:LRU();break;case 0:exit(0);}}

Experiment No. 10

Page 36: SIM Lab Manual(17 July)

Aim :- Simulate a Manufacturing shop and write a program in GPSS.

A machine tool in a manufacturing shop is turning out parts at the rate of every 5 minutes. As they

are finished, the parts are turned over to an inspector who takes 4±3 minutes to examine

each one and rejects about 10% of the parts as faulty. Each part will be represented by an Xact

and the base time unit for the system is chosen as 1 minute. Simulate for 100 parts to leave the system.