You are on page 1of 18

Department of Electronics and Communication Engg.

ME / M.Tech. Digital Communication


Lab -4(simulation and modeling of computer networks)
MEDC-206
INDEX


S. No.

Name of Experiment

Date of
submission

Faculty
signature

Remarks
1
Program to simulate CPU scheduling SJF
algorithm.

2
Program to simulate Disk scheduling FCFS
algorithm.

3
Write a program to SIMULATE AIRCRAFT
Problem

4
Write a program to generate uniformly
distributed Random No.

5 Program to simulate CPU scheduling FCFS
algorithm.

6 Program to simulate Cob Web Model

7 Write a program for Inventory system.



PROGRAM 1

AIM: - Program to simulate CPU scheduling SJF algorithm.


PROGRAM :-

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>

void main()
{
clrscr();
int n, i, j, job[10], cpu[10], arr[10], comp[10], turn[10], wait[10], min;
int precomp=0, sumturn=0, sumwait=0;
float avgturn, avgwait;

cout<<"\n Enter Number of Jobs :";
cin>>n;

cout<<"\n Enter CPU Burst Time and Arrival Time :";
for(i=0;i<n;i++)
{
cin>>cpu[i]>>arr[i];
job[i]=i;
}

cout<<"\n The Number of Jobs are :"<<n;

cout<<"\n \n"<<setw(8)<<"JOB"<<setw(8)<<"Burst"<<setw(8)<<"ARRIVAL";
for(i=0;i<n;i++)
{
cout<<"\n"<<setw(8)<<(job[i]+1)<<setw(8)<<cpu[i]<<setw(8)<<arr[i];
}

for(i=1;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(cpu[j]>cpu[j+1])
{
int x=cpu[j];
cpu[j]=cpu[j+1];
cpu[j+1]=x;

int y=arr[j];
arr[j]=arr[j+1];
arr[j+1]=y;

int z=job[j];
job[j]=job[j+1];
job[j+1]=z;
}
}
}

cout<<"\n \n"<<setw(8)<<"JOB"<<setw(8)<<"BURST"<<setw(8)<<"ARRIVAL";
for(i=0;i<n;i++)
{
cout<<"\n"<<setw(8)<<(job[i]+1)<<setw(8)<<cpu[i]<<setw(8)<<arr[i];
}

for(i=1;i<=n;i++)
{
for(j=0;j<n;j++)
{
if(arr[j]<=precomp)
{
min=j;
break;
}
}

comp[min]=precomp+cpu[min];
turn[min]=comp[min]-arr[min];
wait[min]=precomp-arr[min];

arr[min]=arr[min]+32000;
precomp=precomp+cpu[min];
}

cout<<"\n
\n"<<setw(8)<<"JOB"<<setw(8)<<"BURST"<<setw(8)<<"ARRIVAL"<<setw(8)<<"COM
PLETION"<<setw(8)<<"TURN"<<setw(8)<<"WAIT";
for(i=0;i<n;i++)
{
cout<<"\n"<<setw(8)<<(job[i]+1)<<setw(8)<<cpu[i]<<setw(8)<<(arr[i] -
32000)<<setw(8)<<comp[i]<<setw(8)<<turn[i]<<setw(8)<<wait[i];
}

for(i=0;i<n;i++)
{
sumturn=sumturn+turn[i];
sumwait=sumwait+wait[i];
}

avgturn=(float)sumturn/n;
avgwait=(float)sumwait/n;

cout<<"\n \n The Average Turn Around Time is :"<<avgturn;
cout<<"\n \n The Waiting Time is :"<<avgwait;

getch();
}


OUTPUT:











PROGRAM 2

AIM: - Program to simulate Disk scheduling FCFS algorithm.


PROGRAM:-

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdio.h>

void main()
{
clrscr();

int gdriver=DETECT,gmode,i,j,chp=0,pos[20],n,y,tot=0;
char buffer[5];

cout<<"Enter number of request to be serviced-:";
cin>>n;

cout<<"\nEnter current position of disk head-:";
cin>>chp;

for(i=1;i<=n;i++)
{
cout<<"\n Enter request-"<<i;
cin>>pos[i];
}

pos[0]=chp;

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

line(69,50,569,50);

j=0;

for(i=69;i<=569;i=(i+50))
{
line(i,45,i,55);

sprintf(buffer,"%d",j);

outtextxy(i-5,65,buffer);

j=j+50;
}

y=100;

for(i=0;i<n;i++)
{
line(69+pos[i],y,69+pos[i+1],y+25);

y=y+25;

tot=tot+abs(pos[i+1]-pos[i]);
}

cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n TOTAL HEAD
MOVEMENT="<<tot;

getch();

closegraph();
}

OUTPUT:





PROGRAM 3


AIM: - Write a program to SIMULATE AIRCRAFT Problem

PROGRAM :-

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<iomanip.h>
#include<dos.h>

void main()
{
clrscr();
int a, b, c, d, w, x, y, z, d1, d2, d3, d4, c1, c2, c3, c4; int n;

cout<<"\n Enter the Value of Desired Heading :";
cin>>a>>b>>c>>d;

cout<<"\n Enter the Number of Times You want to Control the Aircraft :";
cin>>n;

cout<<"\n The Number of Controllings are :"<<n;

while (n != 0)
{

// label :

w = rand() % 100;
x = rand() % 100;
y = rand() % 100;
z = rand() % 100;

d1 = a - w;
d2 = b - x;
d3 = c - y;
d4 = d - z;

c1 = d1 + 2;
c2 = d2 + 2;
c3 = d3 + 2;
c4 = d4 + 2;

cout<<"\n \n Desired Heading :"<<setw(8)<<"Actual Heading :"<<setw(8)<<"Deviation
:"<<setw(8)<<"Control Value :"<<endl;

cout<<setw(4)<<a<<":"<<b<<":"<<c<<":"<<d<<setw(8)<<w<<":"<<x<<":"<<y<<":"<<z<
<setw(8)<<d1<<":"<<d2<<":"<<d3<<":"<<d4<<setw(8)<<c1<<":"<<c2<<":"<<c3<<":"<<
c4<<endl;
cout<<"\n";

delay(1000);

n--;

// a+=5; b=5; c+=5; d+=5;
}

// goto label;

getch();
}

OUTPUT:







PROGRAM 4


AIM: - Write a program to generate uniformly distributed
Random

PROGRAM:-

#include<conio.h>
#include<math.h>
#include<stdlib.h>

void main()
{
clrscr();
int a, b, i, m, n, seed, r[50];

cout<<"\n Enter the Values of Constants a, b, and m:";
cin>>a>>b>>m;

cout<<"\n The Value of Constant a :"<<a;
cout<<"\n The Value of Constant b :"<<b;
cout<<"\n The Value of Constant m :"<<m;

cout<<"\n Enter the Value of Seed or Initial Random Number :";
cin>>seed;

cout<<"\n The Value of Initial Random Number or Seed :"<<seed;

cout<<"\n Enter the Number of Random Numbers to be Generated :";
cin>>n;

cout<<"\n The Number of Random Numbers to be Generated are :"<<n;

r[0] = seed;

cout<<"\n The Random Numbers to be Generated after Initial Random Number are :";

for (i = 1; i <= n; i++)
{
r[i] = (a * r[i - 1] + b) % m;

cout<<r[i];
cout<<"\t";
}
getch();
}


OUTPUT:
























PROGRAM 5


AIM: - Program to simulate CPU scheduling FCFS algorithm.


PROGRAM :-

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>

void main()
{
clrscr();
int n, i, cpu[10], arr[10], comp[10], turn[10], wait[10];
int sumturn=0, sumwait=0;
float avgturn, avgwait;

cout<<"Enter Number of Jobs :";
cin>>n;

cout<<"\n \n Enter CPU Burst Time :";
for(i=0;i<n;i++)
{
cin>>cpu[i];
}

arr[0]=0;

cout<<"\n \n Enter Arrival Time in Increasing Order :";
for(i=1;i<n;i++)
{
cin>>arr[i];
}

cout<<"\n \n The Number of Jobs are :"<<n;

cout<<"\n \n CPU Burst Time are :";
for(i=0;i<n;i++)
{
cout<<"\t"<<cpu[i];
}

cout<<"\n \n Arrival Time are :";
for(i=0;i<n;i++)
{
cout<<"\t"<<arr[i];
}

comp[0]=cpu[0];

for(i=1;i<n;i++)
{
comp[i]=comp[i-1]+cpu[i];
}

cout<<"\n \n Completion Time are :";
for(i=0;i<n;i++)
{
cout<<"\t"<<comp[i];
}

for(i=0;i<n;i++)
{
turn[i]=comp[i]-arr[i];
}

cout<<"\n \n Turn Around Time are :";
for(i=0;i<n;i++)
{
cout<<"\t"<<turn[i];
}

wait[0]=0;

for(i=0;i<n;i++)
{
wait[i]=comp[i-1]-arr[i];
}

cout<<"\n \n Waiting Time are :";
for(i=0;i<n;i++)
{
cout<<"\t"<<wait[i];
}

for(i=0;i<n;i++)
{
sumturn=sumturn+turn[i];
sumwait=sumwait+wait[i];
}

for(i=0;i<n;i++)
{
avgturn=(float)sumturn/n;
avgwait=(float)sumwait/n;
}

cout<<"\n \n Average Turn Around Time is :"<<avgturn;

cout<<"\n \n Average Waiting Time is :"<<avgwait;

getch();
}



OUTPUT:















PROGRAM 6


AIM: - Program to simulate Cob Web Model


PROGRAM:-

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int P,a,b,c,d,p,S,Q;
cout<<"\n Enter Initial Price:";
cin>>p;
cout<<"\n Enter the Value of Contants a,b,c and d:";
cin>>a>>b>>c>>d;
cout<<"\n The Initial Price is:"<<p;
cout<<"\n The Values of Constants a:"<<a<<"b:"<<b<<"c:"<<c<<"and d:"<<d;
P=p;
for(int i=0;i<=5;i++)
{
S=c+(d*P);
Q=S;
P=(a-Q)/b;
}
cout<<"\n Price for Interval:"<<P;
cout<<"\n Quantity for Interval:"<<Q;
getch();
}
OUTPUT:
















PROGRAM 7


AIM: - Write a program for Inventory system.


PROGRAM :-

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void main()
{
clrscr();
int q1, q2, rp1, rp2, tdays, dmd, cdmd, stk, cstk, day, dd1, dd2, lt1, lt2;
int ishort, cshort, ord1, ord2;
float x, avstk, s1;
cout<<"\n Enter q1 and q2 :";
cin>>q1>>q2;

cout<<"\n Enter rp1 and rp2 :";
cin>>rp1>>rp2;

day=1; dd1=0; dd2=0;

cout<<"\n Enter thr Run Length Days of Simulation :";
cin>>tdays;

ord1=0; ord2=0;
ishort=0; cshort=0; cdmd=0;

cout<<"\n Enter Value of Initial stk :";
cin>>stk;

cout<<"\n The Value of Initial Stock is :"<<stk;

cstk=stk;

for(day=1;day<=tdays;day++)
{
x=rand()/32768.0;

if(x<=0.15)
dmd=4;

else if((x>0.15) && (x<=0.45))
dmd=5;

else if((x>0.45) && (x<=0.80))
dmd=6;

else
dmd=7;

cdmd=cdmd+dmd;

if(day>=dd1)
{
stk=stk+ord1;

dd1=0;
ord1=0;
}

if(day>=dd2)
{
stk=stk+ord2;

dd2=0;
ord2=0;
}

if(stk<=rp1)
{
ord1=q1;

x=rand()/32768.0;

if(x<=0.2)
lt1=2;

else if((x>0.2) && (x<=0.8))
lt1=3;

else
lt1=4;

dd1=day+lt1;
}

if(stk<=rp2)
{
ord2=q2;

x=rand()/32768.0;

if(x<0.2)
lt2=2;

if((x>0.2) && (x<=0.8))
lt2=3;

else
lt2=4;

dd2=day+lt2;
}

if(dmd<=stk)
ishort=0;

else
ishort=dmd-stk;

cshort=cshort+ishort;

stk=stk-dmd;

cstk=cstk+stk;

if(stk<=0)
stk=0;

if(dd1>0)
dd1=dd1-1;

if(dd2>0)
dd2=dd2-1;
}
float days=tdays;
avstk=cstk/days;

s1=(cdmd-cshort)*100.0/cdmd;

cout<<"\n Simulation Runs :"<<tdays<<" days";
cout<<"\n Q1 :"<<q1<<" Q2 :"<<q2<<" RP1 :"<<rp1<<" RP2 :"<<rp2;
cout<<"\n Average Stock :"<<avstk;
cout<<"\n Service Level :"<<s1;
getch();
}


OUTPUT:

You might also like