Ecs 353 Numerical Techniques
Ecs 353 Numerical Techniques
Ecs 353 Numerical Techniques
LAB MANUAL
Page 1 of 28
INDEX
6 RR (Round Robin)
18-20
7 Page Replacement
21-24
Page 2 of 28
PRACTICAL -1
Page 3 of 28
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,at,bt,temp,temp1,temp2,awt[50],c,c_loc; floatawtime,att[50],burst,attime,a_wait;
int mat[50][3]; clrscr();
printf("Enter the no. of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{ mat[i][0]=i+1;
printf("Enter arrival time and burst time for %d process:\n",i+1);
for(j=1;j<3;j++)
scanf("%d",&mat[i][j]);
}
printf("\nProcess \tArrival time \t Burst time\n"); for(i=0;i<n;i++)
{ for(j=0;j<3;j++)
printf("%d\t ",mat[i][j]); printf("\n");
}
printf("\nAfter sorting processes acc. to arrival time:\n"); for(i=0;i<n-1;i++)
{
c=mat[i][1];
c_loc=i;
for(j=i+1;j<n;j++)
{ if(mat[j][1] < c)
{ c= mat[j][1];
c_loc=j;
}
}
if(i!=c_loc)
{ temp=mat[c_loc][1]; mat[c_loc][1]=mat[i][1]; mat[i][1]=temp; temp1=mat[c_loc][2];
mat[c_loc][2]=mat[i][2]; mat[i][2]=temp1; temp2=mat[c_loc][0]; mat[c_loc][0]=mat[i][0];
mat[i][0]=temp2;
}
}
printf("\nProcess \tArrival time \t Burst time\n");
for(i=0;i<n;i++)
{ for(j=0;j<3;j++)
printf("%d\t ",mat[i][j]);
printf("\n");
}
awt[0]=0;
a_wait=mat[0][1];
for(i=0;i<n-1;i++)
{ awt[i+1]=a_wait+(mat[i][2]-mat[i+1][1]);
a_wait=a_wait+mat[i][2];
}
Page 4 of 28
for(i=0;i<n;i++)
awtime=awtime+awt[i]; awtime=awtime/n;
printf("\nAvg. Waiting time= %f",awtime);
att[0]=mat[0][2]-mat[0][1];
burst=mat[0][2];
for(i=1;i<n;i++)
{ burst=burst+mat[i][2]; att[i]=(burst-mat[i][1]);
}
for(i=0;i<n;i++)
attime=attime+att[i];
attime=attime/n;
printf("\nAvg. Turnaround time=%f",attime);
getch();
}
Output
Enter the no. of processes:3
Enter arrival time and burst time for 1 process: 0
24
Enter arrival time and burst time for 2 process: 0
3
Enter arrival time and burst time for 3 process: 0
3
Page 5 of 28
PRACTICAL 2
Step 5: Set the waiting time of the first process as ‘0’ and its turnaround time as its
burst time.
Step 6: For each process in the ready queue, calculate
(c) Waiting time for process(n)= waiting time of process (n-1) + Burst time of
process(n-1)
(d) Turn around time for Process(n)= waiting time of Process(n)+ Burst time
for process(n)
Step 6: Calculate
(c) Average waiting time = Total waiting Time / Number of process
(d) Average Turnaround time = Total Turnaround Time / Number of process
Step 7: Stop the process
Page 6 of 28
Programme
#include<stdio.h>
#include<conio.h>
void sort(int a[],int b[],int n);
void main()
{ int a[10],b[10],i,n,c[10],j,temp;
floatawt=0.0,att=0.0,sw=0.0,st=0.0; c[0]=0;
clrscr();
printf("enter number of processes to be exexuted:");
scanf("%d",&n);
printf("enter arrival time and burst time of\n:");
for(i=1;i<=n;i++){
printf("\nP%d",i); printf("\narrival time:"); scanf("%d",&a[i]); printf("\nburst time:");
scanf("%d",&b[i]);
}
sort(a,b,n);
c[1]=c[0]+b[1];
sw=sw+(c[0]-a[1]);
st=st+(c[1]-a[1]); sort(b,a,n);
for(i=2;i<=n;i++) {
c[i]=c[i-1]+b[i];
}
for(i=2;i<=n;i++)
{ sw=sw+(c[i-1]-a[i]);
st=st+(c[i]-a[i]);
}
awt=sw/n; att=st/n;
printf("\n\naverage waiting time is:%f",awt);
printf("\naverage turnaround time is:%f",att); getch();
}
void sort(int a[],int b[],int n)
{ inti,j,temp,t; for(i=2;i<=n;i++)
{ for(j=i+1;j<=n;j++)
{ if(a[i]>a[j])
{ temp=a[i];
a[i]=a[j]; a[j]=temp;
t=b[i]; b[i]=b[j]; b[j]=t;
}
}
}
}
Page 7 of 28
Output
Enter arrival time and burst time of:P1 arrival time:0
burst time:10 P2
arrival time:1 burst time:6 P3
arrival time:3 burst time:2 P4
arrival time:5
burst time:4
average waiting time is:7.250000 average turnaround time is:12.750000
Page 8 of 28
PRACTICAL - 3
Page 9 of 28
Programme
#include<stdio.h>
#include<conio.h>
void main()
{
intarr[10],bur[10],tot[10],pri[10],x=0,i,j,n,temp; floatawt = 0, tt =0;
printf("\nEnter no of processes\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter arrival time and burst and priority for %d process\n",i);
scanf("%d",&arr[i]);
scanf("%d",&bur[i]);
scanf("%d",&pri[i]);
}
for(i=0;i<n-1;i++)
for(j=0;j<(n-i-1);j++)
if(arr[j]>arr[j+1])
{
temp =arr[j]; arr[j]=arr[j+1]; arr[j+1] = temp; temp =bur[j]; bur[j]=bur[j+1]; bur[j+1] = temp;
temp =pri[j];
else
Page 10 of 28
}
printf("Average waiting time = %f",awt); printf("Average turn around time = %f",tt); getch();
}
Output
Enter no of processes 4
Enter arrival time and burst and priority for 0 process 0 10 4
Enter arrival time and burst and priority for 1 process 1 6 3
Enter arrival time and burst and priority for 2 process 3 2 1
Enter arrival time and burst and priority for 3 process 5 4 2
Table -
Arrival time = 0 Burst time = 10 Priority = 4 Arrival time = 1 Burst time = 6 Priority = 3
Arrival time = 3 Burst time = 2 Priority = 1
Arrival time = 5 Burst time = 4 Priority = 2Average waiting time = 6.250000
Average turn around time = 10.250000
Page 11 of 28
PRACTICAL - 4
When a new process enters a system, it must declare the maximum number of instances of
each resource type it needed. This number may exceed the total number of resources in the
system. When the user request a set of resources, the system must determine whether the
allocation of each resources will leave the system in safe state. If it will the resources are
allocation; otherwise the process must wait until some other process release the resources.
Data structures
n-Number of process, m-number of resource types.
Available: Available[j]=k, k – instance of resource type Rj is available.
Max: If max[i, j]=k, Pi may request at most k instances resource Rj.
Allocation: If Allocation [i, j]=k, Pi allocated to k instances of resource Rj
Need: If Need[I, j]=k, Pi may need k more instances of resource type Rj,
Need[I, j]=Max[I, j]-Allocation[I,j];
Safety Algorithm
1. Work and Finish be the vector of length m and n respectively,
Work=Available and Finish[i]=False.
2. Find an i such that both
Finish[i]=False
Need<=Work
If no such I exists go to step 4.
3. work=work+Allocation, Finish[i]=True;
4. if Finish[1]=True for all I, then the system is in safestate.
Page 12 of 28
If the resulting resource allocation state
is safe, the transaction is completed and
process Pi is allocated its resources.
However if the state is unsafe, the Pi
must wait for Request i and the old
resource-allocation state is restored.
ALGORITHM:
1. Start the program.
2. Get the values of resources and processes.
3. Get the avail value.
4. After allocation find the need value.
5. Check whether its possible to allocate.
6. If it is possible then the system is in safe state.
7. Else system is not in safety state.
8. If the new request comes then check that the system is in safety.
9. or not if we allow there quest.
10. stop the program.
Page 13 of 28
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
inti,j,all[4][2],max[4][2],need[4][2],ava[2],finish[4],flag=1;
clrscr();
for(i=0;i<4;i++)
{
printf("\nEnter the values for process %d \n",i); printf("\nEnter allocation\n");
for(j=0;j<2;j++) scanf("%d",&all[i][j]); printf("\nEnter max need\n"); for(j=0;j<2;j++)
{
scanf("%d",&max[i][j]);
need[i][j] = max[i][j] - all[i][j];
}
finish[i] = 0;
}
printf("\nEnter total available resources \n"); for(j=0;j<2;j++)
scanf("%d",&ava[j]); for(j=1;j<=3;j++)
for(i=0;i<4;i++)
{
if(need[i][0]<=ava[0]&&need[i][1]<=ava[1]&&finish[i]==0)
{
ava[0] =ava[0]+all[i][0];
ava[1] =ava[1]+all[i][1]; finish[i]=1;
printf("P%d-->",i);
}
}
for(i=0;i<4;i++)
{
if(finish[i]==0)
{
flag =0; break;
}
}
if(flag ==1)
printf("\nSystem is in safe state"); else
printf("\nSystem is in unsafe state"); getch();
}
Page 14 of 28
Output
Enter the values for process 0 Enter allocation
24
Enter max need 3 5
Enter the values for process 1 Enter allocation
42
Enter max need 5 3
Enter the values for process 2 Enter allocation
45
Enter max need 1 3
Enter the values for process 3 Enter allocation
89
Enter max need 11 12
Enter total available resources 12 12
P0-->P1-->P2-->P3-->
System is in safe state
Page 15 of 28
PRACTICAL - 5
ALGORITHM:
Step 1: Create a queue to hold all pages in memory
Step 2: When the page is required replace the page at the head of the queue
Step 3: Now the new page is inserted at the tail of the queue
Page 16 of 28
PROGRAM
#include<stdio.h>
#include<conio.h>
intfr[20],frsize;
void main()
{
void display();
int p[50],i,j,fs[20],n;
int index,k,l,flag1=0,flag2=0; floatpfr,pf=0;
clrscr();
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&frsize);
for(i=0;i<frsize;i++)
fr[i]=-1; for(j=0;j<n;j++)
{
flag1=0,flag2=0;
for(i=0;i<frsize;i++)
if(fr[i]==p[j])
{
flag1=1; flag2=1; break;
}
if(flag1==0) //if there is an empty place
{
for(i=0;i<frsize;i++)
if(fr[i]==-1)
{
fr[i]=p[j]; flag2=1;
pf++; break;
}
}
if(flag2==0){
for(i=0;i<frsize;i++)
fs[i]=0;
for(k=j-1,l=1;l<=frsize-1;l++,k--)
{
for(i=0;i<frsize;i++)
{
if(fr[i]==p[k]) fs[i]=1;
}
}
for(i=0;i<frsize;i++)
{
Page 17 of 28
if(fs[i]==0) index=i;
}
fr[index]=p[j]; pf++;
}
display();
}
pfr=pf/frsize;
printf("\n no of page faults :%f",pf);
printf("\n page fault rate : %f", pfr); getch();
}
void display()
{
inti;
printf("\n");
for(i=0;i<frsize;i++)
printf("\t%d",fr[i]);
}
Output
ENTER THE NUMBER OF FRAMES : 3
7 -1 -1
7 0 -1
7 0 1
2 0 1
2 0 1
2 0 3
2 0 3
4 0 3
4 0 2
4 3 2
0 3 2
0 3 2
0 3 2
1 3 2
1 3 2
1 0 2
1 0 2
1 0 7
1 0 7
1 0 7
no of page faults :12.000000 page fault rate : 4.000000
Page 18 of 28
PRACTICAL - 6
(b) Turn around time for process(n) = waiting time of process(n) + burst time
of process(n)+ the time difference in getting CPU from process (n).
Step 7: Calculate
(a) Average waiting time = Total waiting Time / Number of process
(b) Average Turnaround time = Total Turnaround Time / Number of process
Page 19 of 28
PROGRAM
#include<conio.h>
#include<stdio.h>
void main()
{
intprcs[10],n,i,arv[10],brst[10],temp,tempb,tempp,sumb,j;
inttempa,t,time,wt[10],wtt[10],max,maxi,y=1; floatst=0.0,tt=0.0,wat=0.0;
clrscr();
printf("enter the number of processes"); scanf("%d",&n);
for(i=0;i<n;i++)
{prcs[i]=i;
printf("\nenter the arrival time for P%d",i); scanf("%d",&arv[i]);
printf("enter the burst time for P%d",i); scanf("%d",&brst[i]);
wt[i]=0;
wtt[i]=0;}
printf("enter the time slice");
scanf("%d",&time);
printf("process\t arrival time \t burst time\n");
for(i=0;i<n;i++)
{printf("P%d\t %d \t\t %d\n",prcs[i],arv[i],brst[i]);
} printf("\n");
for(i=0;i<n;i++)
{for(j=0;j<n-1;j++)
{if(arv[j]>arv[j+1])
{tempa=arv[j]; arv[j]=arv[j+1]; arv[j+1]=tempa; tempb=brst[j]; brst[j]=brst[j+1];
brst[j+1]=tempb; tempp=prcs[j]; prcs[j]=prcs[j+1]; prcs[j+1]=tempp; }}}
max=brst[0]; maxi=0;
for(i=1;i<n;i++)
{if(max<brst[i])
Page 20 of 28
{max=brst[i];
maxi=i;} } while( y>0 )
{
for(i=0;i<n;i++)
{if (brst[i]>0)
{if (brst[i]>time)
{brst[i]=brst[i]-time; st=time+st; wt[i]=wt[i]+time;}
else
{
wtt[i]=st-wt[i]; st=st+brst[i];
printf("wtt of %d is %d",i,wtt[i]); wat=wat+wtt[i]-arv[i];
tt=tt+st-arv[i];
printf("\nfor %d waiting time is %f \nturn around time is %f",i,wat/n,tt/n); brst[i]=0;
}
}
if(brst[maxi]==0) y=0;
}
}
printf("\nwaiting time is %f \nturn around time is %f",wat/n,tt/n);
getch();
} OUTPUT
Enter the number of processes 3
Enter the arrival time for P0 0
Enter the burst time for P0 24
Enter the arrival time for P1 0
Enter the burst time for P1 3
Enter the arrival time for P2 0
Enter the burst time for P2 Enter the time slice 4 3
process arrival time burst time P0 0 24
P1 0 3
P2 0 3
Page 21 of 28
waiting time is 5.666667 turnaround time is 15.666667
PRACTIAL - 7
Page 22 of 28
PROGRAM
#include<stdio.h>
#include<conio.h>
int a[21],f[8],n,k;
void main()
{
intft,c,i,j,p,m,min; clrscr();
printf("enter the no. of frames\t");
scanf("%d",&k);
printf("enter the length of string\t");
scanf("%d",&n);
printf("enter the %d string enteries\t",n);
for(i=1;i<=n;i++)
{scanf("%d",&a[i]);}
for(i=1;i<=k;i++)
{f[i]=-1;}
p=0;
ft=1;
for(i=1;i<=n;i++)
{c=0;
for(j=1;j<=k;j++)
{if(a[i]==f[j])
{c=1;}}
if (c==1)
{p=p;}
Page 23 of 28
else
{if(f[i]==-1)
{f[ft]=a[i]; ft++;}
else
{min=selminp(i); f[min]=a[i];}
p++;
}
printf("\nelements in frame after %d iteration:\t",i);
for(m=1;m<=k;m++)
{if (f[m]==-1)
printf("NIL\t"); else
printf("%d\t",f[m]);}
}
printf("\npage faults are %d",p); printf("\npage fault rate is %f",p/2.0); getch();
}
intselminp(intcurposit)
{inti,m,futp[10],ch,t,c; for(i=1;i<=k;i++)
for(m=1;m<=k;m++)
{c=0;
for(i=curposit+1;(i<=n)&&(c==0);i++)
{if (f[m]==a[i])
{futp[m]=n-i+1;
c=1;}
else
{futp[m]=-1;}
}
}
t=futp[1]; ch=1;
for(i=2;i<=k;i++)
{if(t>futp[i])
Page 24 of 28
{t=futp[i];
ch=i;}} returnch;
}
Output
PRACTICAL - 8
Algorithm:
Step 1: Start.
Step 2: Let n be the size of the buffer
Step 3: check if there are any producer
Step 4: if yes check whether the buffer is full
Step 5: If no the producer item is stored in the buffer
Step 6: If the buffer is full the producer has to wait
Step 7: Check there is any consumer. If yes check whether the buffer is empty
Step 8: If no the consumer consumes them from the buffer
Step 9: If the buffer is empty, the consumer has to wait.
Step 10: Repeat checking for the producer and consumer till required
Step 11: Terminate the process
Page 26 of 28
PROGRAM
#include<stdio.h>
#include<conio.h>
main()
{
int n,m[20],i,j,sb[20],s[20],b[20][20],x;
clrscr();
printf("Enter no. of files:");
scanf("%d",&n); for(i=0;i<n;i+
+)
{ printf("Enter starting block and size of file%d:",i+1);
scanf("%d%d",&sb[i],&s[i]);
printf("Enter blocks occupied by file%d:",i+1);
scanf("%d",&m[i]);
printf("enter blocks of file%d:",i+1);
for(j=0;j<m[i];j++)
scanf("%d",&b[i][j]);
} printf("\nFile\t index\tlength\n");
for(i=0;i<n;i++)
{
printf("%d\t%d\t%d\n",i+1,sb[i],m[i]);
}printf("\nEnter file name:");
scanf("%d",&x);
printf("file name is:%d\n",x);
i=x-1;
printf("Index is:%d",sb[i]);
printf("Block occupied are:");
for(j=0;j<m[i];j++)
printf("%3d",b[i][j]);
getch();
}
Page 27 of 28
OUTPUT
Page 28 of 28