Uploaded by Mohammaed Ridwan

OS LAB REPORT FINAL

advertisement
Problem No
01
Date
24-04-19
Problem Name: First-Come First-Serve (FCFS) process scheduling algorithm without arrival
time.
Objectives: To implement FCFS scheduling algorithm using a FIFO queue which is nonpreemptive that is if a process started then CPU executes the process until it ends.
Introduction: The FCFS is a non-preemptive scheduling algorithm in which a process is
automatically queued and processing occurs according to an incoming request or process order
implementation of the FCFS policy is managed with a FIFO queue. When a process enters the
ready queue, its PCB is linked onto the tail of the queue. When the CPU is free, it is allocated to
the process at the head of the queue.
Algorithm:
Step 1: Input the processes along with their burst time(bt).
Step 2: Find waiting time (wt) for all processes.
Step 3: At first process that comes need not to wait so waiting time for process 1 will be 0 i.e.
wt[0]=0.
Step 4: Find waiting time for all other processes i.e. for process I will be wt[i]= wt[i-1]+bt[i-1].
Step 5: Find turnaround time= waiting time + burst time for all processes.
Step 6: Find average waiting time = total waiting time/ no of process.
Step 7: Similarly, find average turnaround time = total turnaround time/no of process.
Program Code:
#include<stdio.h>
int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter the total number of processes(maximum 20):");
scanf("%d",&n);
printf("\nEnter Process Burst Time\n");
for(i=0;i<n;i++)
1
{
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}
printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
}
avwt/=i;
avtat/=i;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\n\nAverage Turnaround Time:%d",avtat);
return 0;
}
2
Output:
Conclusion: FCFS provides an efficient, simple and error-free process scheduling algorithm that
saves valueable CPU resources.
3
Problem No
02
Date
24-04-19
Problem Name: First-Come First-Serve (FCFS) process scheduling algorithm with arrival time.
Objectives: To implement FCFS scheduling algorithm using a FIFo queue which is nonpreemptive that is if a process started then CPU executes the process until it ends.
Introduction: The FCFS is a non-preemptive scheduling algorithm in which a process is
automatically queued and processing occurs according to an incoming request or process order
implementation of the FCFS policy is managed with a FIFO queue. When a process enters the
ready queue, its PCB is linked onto the tail of the queue. When the CPU is free, it is allocated to
the process at the head of the queue.
Algorithm:
Step 1: Input the processes along with their burst time(bt).
Step 2: Find waiting time (wt) for all processes.
Step 3: At first process that comes need not to wait so waiting time for process 1 will be 0 i.e.
wt[0]=0.
Step 4: Find waiting time for all other processes i.e. for process I will be wt[i]= wt[i-1]+bt[i-1].
Step 5: Find turnaround time= waiting time + burst time for all processes.
Step 6: Find average waiting time = total waiting time/ no of process.
Step 7: Similarly, find average turnaround time = total turnaround time/no of process.
Program Code:
#include<stdio.h>
int main()
{
int time,bt[10],at[10],sum_bt=0,smallest,n,i;
int sumt=0,sumw=0;
printf("no of processes : ");
scanf("%d",&n);
4
for(i=0;i<n;i++)
{
printf("arrival time for process P%d : ",i+1);
scanf("%d",&at[i]);
printf("burst time for process P%d : ",i+1);
scanf("%d",&bt[i]);
sum_bt+=bt[i];
}
bt[9]=9999;
for(time=0;time<sum_bt;)
{
smallest=9;
for(i=0;i<n;i++)
{
if(at[i]<=time && bt[i]>0 && bt[i]<bt[smallest])
smallest=i;
}
printf("P[%d]\t|\t%d\t|\t%d|\t%d\n",smallest+1,bt[smallest],time+bt[smallest]at[smallest],time-at[smallest]);
sumt+=time+bt[smallest]-at[smallest];
sumw+=time-at[smallest];
time+=bt[smallest];
bt[smallest]=0;
}
5
printf("\n\n average waiting time = %f",sumw*1.0/n);
printf("\n\n average turnaround time = %f",sumt*1.0/n);
return 0;
}
Output:
Conclusion: FCFS provides an efficient, simple and error-free process scheduling
algorithm that saves valuable CPU resources.
6
Problem No
03
Date
08-05-19
Experiment Name: Shortest Job First (SJF) non-preemptive process scheduling
algorithm.
Objectives: To implement SJF where the process that has the shortest burst time is
allocated the CPU first. Without arrival time it act as non-preemptive.
Introduction : In SJF under non-preemptive scheduling, once the CPU has been
allocated to a process, the process keeps the CPU until it releases the CPU.
Algorithm:
Step 1: Declare the array size.
Step 2: Get the number of elements to be inserted.
Step 3: Select the process which have shortest burst will execute first.
Step 4: If two process have same burst length then FCFS scheduling algorithm used.
Step 5: Make the average waiting the length of next process.
Step 6: Start with the first process from it’s selection as above and let other process to be in
queue.
Step 7: Calculate the total number of burst time. Step 8: Display the values.
Program Code:
#include<stdio.h>
int main()
{
int time,bt[10],at[10],sum_bt=0,smallest,n,i;
int sumt=0,sumw=0;
printf("no of processes : ");
scanf("%d",&n);
7
for(i=0;i<n;i++)
{
printf("arrival time for process P%d : ",i+1);
scanf("%d",&at[i]);
printf("burst time for process P%d : ",i+1);
scanf("%d",&bt[i]);
sum_bt+=bt[i];
}
bt[9]=9999;
for(time=0;time<sum_bt;)
{
smallest=9;
for(i=0;i<n;i++)
{
if(at[i]<=time && bt[i]>0 && bt[i]<bt[smallest])
smallest=i;
}
printf("P[%d]\t|\t%d\t|\t%d|\t%d\n",smallest+1,bt[smallest],time+bt[smallest]at[smallest],time-at[smallest]);
sumt+=time+bt[smallest]-at[smallest];
sumw+=time-at[smallest];
time+=bt[smallest];
bt[smallest]=0;
}
8
printf("\n\n average waiting time = %f",sumw*1.0/n);
printf("\n\n average turnaround time = %f",sumt*1.0/n);
return 0;
}
Output:
Conclusion: The SJF process scheduling algorithm gives the minimum average waiting time for a
given set of processes which ensure maximum throughput but to successfully implement, the
burst time should be known in advanced.
9
Problem No
04
Date
08-05-19
Problem Name: Shortest Job First (SJF) preemptive process scheduling algorithm.
Objectives: To implement SJF where the process that has the shortest burst time is allocated
the CPU first. Preemptive SJF will preempt the currently executing process for a shortest
process arrived after it started execution.
Algorithm:
Step 1:
Step 2:
Step 3:
Step 4:
Step 5:
Step 6:
Traverse until all process gets completely executed.
Find process with minimum remaining time at every single time lap.
Reduce its time by 1.
Check if its remaining time becomes 0
Increment the counter of process completion.
Completion time of current process = current_time +1;
Step 7: Calculate waiting time for each complete process
wt[i]= Completion time - arrival_time-burst_time
Step 8: Increment time lap by one.
Step9: Find turnaround time (waiting_time+burst_time).
Program Code:
#include <bits/stdc++.h>
using namespace std;
struct Process {
int pid; // Process ID
int bt; // Burst Time
int art; // Arrival Time
};
10
void findWaitingTime(Process proc[], int n,
int wt[])
{
int rt[n];
for (int i = 0; i < n; i++)
rt[i] = proc[i].bt;
int complete = 0, t = 0, minm = INT_MAX;
int shortest = 0, finish_time;
bool check = false;
while (complete != n) {
for (int j = 0; j < n; j++) {
if ((proc[j].art <= t) &&
(rt[j] < minm) && rt[j] > 0) {
minm = rt[j];
shortest = j;
check = true;
11
}
}
if (check == false) {
t++;
continue;
}
rt[shortest]--;
minm = rt[shortest];
if (minm == 0)
minm = INT_MAX;
if (rt[shortest] == 0) {
complete++;
check = false;
12
finish_time = t + 1;
wt[shortest] = finish_time proc[shortest].bt proc[shortest].art;
if (wt[shortest] < 0)
wt[shortest] = 0;
}
t++;
}
}
void findTurnAroundTime(Process proc[], int n,
int wt[], int tat[])
{
for (int i = 0; i < n; i++)
tat[i] = proc[i].bt + wt[i];
}
13
void findavgTime(Process proc[], int n)
{
int wt[n], tat[n], total_wt = 0,
total_tat = 0;
findWaitingTime(proc, n, wt);
findTurnAroundTime(proc, n, wt, tat);
cout << "Processes "
<< " Burst time "
<< " Waiting time "
<< " Turn around time\n";
for (int i = 0; i < n; i++) {
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << proc[i].pid << "\t\t"
<< proc[i].bt << "\t\t " << wt[i]
<< "\t\t " << tat[i] << endl;
14
}
cout << "\nAverage waiting time = "
<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "
<< (float)total_tat / (float)n;
}
int main()
{
Process proc[] = { { 1, 6, 0 }, { 2, 2, 1 },
{ 3, 1, 2 }, { 4, 4, 3 } };
int n = sizeof(proc) / sizeof(proc[0]);
findavgTime(proc, n);
return 0;
}
Output
Conclusion: The SJF preemptive process scheduling algorithm works successful
15
Problem No
05
Date
15-05-19
Problem Name: Priority based non-preemptive process scheduling algorithm.
Objectives: To implement priority where the process that has the highest priority is allocated
the CPU first. Under non-preemptive priority scheduling, once the CPU has been allocated to a
process, the process keeps the CPU until it releases the CPU.
Introduction: Priority is associated with each process and the CPU is allocated to the process
with the highest priority when the CPU is available. In non-preemptive priority, a process will
finish its execution although a process of higher priority try to allocate the CPU when it already
allocates the CPU.
Algorithm:
STEP 1: Declare the array size.
STEP 2: Get the number of elements to be inserted.
STEP 3: Get the priority for each process and value
STEP 4: Start with the higher priority process from it’s initial position let other process to be
queue.
STEP 5: Calculate the total number of burst time.
STEP 6: Display the values
Program Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int et[20],at[10],n,i,j,temp,p[10],st[10],ft[10],wt[10],ta[10];
16
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter process name,arrivaltime,execution time & priority:");
scanf("%s%d%d%d",pn[i],&at[i],&et[i],&p[i]);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(p[i]<p[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
17
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0;i<n;i++)
{
if(i==0)
{
st[i]=at[i];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
}
else
{
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
}
totwt+=wt[i];
18
totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\nPname\tarrivaltime\texecutiontime\tpriority\twaitingtime\ttatime");
for(i=0;i<n;i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],p[i],wt[i],ta[i]);
printf("\nAverage waiting time is:%f",awt);
printf("\nAverage turnaroundtime is:%f",ata);
return 0;
}
Output:
Conclusion: The non-preemptive priority process algorithm implement and works successfully.
19
Problem No
06
Date
15-05-19
Problem Name: Priority based preemptive process scheduling algorithm.
Objectives: To implement priority where the process that has the highest priority is allocated
the CPU first. Preemptive priority will preempt the currently executing process for a higher
priority process which arrived after it started execution.
Introduction: Priority is associated with each process and the CPU is allocated to the process
with the highest priority. In preemptive priority, preemptive priority will preempt the currently
executing process for a higher priority process which arrived after it started execution.
Algorithm:
Step 1: First input the processes with their arrival time, burst time and priority.
Step 2: Sort the processes, according to arrival time if two process arrival time is same
then sort according process priority if two process priority are same then sort according
to process number.
Step 3: Now simply apply FCFS algorithm.
a)Create the number of process.
b) Get the ID and Service time for each process.
c) Initially, Waiting time of first process is zero and Total time for the first process is the
starting time of that process.
d) Calculate the Total time and Processing time for the remaining processes.
e) Waiting time of one process is the Total time of the previous process.
f) Total time of process is calculated by adding Waiting time and Service time.
g) Total waiting time is calculated by adding the waiting time for lack process.
h) Total turn around time is calculated by adding all total time of each process.
i) Calculate Average waiting time by dividing the total waiting time by total number of
process.
j):Calculate Average turn around time by dividing the total time by the number of
process.
Step 4: Display the result.
20
Program Code:
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
typedef struct proccess
{
int at,bt,ct,ta,wt,btt,pr;
string pro_id;
}schedule;
bool compare(schedule a,schedule b)
{
return a.at<b.at;
}
bool compare2(schedule a,schedule b)
{
return a.pr>b.pr;
}
int main()
{
schedule pro[10];
int n,i,j,pcom;
cout<<"Enter the number of process::";
21
cin>>n;
cout<<"Enter the Process id arrival time burst time and priority :::(large number is
highest priority) :";
for(i=0;i<n;i++)
{
cin>>pro[i].pro_id;
cin>>pro[i].at;
cin>>pro[i].bt;
pro[i].btt=pro[i].bt;
cin>>pro[i].pr;
}
sort(pro,pro+n,compare);
i=0;
pcom=0;
while(pcom<n)
{
for(j=0;j<n;j++)
{
if(pro[j].at>i)
break;
}
sort(pro,pro+j,compare2);
if(j>0)
{
for(j=0;j<n;j++)
{
if(pro[j].bt!=0)
break;
}
if(pro[j].at>i)
i+=pro[j].at-i;
pro[j].ct=i+1;
pro[j].bt--;
}
i++;
22
pcom=0;
for(j=0;j<n;j++)
{
if(pro[j].bt==0)
pcom++;
}
}
for(i=0;i<n;i++)
{
pro[i].ta=pro[i].ct-pro[i].at;
pro[i].wt=pro[i].ta-pro[i].btt;
cout<<pro[i].pro_id<<"\t"<<pro[i].at<<"\t"<<pro[i].btt<<"\t"<<pro[i].ct<<"\t"<<pro[i].ta
<<"\t"<<pro[i].wt<<"\t"<<pro[i].pr;
cout<<endl;
}
return 0;
}
Output:
Conclusion: The preemptive priority process scheduling algorithm works and implement
successfully.
23
Problem No
07
Date
22-05-19
Problem Name: Round Robin (RR) process scheduling algorithm without arrival time.
Objectives: To implement Round Robin (RR) process scheduling algorithm where every
process gets a chance to access the CPU for a certain time quantum.
Introduction: Round Robin (RR) process scheduling algorithm treat with the ready
queue as a FIFO queue as processes.
Algorithm:
STEP 1: Declare the array size.
STEP 2: Get the number of elements to be inserted.
STEP 3: Get the value.
STEP 4: Set the time sharing system with preemption.
STEP 5: Define quantum is defined from 10 to 100ms.
STEP 6: Declare the queue as a circular. STEP
7: Make the CPU scheduler goes around the ready queue allocating CPU to each
process for the time interval specified.
STEP 8: Make the CPU scheduler picks the first process and sets time to interrupt after
quantum expired dispatches the process.
STEP 9: If the process has burst less than the time quantum than the process releases
the CPU
Program Code:
#include<iostream>
using namespace std;
void findWaitingTime(int processes[], int n,
int bt[], int wt[], int quantum)
{
int rem_bt[n];
for (int i = 0 ; i < n ; i++)
rem_bt[i] = bt[i];
int t = 0;
while (1)
{
24
bool done = true;
for (int i = 0 ; i < n; i++)
{
if (rem_bt[i] > 0)
{
done = false;
if (rem_bt[i] > quantum)
{
t += quantum;
rem_bt[i] -= quantum;
}
else
{
t = t + rem_bt[i];
wt[i] = t - bt[i];
rem_bt[i] = 0;
}
}
}
if (done == true)
break;
}
}
void findTurnAroundTime(int processes[], int n,
int bt[], int wt[], int tat[])
{
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}
void findavgTime(int processes[], int n, int bt[],
int quantum)
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;
findWaitingTime(processes, n, bt, wt, quantum);
findTurnAroundTime(processes, n, bt, wt, tat);
cout << "Processes "<< " Burst time "
<< " Waiting time " << " Turn around time\n";
25
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << i+1 << "\t\t" << bt[i] <<"\t "
<< wt[i] <<"\t\t " << tat[i] <<endl;
}
cout << "Average waiting time = "
<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "
<< (float)total_tat / (float)n;
}
int main()
{
int processes[] = { 1, 2, 3,4};
int n = sizeof processes / sizeof processes[0];
int burst_time[] = {5,3,8,6};
int quantum = 3;
findavgTime(processes, n, burst_time, quantum);
return 0;
}
Output:
Conclusion: Round Robin (RR) process scheduling algorithm without arrival time works
and implement successfully.
26
Problem No
08
Date
22-05-19
Problem Name: Round Robin (RR) process scheduling algorithm with arrival time.
Objectives: To implement Round Robin (RR) process scheduling algorithm where every
process gets a chance to access the CPU for a certain time quantum.
Introduction: Round Robin (RR) process scheduling algorithm treat with the ready
queue as a FIFO queue as processes. Processes are added to the tail of the ready
queue.
Algorithm:
STEP 1: Declare the array size.
STEP 2: Get the number of elements to be inserted.
STEP 3: Get the value.
STEP 4: Set the time sharing system with preemption.
STEP 5: Define quantum is defined from 10 to 100ms.
STEP 6: Declare the queue as a circular.
STEP 7: Make the CPU scheduler goes around the ready queue allocating CPU to each
process for the time interval specified.
STEP 8: Make the CPU scheduler picks the first process and sets time to interrupt after
quantum expired dispatches the process.
STEP 9: If the process has burst less than the time quantum than the process releases
the CPU
Program Code:
#include<stdio.h>
int main()
{
int count,j,n,time,remain,flag=0,time_quantum;
int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
printf("Enter Total Process:\t ");
scanf("%d",&n);
remain=n;
for(count=0;count<n;count++)
{
printf("Enter Arrival Time and Burst Time for Process Process Number %d
:",count+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
27
rt[count]=bt[count];
}
printf("Enter Time Quantum:\t");
scanf("%d",&time_quantum);
printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
for(time=0,count=0;remain!=0;)
{
if(rt[count]<=time_quantum && rt[count]>0)
{
time+=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)
{
rt[count]-=time_quantum;
time+=time_quantum;
}
if(rt[count]==0 && flag==1)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count]);
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
}
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;
}
printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);
printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);
return 0;
}
28
Output:
Conclusion: Round Robin (RR) is a better algorithm which gives all the processes to get
an access but the average waiting time under the RR policy is often long for the context
switchs.
29
Problem No
09
Date
29-05-19
Problem Name: To write a program of Banker’s algorithm.
Objectives: To implement Banker’s algorithm we need to follow safety algorithm,
check every process and gives a safe sequence.
Introduction: Banker’s algorithm is very important for safe sequence. It checks the
state safe or unsafe.
Algorithm:
Step 1: Start the Program
Step 2: Get the values of resources and processes.
Step 3: Get the avail value..
Step 4: After allocation find the need value.
Step 5: Check whether its possible to allocate.
Step 6: If it is possible then the system is in safe state. Step 7: Stop the execution
Program Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int process,resource,i,j,instanc,k=0,count1=0,count2=0; //count,k
taken for counting purpose
printf("\n\t Enter No. of Process:-\n");
printf("\t\t");
scanf("%d",&process);
//Entering No. of Processes
printf("\n\tEnter No. of Resources:-\n");
printf("\t\t");
scanf("%d",&resource);
//No. of Resources
variables are
int
avail[resource],max[process][resource],allot[process][resource],need[process][resourc
e],completed[process];
for(i=0;i<process;i++)
completed[i]=0;
//Setting Flag for uncompleted Process
printf("\n\tEnter No. of Available Instances\n");
30
for(i=0;i<resource;i++)
{
printf("\t\t");
scanf("%d",&instanc);
avail[i]=instanc;
}
// Storing Available instances
printf("\n\tEnter Maximum No. of instances of resources that a Process need:\n");
for(i=0;i<process;i++)
{
printf("\n\t For P[%d]",i);
for(j=0;j<resource;j++)
{
printf("\t");
scanf("%d",&instanc);
max[i][j]=instanc;
}
}
printf("\n\t Enter no. of instances already allocated to process of a resource:\n");
for(i=0;i<process;i++)
{
printf("\n\t For P[%d]\t",i);
for(j=0;j<resource;j++)
{
printf("\t\t");
scanf("%d",&instanc);
allot[i][j]=instanc;
need[i][j]=max[i][j]-allot[i][j];
}
}
printf("\n\t Safe Sequence is:- \t");
//calculating Need of each process
while(count1!=process)
{
count2=count1;
for(i=0;i<process;i++)
{
for(j=0;j<resource;j++)
{
if(need[i][j]<=avail[j])
{
k++;
31
}
}
if(k==resource && completed[i]==0 )
{
printf("P[%d]\t",i);
completed[i]=1;
for(j=0;j<resource;j++)
{
avail[j]=avail[j]+allot[i][j];
}
count1++;
}
k=0;
}
if(count1==count2)
{
printf("\t\t Stop ..After this.....Deadlock \n");
break;
}
}
getch();
}
32
Output:
Conclusion: Banker’s algorithm works successfully and gives a safe sequence correctly.
33
Problem No
10
Date
29-05-19
Problem Name : To write a program of FIFO page replacement.
Objectives: The FIFO page replacement algorithm is easy to understand and program.
It could contain a heavily used variable that was initialized early and is in constant use.
Introduction: FIFO page replacement algorithm associates with each page the time
when that page was brought into memory. When a page must be replaced, the oldest
page is chosen. We can create a FIFO queue to hold all pages in memory. We replace
the page at the head of the queue. When a page is brought into memory, we insert it
at the tail of the queue.
Algorithm:
Step 1: Start the Program
Step 2: Read number of pages n
Step 3: Read number of pages no
Step 4: Read page numbers into an arraya[i]
Step 5: Initialize aval[i]=o, to check page hit
Step 6: Print the results.
Step 7: Stop the Process.
Program Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],b[20],n,p=0,q=0,m=0,h,k,i,q1=1;
char f='F';
printf("Enter the Number of Pages:");
scanf("%d",&n);
printf("Enter %d Page Numbers:",n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
{
34
if(p==0)
{
if(q>=3)
q=0;
a[q]=b[i];
q++;
if(q1<3)
{
q1=q;
}
}
printf("\n%d",b[i]);
printf("\t");
for(h=0;h<q1;h++)
printf("%d",a[h]);
if((p==0)&&(q<=3))
{
printf("-->%c",f);
m++;
}
p=0;
for(k=0;k<q1;k++)
{
if(b[i+1]==a[k])
p=1;
}
}
printf("\nNo of faults:%d",m);
}
35
Output
Conclusion: FIFO page replacement algorithm is successfully run and work correctly .It
gives the number of page fault .
36
Download