0% found this document useful (0 votes)
176 views

Experiment 1 Simulate CPU Scheduling Algorithm and Using Queuing System A) Fcfs

The document describes 4 experiments related to CPU scheduling algorithms, multiplexing, congestion control, and disk scheduling algorithms. Experiment 1 implements and simulates First Come First Serve (FCFS), Shortest Job First (SJF), and Priority CPU scheduling algorithms. Experiment 2 simulates a multiplexer/concentrator using queueing systems. Experiment 3 simulates the Leaky Bucket congestion control algorithm. Experiment 4 implements disk scheduling algorithms.

Uploaded by

Priyesh Yadav
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
176 views

Experiment 1 Simulate CPU Scheduling Algorithm and Using Queuing System A) Fcfs

The document describes 4 experiments related to CPU scheduling algorithms, multiplexing, congestion control, and disk scheduling algorithms. Experiment 1 implements and simulates First Come First Serve (FCFS), Shortest Job First (SJF), and Priority CPU scheduling algorithms. Experiment 2 simulates a multiplexer/concentrator using queueing systems. Experiment 3 simulates the Leaky Bucket congestion control algorithm. Experiment 4 implements disk scheduling algorithms.

Uploaded by

Priyesh Yadav
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 33

Experiment 1

Simulate CPU Scheduling Algorithm and using queuing


system

a) FCFS
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int bt[20],tat[20],wt[20],n,sumw=0,sumt=0;
cout<<"\n Enter the no. of process to be executed: ";
cin>>n;
cout<<"\n Enter their burst times: \n";
for(int i=0;i<n;i++)
{
cout<<" P"<<(i+1)<<": ";
cin>>bt[i];
}
cout<<"\n Implementing FCFS Scheduling.\n";
cout<<"\n START: ";
for(i=0;i<n;i++)
{
if(i==0)
wt[i]=0;
else
wt[i]=tat[i-1];
tat[i]=bt[i]+wt[i];
cout<<" -->P"<<(i+1);
}
cout<<" :END\n";
cout<<"Process Burst Time Waiting Time Turn Around Time";
for(i=0;i<n;i++)
{
cout<<"\nP"<<(i+1)<<"\t\t"<<bt[i]<<"\t
\t"<<wt[i]<<"\t
\t"<<tat[i];
sumw+=wt[i];
sumt+=tat[i];
}
cout<<"\nAvg Waiting Time: "<<(double)sumw/n<<" ms";
cout<<"\nAvg Turn Around Time: "<<(double)sumt/n<<" ms";
getch();
}
OUTPUT
Enter the no. of process to be executed: 3
Enter their burst times:
P1:4
P2:3
P3:5

Implementing FCFS Scheduling.

START: -->P1 -->P2 -->P3 :END

Process Burst Time Waiting Time Turn Around Time


P1 4 0 4
P2 3 4 7
P3 5 7 12

Avg Waiting Time: 3.66667 ms


Avg Turn Around Time: 7.66667 ms

b) SJF
#include<iostream.h>
#include<conio.h>
#include<limits.h>
void main()
{
clrscr();
int bt[10],tat[10],wt[10],n,start=0,sumb=0,sumw=0,sumt=0;
int small,pos;
for(int i=0;i<10;i++)
{
bt[i]=INT_MAX;
}
cout<<"\n Enter the no. of process to be executed: ";
cin>>n;
cout<<"\n Enter their burst times: \n";
for(i=0;i<n;i++)
{
cout<<" P"<<(i+1)<<": ";
cin>>bt[i];
sumb+=bt[i];
}
cout<<"\n Implementing SJF Scheduling.\n\n";
cout<<" START: ";
while(sumb>start)
{
small=INT_MAX;
for(i=0;i<n;i++)
{
if(small>bt[i])
{
small=bt[i];
pos=i;
}
}
cout<<" -->P"<<(pos+1);
bt[pos]=INT_MAX;
wt[pos]=start;
tat[pos]=wt[pos]+small;
start+=small;
}
cout<<" : END";
cout<<"\n\n\n Process\t Waiting Time\tTurn Around
Time";
for(i=0;i<n;i++)
{
cout<<"\n P"<<(i+1)<<"\t\t\t"<<wt[i]<<"\t\t\t
"<<tat[i
];
sumw+=wt[i];
sumt+=tat[i];
}
cout<<"\n Avg Waiting Time: "<<(double)sumw/n<<" ms";
cout<<"\n Avg Turn Around Time: "<<(double)sumt/n<<" ms";
getch();
}

OUTPUT
Enter the no. of process to be executed: 3
Enter their burst times:
P1:4
P2:3
P3:5

Implementing SJF Scheduling.

START: -->P2 -->P1 -->P3 :END

Process Burst Time Waiting Time Turn Around Time


P1 4 3 7
P2 3 0 3
P3 5 7 12

Avg Waiting Time: 3.33333 ms


Avg Turn Around Time: 7.33333 ms
c) Priority
#include<iostream.h>
#include<conio.h>
#include<limits.h>
void main()
{
clrscr();
int tpt[20],bt[20],tat[20],wt[20],p[20],n,start=0,sumb=0;
int sumw=0,sumt=0,small,pos,pt;
for(int i=0;i<20;i++)
{
tat[i]=0; wt[i]=0; p[i]=10;
bt[i]=INT_MAX;
}
cout<<"\nEnter the no. of process to be executed: ";
cin>>n;
cout<<"\nEnter their burst times & priorities(1-10): \n";
for(i=0;i<n;i++)
{
cout<<" P"<<(i+1)<<": ";
cin>>bt[i];
sumb+=bt[i];
cout<<" Priority: ";
cin>>p[i];
tpt[i]=p[i];
}
cout<<"\n Implementing non pre-emtive Priority
Scheduling.\n\n";
cout<<" START: ";
while(sumb>start)
{
pt=10;
for(i=0;i<n;i++)
{
if(tpt[i]<pt)
{
pos=i; pt=tpt[i];
}
}
cout<<" -->P"<<(pos+1);
small=bt[pos];
wt[pos]=start;
start+=small;
tat[pos]=start;
bt[pos]=INT_MAX;
tpt[pos]=10;
}
cout<<" : END";
cout<<"\n\n\n Process\tPriority\tWaiting Time\tTurn
Around Time";
for(i=0;i<n;i++)
{
cout<<"\n P"<<(i+1)<<"\t\t\t "<<p[i]<<"\t\t "
<<wt[i]<<"\t\t\t"<<tat[i];
sumw+=wt[i];
sumt+=tat[i];
}
cout<<"\n Avg Waiting Time: "<<(double)sumw/n<<" ms";
cout<<"\n Avg Turn Around Time: "<<(double)sumt/n<<" ms";
getch();
}

OUTPUT
Enter the no. of process to be executed: 3
Enter their burst times:
P1:4
Priority: 5
P2:3
Priority: 4
P3:5
Priority: 2

Implementing pre-emtive Priority Scheduling.

START: -->P3 -->P2 -->P1 :END

Process Burst Time Priority Waiting Time Turn Around


Time
P1 4 5 8 12
P2 3 4 5 8
P3 5 2 0 5

Avg Waiting Time: 4.33333 ms


Avg Turn Around Time: 8.33333 ms
Experiment 2
Simulate multiplexer/concentrator using queuing system
In telecommunications, computer networks and digital video, a statistical multiplexer
may combine several variable bit rate data streams into one constant bandwidth
signal, for example by means of packet mode communication. An inverse multiplexer
may utilize several communication channels for transferring one signal.

Program:

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

class Queue
{
public:
char ptr[20];
int rear,front;
Queue()
{
rear=-1;
front=-1;
}
void insert(char in)
{
rear++;
if(rear==0)
front=0;
ptr[rear]=in;
}
char remove()
{ return ptr[front++];
}

};
void mux(Queue q1,Queue q2,Queue q3,Queue q4)
{
Queue qout;
for(int i=0;i<3;i++)
{
qout.insert(q1.remove());
qout.insert(q2.remove());
qout.insert(q3.remove());
qout.insert(q4.remove());
}
for(i=0;i<12;i++)
{
cout<<qout.remove()<<(i/4)+1<<" ";
}
}
void main()
{
clrscr();
Queue q1,q2,q3,q4;
for(int i=0;i<3;i++)
{
q1.insert('A');
q2.insert('B');
q3.insert('C');
q4.insert('D');
}
mux(q1,q2,q3,q4);
getch();
}

Output :

A1 B1 C1 D1 A2 B2 C2 D2 A3 B3 C3 D3
Experiment 3

Simulate congestion control algorithm (Leaky Bucket)


# include<iostream.h>
# include<conio.h>
# include<dos.h>
# include<stdlib.h>
# include<time.h>
class receiver;
class bucket;
int z[6];

class sender
{
private:
int datas[6];
public:
void getbits()
{
int i;
cout<<"\nEnter the data you want to send";
for(i=0;i<6;i++)
{
cin>>datas;
}
}
void printdatas()
{
cout<<"value you have entered is\n";
for(int j=0;j<6;j++)
{
cout<<"\n"<<datas[j];
}
cout<<"\n\nsender is ready to send the data";
delay(2000);
}
friend int* enqueue(sender&,bucket&,receiver&);
};

class bucket
{
private:
int datab[4];
public:
friend int* enqueue(sender&,bucket&,receiver&);
};

class receiver
{
private:
int datar[6];
public:
void askinformation()
{
cout<<"Enter data you want to send from this end";
for(int k=0;k<6;k++)
{
cin>>datar[k];
}
cout<<"\n\nReceiver is ready to send the data to
sender";
}
void printinformation()
{
for(k=0;k<6;k++)
{
cout<<"\n"<<datar[k];
}
cout<<"value you have entered is";
}
friend int* enqueue(sender&,bucket&,receiver&);
};

int main()
{
clrscr();
int n=0;
sender s;
bucket b;
receiver r;
s.getbits();
s.printdatas();
int*k =enqueue(s,b,r);
while(n<4)
{
cout<<"\ndata received by receiver is"<<(*k);
k++;
n++;
}
getch();
return 0;
}

int* enqueue(sender& s1,bucket& b1,receiver& r1)


{
/*int n,*p;
if(n==9)
{
cout<<"\nbucket is full\n"
cout<<"\npackets will be discarded\n";
}
else{*/
int m;
int* p;
for(m=0;m<4;m++)
{
b1.datab[m]=s1.datas[m];
r1.datar[m]=b1.datab[m];
z[m]=r1.datar[m];
}
p=(z);
return p;
}

Output
Enter the data you want to send: 12 23 34 45 56 67
value you have entered is

12
23
34
45
56
67

Sender is ready to send the data

data received by receiver is 12


data received by receiver is 23
data received by receiver is 34
data received by receiver is 45
data received by receiver is 56
data received by receiver is 67
Experiment 4

Implementation of Disk Scheduling Algorithms


#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");
clearviewport();
printf("(1) LOOK (2) CLOOK (3) FCFS (0) EXIT \n
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):");
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)
{
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*/
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];
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();
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*/
getch();
}/*main*/
Output:
(1) LOOK (2) C-LOOK (3) FCFS (0) EXIT
Enter your choice:3
Enter The current head position:199
Enter no. of request:4
Enter the request:180 1 140 100

Order of service: 199 180 1 140 100


Total head movements: 378
Experiment 6
Simulation of Telephone System
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <dos.h>

extern int busy=0,s1=-1,s2=-1,s3=-1,d1=-1,d2=-1,


d3=-1,t1=0,t2=0,t3=0;

void main()
{
int link=0,block=0,success=0;
void gens1(),gend1(),gent1(),gens2(),gend2(),gent2(),
gens3(),gend3(),gent3(),tim(),wind(),print();
wind();
randomize();
do
{
delay(100);
tim();
if(link==3 && random(100)%9==0)
block++;
print();
gotoxy(56,17);
printf("%d",s1+1);
gotoxy(63,17);
printf("%d",d1+1);
gotoxy(70,17);
printf("%02d",t1);
gotoxy(56,19);
printf("%d",s2+1);
gotoxy(63,19);
printf("%d",d2+1);
gotoxy(70,19);
printf("%02d",t2);
gotoxy(56,21);
printf("%d",s3+1);
gotoxy(63,21);
printf("%d",d3+1);
gotoxy(70,21);
printf("%02d",t3);
gotoxy(72,29);
printf("%d",(success+block+busy));
gotoxy(72,31);
printf("%d",success);
gotoxy(72,33);
printf("%d",block);
gotoxy(72,35);
printf("%d",busy);
gotoxy(72,45);
printf("%d",link);

if(link==0)
{
gens1();
gend1();
gent1();
++link;
++success;
continue;
}
if(link==1)
{
gens2();
gend2();
gent2();
++link;
++success;
continue;
}
if(link==2)
{
gens3();
gend3();
gent3();
++link;
++success;
continue;
}

--t1;
--t2;
--t3;
if(t1<=0)
{
gotoxy(56,17);
printf(" ");
gotoxy(63,17);
printf(" ");
gotoxy(70,17);
printf(" ");
}
if(t2<=0)
{
gotoxy(56,19);
printf(" ");
gotoxy(63,19);
printf(" ");
gotoxy(70,19);
printf(" ");
}
if(t3<=0)
{
gotoxy(56,21);
printf(" ");
gotoxy(63,21);
printf(" ");
gotoxy(70,21);
printf(" ");
}
if(t1<=0 && (t2>=0 || t3>=0))
{
--link;
s1=s2;
d1=d2;
t1=t2;
s2=s3;
d2=d3;
t2=t3;
s3=-1;
d3=-1;
t3=0;
}
if(t2==0 && (t1>=0 || t3>=0))
{
--link;
s2=s3;
d2=d3;
t2=t3;
s3=-1;
d3=-1;
t3=0;
}
if(t3<=0)
{
--link;
}
} while(!kbhit());
}

void print()
{
textcolor(RED);
gotoxy(11,29);
cprintf("%d",s1+1);
gotoxy(11,35);
cprintf("%d",s2+1);
gotoxy(11,41);
cprintf("%d",s3+1);
gotoxy(41,29);
cprintf("%d",d1+1);
gotoxy(41,35);
cprintf("%d",d2+1);
gotoxy(41,41);
cprintf("%d",d3+1);
textcolor(s1+1);
gotoxy(10,30);
cprintf("%c%c%c",219,219,219);
textcolor(s2+1);
gotoxy(10,36);
cprintf("%c%c%c",219,219,219);
textcolor(s3+1);
gotoxy(10,42);
cprintf("%c%c%c",219,219,219);
textcolor(d1+1);
gotoxy(40,30);
cprintf("%c%c%c",219,219,219);
textcolor(d2+1);
gotoxy(40,36);
cprintf("%c%c%c",219,219,219);
textcolor(d3+1);
gotoxy(40,42);
cprintf("%c%c%c",219,219,219);
}

void gens1()
{
randomize();
s1=random(8);
}
void gend1()
{
do
{
d1=random(8);
}while(s1==d1);
}
void gent1()
{
t1=0;
t1=random(30)+1;
}
void gens2()
{
do
{
s2=random(8);
} while(s2==s1 || s2==d1);
}
void gend2()
{
do
{
d2=random(8);
if(d2==d1 || d2==s1)
busy++;
} while(d2==s2 || d2==d1 || d2==s1);
}
void gent2()
{
t2=0;
t2=random(30)+1;
}
void gens3()
{
do
{
s3=random(8);
} while(s3==s1 || s3==s2 || s3==d1 || s3==d2);
}
void gend3()
{
do
{
d3=random(8);
if(d3==s1 || d3==s2 || d3==d1 || d3==d2)
busy++;
} while(d3==s3 || d3==s1 || d3==s2 || d3==d1 || d3==d2);
}
void gent3()
{
t3=0;
t3=random(30)+1;
}
void tim()
{
struct time t;
gettime(&t);
gotoxy(39,7);
printf("%02d:%02d:%02d",t.ti_hour, t.ti_min, t.ti_sec);
}
void wind()
{
int x,y=0;
clrscr();
textcolor(YELLOW);
for(x=1;x<=79;x++)
{
gotoxy(x,1);
cprintf("%c",205);
gotoxy(x,10);
cprintf("%c",205);
gotoxy(x,50);
cprintf("%c",205);
}
for(x=1;x<=49;x++)
{
gotoxy(x,20);
cprintf("%c",205);
}
for(x=2;x<=49;x++)
{
gotoxy(1,x);
cprintf("%c",186);
gotoxy(79,x);
cprintf("%c",186);
}
for(x=11;x<=49;x++)
{
gotoxy(50,x);
cprintf("%c",186);
}
gotoxy(1,1);
cprintf("%c",201);
gotoxy(79,1);
cprintf("%c",187);
gotoxy(1,50);
cprintf("%c",200);
gotoxy(79,50);
cprintf("%c",188);
gotoxy(1,10);
cprintf("%c",204);
gotoxy(79,10);
cprintf("%c",185);
gotoxy(50,10);
cprintf("%c",203);
gotoxy(50,50);
cprintf("%c",202);
// End of window system//
textcolor(RED);
gotoxy(25,3);
cprintf("SIMULATION OF TELEPHONE SYSTEM");
gotoxy(24,5);
cprintf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
gotoxy(32,7);
textcolor(BLUE);
cprintf("CLOCK :");
// End of heading//
gotoxy(22,12);
textcolor(MAGENTA);
cprintf("TELEPHONES");
gotoxy(22,13);
cprintf("----------");
for(x=1;x<=8;x++)
{
textcolor(x);
gotoxy(y+3,15);
cprintf("%c%c%c",219,219,219);
textcolor(RED);
gotoxy(y+4,17);
cprintf("%d",x);
y=y+6;
}
//End of telephones//
textcolor(MAGENTA);
gotoxy(17,22);
cprintf("TELEPHONE SWITCHING");
gotoxy(17,23);
cprintf("-------------------");
y=0;
for(x=1;x<=3;x++)
{
gotoxy(14,30+y);
printf("%c-----------------------%c",174,175);
gotoxy(23,29+y);
textcolor(GREEN);
cprintf("LINK %d",x);
y=y+6;
}
//End of telephone switching//
textcolor(MAGENTA);
gotoxy(52,12);
cprintf("LIST OF CALLS IN PROGRESS");
gotoxy(52,13);
cprintf("-------------------------");
gotoxy(55,15);
textcolor(BLUE);
cprintf("FROM TO DURATION");
textcolor(YELLOW);
for(x=0;x<=21;x++)
{
gotoxy(x+54,16);
cprintf("%c",196);
gotoxy(x+54,18);
cprintf("%c",196);
gotoxy(x+54,20);
cprintf("%c",196);
gotoxy(x+54,22);
cprintf("%c",196);
}
for(x=0;x<=4;x++)
{
gotoxy(53,x+17);
cprintf("%c",179);
gotoxy(60,x+17);
cprintf("%c",179);
gotoxy(66,x+17);
cprintf("%c",179);
gotoxy(76,x+17);
cprintf("%c",179);
}
//end of progress calls//
textcolor(MAGENTA);
gotoxy(60,26);
cprintf("COUNTERS");
gotoxy(60,27);
cprintf("--------");
gotoxy(53,29);
printf("Processed Calls : ");
gotoxy(53,31);
printf("Successful Calls : ");
gotoxy(53,33);
printf("Blocked Calls : ");
gotoxy(53,35);
printf("Busy Calls : ");
gotoxy(62,40);
textcolor(MAGENTA);
cprintf("LINKS");
gotoxy(62,41);
cprintf("-----");
gotoxy(53,43);
printf("Maximum Links : 3");
}
Output:
Experiment 5
Simulation of a manufacturing shop
Implemented in VC++:
#include <iostream.h>
#include <queue>
#include<stdlib>
int main()
{
queue<int> orders;
queue<int> raw;
queue<int> prod;

do
{
raw.push(rand()*50);
orders.push(rand*50);
cout<<"\nNo. of orders : "<<orders.front();
cout<<"\nUnits of raw material available : "
<<raw.front();
if(orders.front()<=raw.front())
{
int quantity_left=raw.front();
quantity_left-=orders.front();
prod.push(orders.front());
raw.pop();
orders.pop();
raw.push(quantity_left());
}
else
{
prod.push(raw.front());
int pending=orders.front();
pending-=raw.front();
raw.pop();
orders.pop();
orders.push(pending);
}
}while(orders.front()!=0);
cout<<"\nThe order of productions are : ";
while(!prod.empty())
cout<<" "<<prod.front();

return 0;
}
Output :
No. of orders : 5
Units of raw material available : 6
No. of orders : 2
Units of raw material available : 1
The order of productions are : 5 2
Experiment 7
Simulation of Inventory Control System
//inventory simulation
#include<conio.h>
#include<dos.h>
#include<iostream.h>
#include<stdlib.h>
class Shop{
int seats;
int customers_seated;
int customers_waiting;
int totalServed;
public:
void init(int s,int cs,int cw){
seats=s;
customers_seated=cs;
customers_waiting=cw;
totalServed=0;
cout<<"Welcome to icecream shop";
cout<<"\nseats:"<<s;
cout<<"\nCustomers inside shop:"<<(cs+cw);
cout<<"\nPress any key to start simulation...";
getch();
clrscr();
}
void addCustomer(){
if(seats==customers_seated){
customers_waiting++;
}
else{
customers_seated++;
}
}
void serveCustomer(){
int someoneServed=0;
if(rand()%11>5)
{
if(customers_waiting!=0)
{
customers_waiting--;
someoneServed=1;
}
else{
if(customers_seated!=0)
{
customers_seated--;
someoneServed=1;
}
}
if(someoneServed)
totalServed++;
}
}
void showStatus(){
clrscr();
cout<<"\nSeats :
"<<seats;
cout<<"\nNumber of Customers seated currently for being served
: "<<customers_seated;
cout<<"\nNumber of Customers waiting currently for being
seated: "<<customers_waiting;
cout<<"\nNumber of Customers who were served and left the shop
: "<<totalServed;
}
};

void main(){
clrscr();
Shop icecreamShop;
icecreamShop.init(20,0,0);
while(!kbhit()){
if(rand()%11>5)
{
icecreamShop.addCustomer();
}
icecreamShop.showStatus();
delay(500);
if(rand()%11<5)
{
icecreamShop.serveCustomer();
}
icecreamShop.showStatus();
delay(500);
}
}

Output :
Welcome to icecream shop
Seats: 20
Customers inside shop: 0
Press any key to start simulation...

Seats
:20
Number of Customers seated currently for being served : 13
Number of Customers waiting currently for being seated : 0
Number of Customers who were served and left the shop : 3
Experiment 10
Distributed Lag Model and Cobweb model

COBWEB Model

The cobweb model or cobweb theory is an economic model that explains why prices
might be subject to periodic fluctuations in certain types of markets. It describes
cyclical supply and demand in a market where the amount produced must be chosen
before prices are observed. Producers' expectations about prices are assumed to be
based on observations of previous prices. Nicholas Kaldor analyzed the model in
1934, coining the term 'cobweb theorem' (see Kaldor, 1938 and Pashigian, 2008),
citing previous analyses in German by Henry Schultz and U. Ricci.

The model

The cobweb model is based on a time lag between supply and demand decisions.
Agricultural markets are thought to be a situation where the cobweb model might
apply, since there is a lag between planting and harvesting. Suppose for example that
as a result of unexpectedly bad weather, farmers go to market with an unusually small
crop of strawberries (this example follows Kaldor, 1934, p. 134). This shortage,
equivalent to a leftward shift in the market's supply curve, results in high prices. If
farmers expect these high price conditions to continue, then in the following year,
they will raise their production of strawberries relative to other crops. Therefore when
they go to market the supply will be high, resulting in low prices. If they then expect
low prices to continue, they will decrease their production of strawberries for the next
year, resulting in high prices again.

This process is illustrated by the diagram on the right. The equilibrium price is at the
intersection of the supply and demand curves. A poor harvest in period 1 means
supply falls to Q1, so that prices rise to P1. If producers plan their period 2 production
under the expectation that this high price will continue, then the period 2 supply will
be high, at Q2. Prices therefore fall to P2 when they try to sell all their output. Notice
that as this process repeats itself, oscillating between periods of low supply with high
prices and then high supply with low prices, the price and quantity spiral inwards and
the economy converges to the equilibrium price where supply and demand cross.

Simplifying, the cobweb model can have two main types of outcomes:

If the slope of the supply curve is greater than the slope of the demand curve (in
absolute value), then the fluctuations decrease in magnitude with each cycle, so a plot
of the prices and quantities over time would look like an inward spiral, as shown in
the diagram. This is called the stable or convergent case.
If the slope of the supply curve is less than the slope of the demand curve (in absolute
value), then the fluctuations increase in magnitude with each cycle, so that prices and
quantities spiral outwards. This is called the unstable or divergent case.
Two other possibilities are:
Fluctuations may also remain of constant magnitude, so a plot of the equilibria would
produce a simple rectangle, if the supply and demand curves have exactly the same
slope.
If the supply curve is less steep than the demand curve near the point where the two
curves cross, but more steep when we move sufficiently far away, then prices and
quantities will spiral away from the equilibrium price but will not diverge indefinitely;
instead, they may converge to a limit cycle.
In either of the first two scenarios, the combination of the spiral and the supply and
demand curves often looks like a cobweb, hence the name of the theory.
Distributed Lag Model

In statistics and econometrics, a distributed lag model is a model for time series data
in which a regression-like equation is used to predict current values of a dependent
variable based on both the current values of an explanatory variable and the lagged
(past period) values of this explanatory variable.

Unstructured estimation

The simplest way to estimate an equation with a distributed lag is by ordinary least
squares, putting a pre-determined number of lagged values of the independent
variable on the right-hand side of the regression equation, without imposing any
structure on the relationship of the values of the lagged-variable coefficients to each
other. A problem that usually arises with this approach is multicollinearity: the
various lagged values of the independent variable are so highly correlated with each
other that their coefficients can be estimated only very imprecisely. Therefore
researchers often impose structure on the shape of the distributed lag (the relation of
the lagged-variable coefficients to each other). Such a structure is called a distributed
lag model.

Structured distributed lag models come in two types: finite and infinite. Infinite
distributed lags allow the value of the independent variable at a particular time to
influence the dependent variable infinitely far into the future, or to put it another way,
they allow the current value of the dependent variable to be influenced by values of
the independent variable that occurred infinitely long ago; but beyond some lag length
the effects taper off toward zero. Finite distributed lags allow for the independent
variable at a particular time to influence the dependent variable for only a finite
number of periods.

Finite distributed lags

The most important finite distributed lag model is the Almon lag[3]. This model
allows the data to determine the shape of the lag structure, but the researcher must
specify the maximum lag length; an incorrectly specified maximum lag length can
distort the shape of the estimated lag structure as well as the cumulative effect of the
independent variable.

Infinite distributed lags

The most common type of infinite distributed lag model is the geometric lag, also
known as the Koyck lag. In this lag structure, the weights (magnitudes of influence)
of the lagged independent variable values decline exponentially with the length of the
lag; while the shape of the lag structure is thus fully imposed by the choice of this
technique, the rate of decline as well as the overall magnitude of effect are determined
by the data. Specification of the regression equation is very straightforward: one
includes as explanators (right-hand side variables in the regression) the one-period-
lagged value of the dependent variable and the current value of the independent
variable. If the estimated coefficient of the lagged dependent variable is λ and the
estimated coefficient of the current value of the independent variable is b, then the
estimated short-run (same-period) effect of a unit change in the independent variable
is b while the long-run (cumulative) effect of a sustained unit change in the
independent variable is b / (1 − λ).
Experiment 9
Study of simulation languages

GPSS

General Purpose Simulation System (GPSS) (originally Gordon's Programmable


Simulation System after creator Geoffrey Gordon. The name was changed when it
was decided to release it as a product) is a discrete time simulation language, where a
simulation clock advances in discrete steps. A system is modelled as transactions
enter the system and are passed from one service (represented by blocs) to another.
This is particularly well suited for problems such as a factory. It was popular in the
late 1960s and early 1970s but is little used today. GPSS is less flexible than
simulation languages such as Simula and SIMSCRIPT II.5.

The General Purpose Simulation System (GPSS) is a programming system designed


for the simulation of discrete systems. These are systems that can be modeled as a
series of state changes that occur instantaneously, usually over a period of time.
Complexities in their analysis arise because there are many elements in the system,
and there is competition for limited system resources. The simulation technique uses
numerical computation methods to follow the system elements through their changes
of state, and predicts properties of the system from measurements on the model.
GPSS came into existence rapidly, with virtually no planning, and surprisingly little
effort. It came rapidly because it filled an urgent need that left little time for exploring
alternatives. The lack of planning came from a happy coincidence of a solution
meeting its problem at the right time. The economy of effort was based on a
background of experience in the type of application for which the language was
designed, both on the part of the designer and the early users.

SIMSCRIPT

SIMSCRIPT is a free-form, English-like general-purpose simulation language


conceived by Harry Markowitz and Bernard Hausner at the RAND Corporation in
1963. It was implemented as a Fortran preprocessor on the IBM 7090 and was
designed for large discrete event simulations. It influenced Simula.

Though earlier versions were released into the public domain, SIMSCRIPT was
commercialized by Markowitz's company, California Analysis Center, Inc., which
produced proprietary versions SIMSCRIPT I.5 and SIMSCRIPT II.5.
As an aid to making important decisions, the use of computer simulation has grown at
an astonishing rate since its introduction. Simulation is now used in manufacturing,
military, nuclear applications, models relating to urban growth, hydroelectric
planning, transportation systems, election redistricting, cancer and tuberculosis
studies, hospital planning, communications, and multi-computer networks.
SIMSCRIPT III is suitable for building high-fidelity simulation models especially
very large models. SIMSCRIPT III modularity and object-orientated concepts
simplify development, maintenance and code reuse.
SIMSCRIPT III is a language designed specifically for simulation. It is the most
efficient and effective program development technique for simulation, due to the
following properties:
 Portability. SIMSCRIPT III development environment, which includes
Development Studio, language compiler and Graphical systems are available on
the various computer systems. This facilitates the development of general-purpose
models and simulation applications that can be moved easily from one site to
another and from one organization to another.

 Appropriate Constructs. SIMSCRIPT III provides constructs designed especially


for simulation (e.g., classes, objects, object attributes, processes, resources, events,
entities, and sets). These constructs make it easier to formulate a simulation
model. Implementation of the simulation program is also quicker because these
powerful tools do not have to be invented anew.

 Self-Documenting Language. Applications developed using the SIMSCRIPT III


language is characteristically easy to read and understand. The language
encourages this because it is oriented toward the kinds of problems being solved
rather than the machines being used as tools. The very high-level language
features of SIMSCRIPT III were designed to make it possible to manage a
complex simulation models.

 Error Detection. SIMSCRIPT III performs a number of error checks that help to
assure that a simulation model is running correctly. Powerful inline symbolic
debugger speeds up run-time analysis of model behavior.

 When an error in a run is detected, model enters SIMSCRIPT III symbolic


debugger, which allows program status investigation, which includes the names
and values of variables, system status, and other valuable information. This
reduces the time spent in developing and testing programs.

 Statistical Tools. Along with the mathematical and statistical functions most often
used in simulation (exponential functions, random number generators, and so on),
SIMSCRIPT III includes the accumulate and tally statements that allow the model
builder to collect statistics on key variables in his model.

 Simulation Graphics. Brings interactive animated and display graphics to the


SIMSCRIPT III models. Graphical objects can be easily added to the program
providing automatic animation and information display. Input/ Output dialog
boxes, menu bars, pallets can easily be added to the model providing elegant and
functional Graphical User Interfaces.

 Data Base Connectivity. Provides SIMSCRIPT III Application Program Interfaces


(API’s) to the major databases available on the market: Microsoft Access, SQL
Server Oracle, IBM DB2 and IBM Informix.

 Operating System Interface. Provides SIMSCRIPT III Application Program


Interfaces (API’s) to Operating System Services facilitating portable models
across all SIMSCRIPT III supported computer platforms.
 Open System. SIMSCRIPT III provides possibility to call non-simscript
routines/functions from a SIMSCRIPT model. This facilitates usage of libraries
written in C/C++ , Java or FORTRAN from SIMSCRIPT models.

 Complete Methodology. The SIMSCRIPT III approach to simulation model


development provides the complete set of capabilities needed to develop a
simulation model. A simulation model developed in the SIMSCRIPT III
programming language is readable by the analyst familiar with the system under
study.

 Support. CACI provides SIMSCRIPT III software, documentation, training and


technical support. Model development services are also available from CACI.
Experiment 8
Simulation of pseudorandom number generator

A pseudorandom number generator (PRNG), also known as a deterministic random


bit generator, is an algorithm for generating a sequence of numbers that approximates
the properties of random numbers. The sequence is not truly random in that it is
completely determined by a relatively small set of initial values, called the PRNG's
state. Although sequences that are closer to truly random can be generated using
hardware random number generators, pseudorandom numbers are important in
practice for simulations (e.g., of physical systems with the Monte Carlo method), and
are central in the practice of cryptography and procedural generation. Common
classes of these algorithms are linear congruential generators, Lagged Fibonacci
generators, linear feedback shift registers, feedback with carry shift registers, and
generalised feedback shift registers. Recent instances of pseudorandom algorithms
include Blum Blum Shub, Fortuna, and the Mersenne twister.

Careful mathematical analysis is required to have any confidence a PRNG generates


numbers that are sufficiently "random" to suit the intended use. Robert R. Coveyou of
Oak Ridge National Laboratory once titled an article, "The generation of random
numbers is too important to be left to chance." As John von Neumann joked, "Anyone
who considers arithmetical methods of producing random digits is, of course, in a
state of sin."

Implementation:
#include<conio.h>
#include<iostream.h>
void main()
{
int a=21,b=53,r;
int seed = 52;
r=seed;
cout<<"Ten random numbers are : \n";
for(int i=0;i<10;i++)
{
r=(a*r+b)%100;
cout<<r<<" ";
}
}

Output:
Ten random numbers are :
45 98 11 84 17 10 63 76 49 82

You might also like