DEAR FRIENDS!PLEASE CONTACT ME FOR THE PROBLEMS OF MY POSTS AND GIVE ME YOUR OPINION,YOUR OPINIONS WHOULD BE APPRICIATED.MY CONTACT NO. IS 00923056395196

Sunday, 19 May 2013

THEORY OF ENCODERS/DECODERS IN DLD

Theory

  

Decoder


In digital electronics, a decoder can take the form of a multiple-input, multiple-output logic circuit that converts coded inputs into coded outputs, where the input and output codes are different e.g. n-to-2n , binary-coded decimal decoders. Decoding is necessary in applications such as data multiplexing, 7 segment display and memory address decoding.

The example decoder circuit would be an AND gate because the output of an AND gate is "High" (1) only when all its inputs are "High." Such output is called as "active High output". If instead of AND gate, the NAND gate is connected the output will be "Low" (0) only when all its inputs are "High". Such output is called as "active low output".

A slightly more complex decoder would be the n-to-2n type binary decoders. These types of decoders are combinational circuits that convert binary information from 'n' coded inputs to a maximum of 2n unique outputs. In case the 'n' bit coded information has unused bit combinations, the decoder may have less than 2n outputs. 2-to-4 decoder, 3-to-8 decoder or 4-to-16 decoder are other examples.

The input to a decoder is parallel binary number and it is used to detect the presence of a particular binary number at the input. The output indicates presence or absence of specific number at the decoder input.

Let us suppose that a logic network has 2 inputs A and B. They will give rise to 4 states A, A’, B, B’ . The truth table for this decoder is shown below:

Table 1: Truth Table of 2:4 decoder


             

Fig 1: Logic Diagram of 2:4 decoder


Fig 2: Representation of 2:4 decoder


For any input combination only one of the outputs is low and all others are high. The low value at the output represents the state of the input.

Decoder expansion

Combine two or more small decoders with enable inputs to form a larger decoder e.g. 3-to-8-line decoder constructed from two 2-to-4-line decoders.
Decoder with enable input can function as demultiplexer.

3:8 decoder

It uses all AND gates, and therefore, the outputs are active- high. For active- low outputs, NAND gates are used. It has 3 input lines and 8 output lines. It is also called as binary to octal decoder it takes a 3-bit binary input code and activates one of the 8(octal) outputs corresponding to that code. The truth table is as follows:

 
Table 2: Truth Table of 3:8 decoder


Fig 3: Logic Diagram of 3:8 decoder


Encoder

An encoder is a device, circuit, transducer, software program, algorithm or person that converts information from one format or code to another. The purpose of encoder is standardization, speed, secrecy, security, or saving space by shrinking size. Encoders are combinational logic circuits and they are exactly opposite of decoders. They accept one or more inputs and generate a multibit output code.

Encoders perform exactly reverse operation than decoder. An encoder has M input and N output lines. Out of M input lines only one is activated at a time and produces equivalent code on output N lines. If a device output code has fewer bits than the input code has, the device is usually called an encoder.


Octal to binary encoder

Octal-to-Binary take 8 inputs and provides 3 outputs, thus doing the opposite of what the 3-to-8 decoder does. At any one time, only one input line has a value of 1. The figure below shows the truth table of an Octal-to-binary encoder.


 
Table 3: Truth Table of octal to binary encoder

For an 8-to-3 binary encoder with inputs I0-I7 the logic expressions of the outputs Y0-Y2 are:
Y0 = I1 + I3 + I5 + I7
Y1= I2 + I3 + I6 + I7
Y2 = I4 + I5 + I6 +I7

 
Fig 4: Logic Diagram of octal to binary encoder


Priority encoder

A priority encoder is a circuit or algorithm that compresses multiple binary inputs into a smaller number of outputs. The output of a priority encoder is the binary representation of the ordinal number starting from zero of the most significant input bit. They are often used to control interrupt requests by acting on the highest priority request. It includes priority function. If 2 or more inputs are equal to 1 at the same time, the input having the highest priority will take precedence. Internal hardware will check this condition and priority is set.


 
Table 4: Truth Table of 4 bit priority encoder/p>

 
Fig 5: Logic Diagram of 4 bit priority encoder

IC 74148 is an 8-input priority encoder. 74147 is 10:4 priority encoder

Multiplexer

In electronics, a multiplexer or mux is a device that selects one of several analog or digital input signals and forwards the selected input into a single line. A multiplexer of 2n inputs has n select lines, which are used to select which input line to send to the output. An electronic multiplexer can be considered as a multiple-input, single-output switch i.e. digitally controlled multi-position switch. The digital code applied at the select inputs determines which data inputs will be switched to output.

A common example of multiplexing or sharing occurs when several peripheral devices share a single transmission line or bus to communicate with computer. Each device in succession is allocated a brief time to send and receive data. At any given time, one and only one device is using the line. This is an example of time multiplexing since each device is given a specific time interval to use the line.

In frequency multiplexing, several devices share a common line by transmitting at different frequencies.

 
Table 5: Truth Table of 8:1 MUX

 
Fig 6: Logic Diagram of 8:1 MUX


Demultiplexer

A demultiplexer (or demux) is a device taking a single input signal and selecting one of many data-output-lines, which is connected to the single input. A multiplexer is often used with a complementary demultiplexer on the receiving end. A demultiplexer is a single-input, multiple-output switch. Demultiplexers take one data input and a number of selection inputs, and they have several outputs. They forward the data input to one of the outputs depending on the values of the selection inputs.

Demultiplexers are sometimes convenient for designing general purpose logic, because if the demultiplexer's input is always true, the demultiplexer acts as a decoder. This means that any function of the selection bits can be constructed by logically OR-ing the correct set of outputs. Demultiplexer is called as a ‘distributro’, since it transmits the same data to different destinations.


 
Table 6: Truth Table of 1:8 DEMUX
 
Fig 7: Logic Diagram of 1:8 DEMUX

ENCODERS/DECODERS

ENCODERS/DECODERS


Hybrid Electronics Laboratory
Experiment No.  5

Design and Simulation of Decoders, Encoders, Multiplexer and Demultiplexer

Aim: To study decoders, encoders, multiplexer and demultiplexer .

Objectives:
  1. Implement and simulate decoder and encoder
  2. Implement and Simulate multiplexer and demultiplexer
Theory:
Decoder
   In digital electronics, a decoder can take the form of a multiple-input, multiple-output logic circuit that converts coded inputs into coded outputs, where the input and output codes are different e.g. n-to-2n , binary-coded decimal decoders. Decoding is necessary in applications such as data multiplexing, 7 segment display and memory address decoding.
   The example decoder circuit would be an AND gate because the output of an AND gate is "High" (1) only when all its inputs are "High." Such output is called as "active High output". If instead of AND gate, the NAND gate is connected the output will be "Low" (0) only when all its inputs are "High". Such output is called as "active low output".
   A slightly more complex decoder would be the n-to-2n type binary decoders. These types of decoders are combinational circuits that convert binary information from 'n' coded inputs to a maximum of 2n unique outputs. In case the 'n' bit coded information has unused bit combinations, the decoder may have less than 2n outputs. 2-to-4 decoder, 3-to-8 decoder or 4-to-16 decoder are other examples.
  The input to a decoder is parallel binary number and it is used to detect the presence of a particular binary number at the input. The output indicates presence or absence of specific number at the decoder input.
    Let us suppose that a logic network has 2 inputs A and B. They will give rise to 4 states A, A’, B, B’ . The truth table for this decoder is shown below:

Table 1: Truth Table of 2:4 decoder
Fig 1: Logic Diagram of 2:4 decoder
Fig 2: Representation of 2:4 decoder
    For any input combination only one of the outputs is low and all others are high. The low value at the output represents the state of the input.
 Decoder expansion
    Combine two or more small decoders with enable inputs to form a larger decoder e.g. 3-to-8-line decoder constructed from two 2-to-4-line decoders.
    Decoder with enable input can function as demultiplexer.
 3:8 decoder
    It uses all AND gates, and therefore, the outputs are active- high. For active- low outputs, NAND gates are used. It has 3 input lines and 8 output lines. It is also called as binary to octal decoder it takes a 3-bit binary input code and activates one of the 8(octal) outputs corresponding to that code. The truth table is as follows:
Table 2: Truth Table of 3:8 decoder
Fig 3: Logic Diagram of 3:8 decoder
  
Encoder
    An encoder is a device, circuit, transducer, software program, algorithm or person that converts information from one format or code to another. The purpose of encoder is standardization, speed, secrecy, security, or saving space by shrinking size. Encoders are combinational logic circuits and they are exactly opposite of decoders. They accept one or more inputs and generate a multibit output code.
    Encoders perform exactly reverse operation than decoder. An encoder has M input and N output lines. Out of M input lines only one is activated at a time and produces equivalent code on output N lines. If a device output code has fewer bits than the input code has, the device is usually called an encoder.
 Octal to binary encoder
    Octal-to-Binary take 8 inputs and provides 3 outputs, thus doing the opposite of what the 3-to-8 decoder does. At any one time, only one input line has a value of 1. The figure below shows the truth table of an Octal-to-binary encoder.
Table 3: Truth Table of octal to binary encoder

For an 8-to-3 binary encoder with inputs I0-I7 the logic expressions of the outputs Y0-Y2 are:
Y0 = I1 + I3 + I5 + I7
Y1= I2 + I3 + I6 + I7
Y2 = I4 + I5 + I6 +I7
Fig 4: Logic Diagram of octal to binary encoder
Priority encoder
     A priority encoder is a circuit or algorithm that compresses multiple binary inputs into a smaller number of outputs. The output of a priority encoder is the binary representation of the ordinal number starting from zero of the most significant input bit. They are often used to control interrupt requests by acting on the highest priority request. It includes priority function. If 2 or more inputs are equal to 1 at the same time, the input having the highest priority will take precedence. Internal hardware will check this condition and priority is set.
  
 
Table 4: Truth Table of 4 bit priority encoder

 
Fig 5: Logic Diagram of 4 bit priority encoder
 IC 74148 is an 8-input priority encoder. 74147 is 10:4 priority encoder
 Multiplexer
    In electronics, a multiplexer or mux is a device that selects one of several analog or digital input signals and forwards the selected input into a single line. A multiplexer of 2n inputs has n select lines, which are used to select which input line to send to the output. An electronic multiplexer can be considered as a multiple-input, single-output switch i.e. digitally controlled multi-position switch. The digital code applied at the select inputs determines which data inputs will be switched to output.
    A common example of multiplexing or sharing occurs when several peripheral devices share a single transmission line or bus to communicate with computer. Each device in succession is allocated a brief time to send and receive data. At any given time, one and only one device is using the line. This is an example of time multiplexing since each device is given a specific time interval to use the line.
    In frequency multiplexing, several devices share a common line by transmitting at different frequencies.

 
Table 5: Truth Table of 8:1 MUX

 
Fig 6: Logic Diagram of 8:1 MUX
  Demultiplexer
     A demultiplexer (or demux) is a device taking a single input signal and selecting one of many data-output-lines, which is connected to the single input. A multiplexer is often used with a complementary demultiplexer on the receiving end. A demultiplexer is a single-input, multiple-output switch. Demultiplexers take one data input and a number of selection inputs, and they have several outputs. They forward the data input to one of the outputs depending on the values of the selection inputs.
     Demultiplexers are sometimes convenient for designing general purpose logic, because if the demultiplexer's input is always true, the demultiplexer acts as a decoder. This means that any function of the selection bits can be constructed by logically OR-ing the correct set of outputs. Demultiplexer is called as a ‘distributro’, since it transmits the same data to different destinations.
 
Table 6: Truth Table of 1:8 DEMUX
 
Fig 7: Logic Diagram of 1:8 DEMUX

Procedure:
1. Select appropriate combinational logic circuit from the tab menu.
2. Select run button in the top to execute the operation.
3. Observe the output on the output LEDs and observe digital waveforms on digital display.
4. Repeat the procedure and observe the corresponding outputs of encoder, decoder, multiplexer and demultiplexer.

Screen Shots:
 



 
Results:
     Combinational logic circuits decoder , encoder are designed and simulated using logic gates.

BASICS OF ENGINEERING ECONOMY

Computer System Architecture (Morris Mano)


Computer System Architecture (Morris Mano)




DOWNLOAD LINK:

 DOWNLOAD

Programming with Java -- E Balagurusamy (3rd edition)


Programming with Java -- E Balagurusamy (3rd edition)

JAVA The Complete Reference 7th Edition


JAVA The Complete Reference 7th Edition


Computer Networks – Andrew S Tanenbaum, 4 th Edition


Computer Networks – Andrew S Tanenbaum, 4 th Edition


The Complete Reference JAVA 2, Herbert Schield( 5th Edition)


The Complete Reference JAVA 2, Herbert Schield( 5th Edition)


I.C. Engines -- V. Ganesan


I.C. Engines -- V. Ganesan


APPLIED THERMODYNAMICS – II



Fluid Mechanics and Hydraulic Machines -- R.K.Bansal


Fluid Mechanics and Hydraulic Machines -- R.K.Bansal

 


Thermal Engineering by R K Rajput



Front Cover



Thermal Engineering by R K Rajput



Eight Edition

Principles of Programming Language by Robert W Sebesta


Principles of Programming Language by Robert W Sebesta

5th Edition




Download from HERE

Microprocessors and Interfacing by Godse


Microprocessors and Interfacing by Godse

Operating Systems Slides- Galvin


Operating Systems Slides- Galvin:




Download from HERE

Data Structures with Java


Data Structures with Java:

Download from HERE

Saturday, 18 May 2013

OS Lab Programs


OS Lab Programs:

#1]Program to display "welcome to unix world" n times

#include<stdio.h>
int main()
{
int i,n;
printf("Enter number");
scanf("%d",&n);
for(i=0;i<n;i++)
printf("Welcome to UNIX WORLD\n");
}


#2]program to display process id,parent id and group id

#include<stdio.h>
#include<unistd.h>
int main()
{
printf("process id %d\n",getpid());
printf("Parent id %d\n",getppid());
printf("group id %d\n",getgid());
}



#3]program to print limit of system configuration using  built in

#include<stdio.h>
#include<unistd.h>
#include<sys/sysinfo.h>
int main()
{
long ret;
if((ret=sysconf(_SC_ARG_MAX))!=-1)
printf("argmax=%d",ret);
else
perror("args");
if((ret=sysconf(_SC_CHILD_MAX))!=-1)
printf("\nchildmax=%d",ret);
else
perror("child");
if((ret=sysconf(_SC_CLK_TCK))!=-1)
printf("\nclickmax=%d",ret);
else
perror("clicks");
if((ret=sysconf(_SC_NPROCESSORS_CONF))!=-1)
printf("\nnprocessors=%d",ret);
else
perror("\nnprocessors");
}


#4]program to create a child process using fork in-built function

#include<stdio.h>
int main()
{
int pid;
printf("process id %d\n",getpid());
pid=fork();
if(pid<0)
printf("fork failed child not created\n");
else if(pid==0)
printf("child created %d\n",getpid());
return 0;
}


#5]program to dispaly "UNIX PROGRAMMING LAB" n times using system calls and user defined function display(char*). n is value read from keyboard.


#include<stdio.h>
#include<string.h>
void display(char *);
int main()
{
system("clear");
display("UNIX PROGRAMMING LAB\n");
}
void display(char *s)
{
int l,n;
char ch[10],mes[]="Enter n value:";
write(1,mes,strlen(mes));
read(0,ch,10);
n=atoi(ch);
while(n--,n>-1)
write(1,s,strlen(s));
}


#6]Program to implement First Come First Serve Algorithm

#include<stdio.h>
#include<unistd.h>
int main()
{
int bursttime[4];
int waitingtime[4];
int n,i,j;
printf("enter number of processes:");
scanf("%d",&n);
printf("enter burst times of %d processes:",n);
for(i=0;i<n;i++)
{
printf("\nEnter burst time of process %d:",i+1);
scanf("%d",&bursttime[i]);
}
printf("\ncalculating the waiting time....");
for(i=0;i<n;i++)
{
waitingtime[i]=0;
for(j=i-1;j>=0;j--)
{
waitingtime[i]=waitingtime[i]+bursttime[j];
}
}
printf("\n\n\tPROCESS\tBURST TIME \tWAITING TIME");
for(i=0;i<n;i++)
{
printf("\n\t%d\t%d\t\t%d",i+1,bursttime[i],waitingtime[i]);
}
printf("\n");
}


#7]program to implement shortest job first(non- preemption)


#include<stdio.h>
struct process
{
int pno,bt,wt,ft,tat;
}a[10],temp;
main()
{
int i,n,j;
float twt=0.0,total=0.0;
float avgwt,avgtat;
printf("\nenter no of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter pno,bt:");
scanf("%d%d",&a[i].pno,&a[i].bt);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i].bt>a[j].bt)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
a[0].ft=a[0].bt;
a[0].wt=0;
a[0].tat=a[0].wt+a[0].bt;
a[i].wt=a[i-1].ft;
a[i].tat=a[i].wt+a[i].bt;
}
printf("pno\tbt\tft\twt\ttat\n");
for(i=0;i<n;i++)
{
printf("\n%d\t%d\t%d\t%d\t%d",a[i].pno,a[i].bt,a[i].ft,a[i].wt,a[i].tat);
}
for(i=0;i<n;i++)
{
twt=twt+a[i].wt;
total=total=total+a[i].tat;
avgwt=twt/n;
avgtat=total/n;
}
printf("avg waiting time is %f\n",avgwt);
printf("avg turnaround time is %f",avgtat);
}



#8]Program to print n times "UNIX PROGRAMMING LAB" reading from "infile.txt" and print the message in "outfile.txt" using a user defined function writefile(n)


#include<stdio.h>
char st[]="Unix programming lab";
main()
{
FILE *fp;
int n;
fp=fopen("infile.txt","w");
printf("\nhow many times do you want to dispaly");
scanf("%d",&n);
fprintf(fp,"%d",n);
writefile(n);
fclose(fp);
}
writefile(int n)
{
int i=0;
FILE *fp;
fp=fopen("outfile.txt","w");
while(i<n)
{
fprintf(fp,"%s\n",st);
i++;
}
fclose(fp);
}


#9]Program to implement Shortest job first (preemption)

#include<stdio.h>
#include<stdlib.h>
 main()
{
int a[10],b[10],w[10],at[10],n,s=0,i,j,k,t,t1,t2;
float avg=0,tt=0;
printf("enter no. of processes\n");
scanf("%d",&n);
printf("enter burst times of proceses\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]<0)
{
printf("invalid input");
exit(0);
}
b[i]=0;
tt=tt+a[i];
}
printf("enter arrival times\n");
for(i=0;i<n;i++)
scanf("%d",&at[i]);
k=0;
j=1;
for(i=0;i<n-1;i++)
{
if(a[i]!=0)
{
while(at[i+1]!=s)
{
k=i;
for(j=0;j<i;j++)
{
if(a[i]>a[j])
k=j;
}
a[k]=a[k]-1;
b[k]++;
s=s+1;
}
}
}
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
t1=at[i];
at[i]=at[j];
at[j]=t1;
t2=b[i];
b[i]=b[j];
b[j]=t2;
}
}
}
for(i=0;i<n;i++)
{
w[i]=s-b[i];
s=s+a[i];
}
for(i=0;i<n;i++)
{
w[i]=w[i]-at[i];
avg=avg+w[i];
}
tt=tt+avg;
tt=tt/n;
avg=avg/n;
printf("turn around time is %f",tt);
printf("Avg waiting time is %f",avg);
}



#10]Program to implement Bankers Algorithm


#include<stdio.h>
#include<stdlib.h>
struct process
{
int pid,max[8],alloc[8],need[8],finish;
}p[8];
main()
{
int total[8],avail[8],count=0,i,j,k=0,n,r;
printf("\n Enter no. of process:");
scanf("%d",&n);
printf("\n Enter the no. of resources");
scanf("%d",&r);
for(j=0;j<r;j++)
{
printf("Enter the total resources of type %d",j);
scanf("%d",&total[j]);
avail[j]=total[j];
}
for(i=0;i<n;i++)
{
printf("Enter the process ID: \n");
scanf("%d",&p[i].pid);
p[i].finish=0;
for(j=0;j<r;j++)
{
printf("Enter maximum resources requried of type %d:",j);
scanf("%d",&p[i].max[j]);
}
for(j=0;j<r;j++)
{
printf("Enter the resoursces allocated of type %d",j);
scanf("%d",&p[i].alloc[j]);
}
}
printf("The available resources sre:\n");
for(j=0;j<r;j++)
printf("%3d \n",avail[j]);
printf("The need matrix i.e:\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
p[i].need[j]=p[i].max[j]-p[i].alloc[j];
}
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
printf("%3d",p[i].need[j]);
printf("\n");
}
for(i=0,k=0;k<n;i++)
{
if(i==n)
i=0;
if(p[i].finish==0)
{
for(j=0;j<r && p[i].need[j]<=avail[j];j++)
if(j==r)
{
for(j=0;j<r;j++)
{
avail[j]+=p[i].alloc[i];
}
count++;
printf("\n process %d is finished-avail",p[i].pid);
for(j=0;j<r;j++)
printf("%3d",avail[j]);
if(count==n)
{
printf("\n the system is in safe state");
exit(0);
}
k=0;
}
else
k++;
}
else
k++;
}
printf("the system is in unsafe state \n");
}



#11] FCFS with arrival time


#include<stdio.h>
struct process
{
int no,at,bt,ft,wt,tt;
}a[10],temp;
main()
{
int n,i,j,twt=0,tat=0;
float awt,atat;
printf("Enter no. of processes");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter process no");
scanf("%d",&a[i].no);
printf("Enter arrival time and brust time");
scanf("%d%d",&a[i].at,&a[i].bt);
}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(a[i].at>a[j].at)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
a[1].ft=a[1].at+a[1].bt;
a[1].wt=0;
a[1].tt=a[1].wt+a[1].bt;
for(i=2;i<=n;i++)
{
if(a[i-1].ft>a[i].at)
{
a[i].ft=a[i-1].ft+a[i].bt;
a[i].wt=a[i-1].ft-a[i].at;
a[i].tt=a[i].wt+a[i].bt;
}
else
{
a[i].ft=a[i].at+a[i].bt;
a[i].wt=0;
a[i].tt=a[i].wt+a[i].bt;
}
}
printf("\n no\tat\tbt\tft\twt\ttt\n");
for(i=1;i<=n;i++)
{
twt=twt+a[i].wt;
tat=tat+a[i].tt;
printf("\n%d\t%d\t%d\t%d\t%d\t%d",a[i].no,a[i].at,a[i].bt,a[i].ft,a[i].wt,a[i].tt);
}
awt=(float)twt/n;
atat=(float)tat/n;
printf("average wating time is %f",awt);
printf("\n average turn around time is %f",atat);
}


FIFO replacement

                                             
#include<stdio.h>
 main()
{
int n,i,j,k,l,np,nf,pf,pos,flag=0;
int p[20],f[20];
printf("\nEnter the number of free frames:");
scanf("%d",&nf);
printf("\nEnter the referece string length:");
scanf("%d",&np);
printf("\nEnter the reference string:");
for(i=0;i<np;i++)
scanf("%d",&p[i]);
for(i=0;(i<nf)&&(i<np);i++)
{
f[i]=-1;
}
pos=0;
pf=0;
for(j=0;j<np;j++)
{
flag=0;
for(k=0;k<np;k++)
if(f[k]==p[j])
{
flag=1;
break;
}
if(flag!=1)
{
pf++;
printf("%d %d\n",p[j],pf);
f[pos]=p[j];
pos++;
if(pos==nf)
pos=0;
}
}
printf("\nNumber of page faults is %d",pf);
}
 
LEAST RECENTLY USED algorithm
 
#include<stdio.h>
int main()
{
int i,m,n,k,a[10],b[10],pf,flag=0,pos=0,l,j;
printf("\nEnter the number of free frames:");
scanf("%d",&m);
printf("\nEnter the reference string length:");\
scanf("%d",&n);
printf("\nEnter the refernce string:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
pf=0;
for(i=0;(i<m)&&(i<n);i++)
{
b[i]=-1;
}
for(k=0;k<n;k++)
{
flag=0;
for(j=0;j<m;j++)
{
if(a[k]==b[j])
{
flag=1;pos=j;
break;
}
}
if(flag==1)
{
for(l=pos;l<m-1;l++)
b[l]=b[l+1];
b[m-1]=a[k];
}
else
{
for(l=0;l<m-1;l++)
b[l]=b[l+1];
b[m-1]=a[k];
pf++;
printf("%d %d\n",a[k],pf);
}
}
printf("\nNumber of page faults is %d\n",pf);
}
 
 
OPTIMAL page replacement
 
 
                                             
#include<stdio.h>
main()
{
int i,j,m,n,a[20],b[20],flag,c[10];
int pos=0,max,l,p,k,pf;
printf("\nEnter the number free frames:");
scanf("%d",&m);
printf("\nEnter thr reference string length:");
scanf("%d",&n);
printf("\nEnter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
pf=0;
for(i=0;i<m;i++)
b[i]=-1;
for(i=0;i<m;i++)
c[i]=0;
for(k=0;k<n;k++)
{
flag=0;
for(j=0;j<m;j++)
{
if(a[k]==b[j])
{
flag=1;
break;
}
}
if(flag!=1)
{
for(p=0;p<m;p++)
{
for(l=k+1;l<n;l++)
{
if(a[l]==b[p])
break;
else
c[p]=c[p]+1;
}
}
max=c[0];
pos=0;
for(i=1;i<m;i++)
{
if(max<c[i])
{
max=c[i];
pos=i;
}
}
i=pos;
b[i]=a[k];
pf=pf+1;
for(i=0;i<m;i++)
c[i]=0;
}
}
printf("\nNumber of page faults:%d\n",pf);
} 
Related Posts Plugin for WordPress, Blogger...