Sunday, 27 October 2013

Least Recently Used(LRU) Page Replacement algorithm

Least Recently Used(LRU) Page Replacement algorithm:

Program Description: 

The Least Recently Used replacement policy chooses to replace the page which has not been referenced for the longest time.

The operating system keeps track of when each page was referenced by recording the time of reference or by maintaining a stack of references.This policy assumes the recent past will approximate the immediate future.

Program Code:

#include<stdio.h>
#include<conio.h>
int main()
{
int cnt[10],page[20],frame[10];
int page_fault=0,t1,t2,i,t=0,j,k,m=0,n=0,min,count=0,nop,frame_size;//nop-->no. of pages
clrscr();
printf("Enter the no.of pages:\t");
scanf("%d",&nop);
printf("Enter the size of frame:\t");
scanf("%d",&frame_size);
printf("Enter page sequence:\n");
for(i=0;i<nop;i++)
scanf("%d",&page[i]);
for(i=0;i<frame_size;i++)
{
  frame[i]=0;cnt[i]=0;   //initialize frame to zero
}
k=0;
for(i=0;i<nop;i++)
{
  m=0;
  for(j=0;j<frame_size;j++)
  cnt[j]=0;
  for(j=0;j<frame_size;j++)
  {
      if(frame[j]==page[i])
      {
     m++;
      }
  }
  if(m!=0)
  {
      count++;
      if(k>2)
      k=0;
      k=k+1;
      printf("page %d\t",page[i]);
      printf("  frame\t");
      for(j=0;j<frame_size;j++)
      printf(" %d",frame[j]);
      printf("\tNo page fault\t");
      printf("\tPage_fault till now:\t%d\n",page_fault);
  }
  else if(count>3)
  {
      for(j=0;j<frame_size;j++)
      {
     t2=(count-3);
     for(n=(count-1);n>t2;n--)
     {
       if(frame[j]==page[n])
       {  cnt[j]++;
       }
     }
      }
      for(j=0;j<3;j++)
      min=cnt[0];
      t=0;
      for(j=0;j<frame_size;j++)
      {
     if(cnt[j]<min)
     {   min=cnt[j];
         t=j;
      }
      }
      if(k>2)
      k=0;
      frame[t]=page[i];
      count++;
      k++;
      printf("page %d\t",page[i]);
      printf("  frame\t");
      for(j=0;j<frame_size;j++)
      printf(" %d",frame[j]);
      page_fault++;
      printf("\tpage fault occurs\t");
      printf("Page_fault till now:\t%d\n",page_fault);
}
else
{     if(k>2)
      { k=0;}
      frame[k]=page[i];
      count++;
      k++;
      printf("page %d\t",page[i]);
      printf("  frame\t");
      for(j=0;j<frame_size;j++)
      printf(" %d",frame[j]);
      page_fault++;
      printf("\tpage fault occurs\t");
      printf("Page_fault till now:\t%d\n",page_fault);
}
}
getch();
return 0;
}

Output:



 

FIFO Page Replacement Algorithm - C Program

C Program - FIFO Page Replacement Algorithm

Program code:

#include<stdio.h>
int main()
{
int k,nop,frame_size,i,j,temp=100,page_fault=0;
int page_name[20],frame[20];
int m,n=0,count=0;
printf("Enter the No. of pages\t");
scanf("%d",&nop);
printf("Enter the frame size\t");
scanf("%d",&frame_size);
printf("Enter page name\n");
for(i=0;i<nop;i++)
scanf("%d",&page_name[i]);
for(i=0;i<frame_size;i++)
frame[i]=0;
for(i=0,j=0;i<frame_size;i++,j++)
{
frame[j]=page_name[i];
page_fault++;
printf("\npage\t%d\t",page_name[i]);
printf("Frame\t");
for(k=0;k<frame_size;k++)
printf(" %d",frame[k]);
printf("\tPage fault till now:\t");
printf("%d",page_fault);
}
j=0;
 

for(m=i;m<nop;m++)

if(page_name[m]==frame[j])
{

j++;
count++;
printf("\npage\t%d\t",page_name[m]);
printf("Frame\t");
for(k=0;k<frame_size;k++)
printf(" %d",frame[k]);
printf("\tPage fault till now:\t");
printf("%d",page_fault);
 }

else
{

frame[j]=page_name[m];
page_fault++;
j++;
count++;
printf("\npage\t%d\t",page_name[m]);
printf("Frame\t");
for(k=0;k<frame_size;k++)
printf(" %d",frame[k]);
printf("\tPage fault till now:\t");
printf("%d",page_fault);

}
if(count==frame_size)
j=0;
}
return 0;
}

 Output:

Wednesday, 23 October 2013

CGPA TO PERCENTAGE CALCULATOR

CGPA TO PERCENTAGE CALCULATOR

Program: To calculate percentage from CGPA

Program Code:

import java.applet.*;
 import java.awt.*;
import java.awt.event.*;
/*
<applet code="applet2" width=400 height=200>
</applet>
*/

public class applet2 extends Applet implements ActionListener
 {        
           TextField Name, Reg_no, cgpa,per;
  public applet2()
 {
            setLayout(new GridLayout(4, 2, 10, 15));
            setBackground(Color.gray);  
            Name = new TextField(15);
            Reg_no = new TextField(15);
            cgpa = new TextField(15);
            per=new TextField(15);
            cgpa.addActionListener(this); 
            add(new Label("Enter Your Name"));
            add(Name);
            add(new Label("Enter Your Registration No."));
            add(Reg_no);
            add(new Label("Enetr your CGPA"));
            add(cgpa);
            add(new Label("Your percentage:"));
            add(per);
}

 public void actionPerformed(ActionEvent e)
{
           double fn; 
           double sn = Double.parseDouble(cgpa.getText());
            fn=sn*9;
            per.setText(" "+ fn);

 }
 }

Output:


Download the complete code:- Applet2.rar

              

Tuesday, 22 October 2013

JAVA APPLET PROGRAMMING - -

JAVA APPLET PROGRAMMING

Program Cycle vs Car racing via Java Applets

Program Code:-

import java.applet.*;
import java.awt.*;

/*<applet code="applet" height=500 width=1000>
</applet> */

public class applet extends Applet
 {

   int x,y,k,h,w,n;
   public void init()
     {
            h=getHeight();
            w=getWidth();
            n=0;k=0;
           setBackground(Color.cyan);
      }  
   public void paint(Graphics g)
    {
           int i,m;
           g.setColor(Color.red);
           g.drawString("Cycle vs Car Racing Championship 2013",w/2-70,50);
           g.setColor(Color.gray);
           g.fillRect(0,h/2-60,w,120);
           m=0;
           for(i=0;m<=w;i++)
           {   
                 g.setColor(Color.white);
                 g.fillRect(m,h/2-7,80,14);
                 m+=120;  
           }
           g.setColor(Color.red);
           g.fillRect(100,h/2-60,7,120);
           g.fillRect(w-50,h/2-60,7,120);

           g.setColor(Color.black);
           g.drawOval(n,h/2+25,30,30);
           g.drawOval(n+13,h/2+38,4,4);
           g.drawOval(n+46,h/2+36,8,8);
           g.drawOval(n+48,h/2+38,4,4);
           g.drawLine(n+15,h/2+40,n+50,h/2+40);
           g.drawLine(n+15,h/2+42,n+50,h/2+44);
           g.drawLine(n+15,h/2+38,n+50,h/2+36);
           g.drawOval(n+70,h/2+25,30,30);
           g.drawOval(n+84,h/2+39,2,2);
           g.drawLine(n+15,h/2+40,n+30,h/2+20);
           g.drawLine(n+30,h/2+20,n+50,h/2+40);
           g.drawLine(n+50,h/2+40,n+70,h/2+20);
           g.drawLine(n+30,h/2+20,n+70,h/2+20);
           g.drawLine(n+70,h/2+20,n+85,h/2+40);
           g.drawLine(n+70,h/2+20,n+70,h/2+15);
           g.drawLine(n+65,h/2+13,n+75,h/2+17);
           g.drawLine(n+65,h/2+13,n+62,h/2+13);
           g.drawLine(n+72,h/2+17,n+75,h/2+17);
           g.drawLine(n+30,h/2+20,n+30,h/2+15);
           g.drawLine(n+26,h/2+15,n+34,h/2+15);
           g.fillRect(n+26,h/2+10,8,6);
           g.fillRect(n+27,h/2+6,6,4);
           g.drawLine(n+26,h/2+10,n+75,h/2+17);
                     /*Car Making*/
           Color c=new Color(200,60,200);
           g.setColor(c);
           g.fillRoundRect(k,h/2-30,100,20,5,5);
           Color c1=new Color(20,160,200);
           g.setColor(c1);
           g.fillOval(k+10,h/2-20,20,20);
           g.fillOval(k+70,h/2-20,20,20);
           g.drawRect(k+20,h/2-45,60,15);
           g.drawLine(k+50,h/2-45,k+50,h/2-30);
           g.drawLine(k+20,h/2-45,k+10,h/2-30);
           g.drawLine(k+80,h/2-45,k+90,h/2-30);
           g.setColor(Color.red);
           g.drawLine(100,h/2-70,100,h/2-120);
           g.drawString("Start",100,h/2-150);
                     /* Creating Flags*/
           int q[]={100,130,100};
           int e[]={h/2-120,h/2-110,h/2-100};
           g.fillPolygon(q,e,3);
           g.drawLine(w-50,h/2-60,w-50,h/2-120);
           g.drawString("End",w-50,h/2-150);
           int t[]={w-50,w-20,w-50};
           int y[]={h/2-120,h/2-110,h/2-100};
           g.fillPolygon(t,y,3);
           n+=2;
           k+=3;

           try
           { Thread.sleep(100);
            }
            catch(Exception ex)
            {
             }
           if(k+100<w-50)
           {repaint();}
         
       }
}

Output:-
   



Download the complete code:- Applet1.rar