Monday, 30 September 2013

Optimal Page Replacement

Since optimal page replacement is not used practically and is implement for comparing algos hence it doesn't need a dynamic queue or link list and hence here it has been implemented using arrays

let 70120304230321201701 be the sequence string
number of pages are 20
if frame size taken 3 number of page faults will be 9

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

int frame[100];//frame
int a[100];//to store pages
int Frame_size;//to store frame size;
int n;//to store number of pages to be input by user
int j=0;
int nulp()//function to return the index number of the page which will not be used for longest period ie..which is to removed from the frame
{
    int i=0;
    int jj=j+1;
    int max=-1;
    int pos;
  while(1)
  {
    while(1)
    {
      if(frame[i]==a[jj])
      {
        if(max<(jj-j))
        {
        max=jj-j;
        pos=i;
        }
        jj=j+1;  
        i++;
        break;    
      }
      else  
      {
             
               jj++;
               if(jj==n)
               {
               max=jj-n;
               pos=i;
               return i;
               jj=j+1;
               i++;
               break;
               }
      }
   
    }
    if(i==Frame_size)
      break;      
  }
  return pos;
}

int ifexist(int i)
{
    int j1;
    for(j1=0;j1<Frame_size;j1++)
    {
                             if(frame[j1]==i)
                             return 1;
    }
    return 0;
}
main()
{
      puts("Enter the number of pages");
      scanf("%d",&n);
      puts("Enter  pages");
      int i=0;
      for(i=0;i<n;i++)
      scanf("%d",&a[i]);
      puts("Eneter the Frame size");
      scanf("%d",&Frame_size);
   
   
   
      int Fault=0;//counting the number of page faults occur
      int counter=0;//to make sure size of frame is equal to the Frame_size as entered
   
      frame[counter]=a[j];
      counter++;
      j++;
      Fault++;//we can be sure when first element will be entered it will be a fault
      while(j<n)
      {
             if((counter<Frame_size)&&(!ifexist(a[j])))
             {
                frame[counter]=a[j];
                j++;
                counter++;
                Fault++;                          
             }
             if(ifexist(a[j]))
             j++;
             if((counter==Frame_size)&&(!ifexist(a[j])))
             {
             
               int i=nulp();//returns the index number of element which will not be utilized for longest period of time or page which has to be removed                                      
               frame[i]=a[j];
               j++;
               Fault++;
                                 
             }            
      }
      printf("%d",Fault);
      getch();    
}

Producing a mirror tree of given tree


Given Tree
                a                              
             /    \
            b      c
          /  \
         d    e
Mirror Tree

                a
             /    \
            c      b
                  /  \  
                e    d    

ALGO

Here we have simply traversed the tree in postfix manner and instead of printing the node we have switched it right child with left and vice versa
Per-order is used because root's child will be  switched at last




#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<time.h>//for producing random tree

struct node
{int n;
struct node *left;
struct node *right;
};
int c=0;
 create(int d,struct node**m)
{
            struct node *q=*m;
       if(*m==NULL)
       {
       struct node *r=(struct node*)malloc(sizeof(struct node));

        r->n=d;
        r->left=NULL;
        r->right=NULL;
        *m=r;
       
        }
       
       else
           {
               if(q->n>d)
           create(d,&(q->left));            
           if(q->n<d)
           create(d,&(q->right));            
                                         
                         
                          }
                         
           }
        display(struct node *root)
       {
           
            if(root==NULL)
            return;
           
            else
            {
               
            display(root->left);
            display(root->right);
            printf("%d\n",(root->n));
            }
       
        }
       
        conversion(struct node *m)//code for creating mirror tree of the given tree
        {
                          struct node *n=m;
                          if(n==NULL)
                          return;
                          conversion(n->left);
                          conversion(n->right);
                          struct node *t=n->left;
                          n->left=n->right;
                          n->right=t;
                          
                          }
        
       
     
main()
{
      struct node *root=NULL;
      srand(time(NULL));
      int i;
     for(i=0;i<=5;i++)
      create((rand()%20),&root);
     
      display(root);//before conversion
      conversion(root);
      display(root);//after conversion
      getch();
     
    }
       

Sunday, 29 September 2013

FIFO Page Replacement

//ALL there are three possibileties 
//first-: All frames are occupied and fault occurs, in such case we need to first deque and then enqueue the required page and variable counting the fault will be incremented
//second-: Some Frame is empty and fault occurs, in such we will enqueue the required page and increment the fault variable
//third-: if fault does not occur,in such case we will  move to next page 

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct que
{
       int data;
       struct que *next;
       };
     
        void enque(struct que **q,int a)
       {
                      struct que *t,*r;
                   
                      if(*q==NULL)
                      {        
                        t=(struct que*)malloc(sizeof(struct que));
                        t->data=a;
                        t->next=NULL;
                        *q=t;        
                               
                                  }
                                  else
                                  {
                                      t=*q;
                                      while(t->next!=NULL)
                                      t=t->next;
                                      r=(struct que*)malloc(sizeof(struct que));
                                      r->data=a;
                                      r->next=NULL;
                                      t->next=r;
                                      }
                     
                     
                             }
                                             
                     
     
 int deque(struct que **q)
{
     if(*q==NULL)
     {
     printf("que is underflow");
     return -999;
     }
     struct que *r;
     r=*q;
     int x;
     x=r->data;
     *q=r->next;
     free(r);
     return x;
   
 }
 int ifexist(struct que **q,int i)
 {
     struct que *t,*r;
     t=*q;
                                      while(t!=NULL)
                                      {
                                     
                                       if(t->data==i)
                                       return 1;                
                                      t=t->next;
                                      }
                                      return 0;
 }
main()
{
     int a[100];//page sequence;
     int n;
     int Frame_size;
     puts("Eneter the number of pages to be entered");//number of pages
     scanf("%d",&n);
     int i=0;
     for(i=0;i<n;i++)
     scanf("%d",&a[i]);
     puts("\nEnter the size of Frame");//number of frames
     scanf("%d",&Frame_size);
     struct que *p;
     p=NULL;
     int j=0;
     int counter=0;//to count the number of frames
     enque(&p,a[j]);//first element is to insert without any condition
     j=j+1;
     int fault=1;//first element will always produce fault
     counter=counter+1;
     while(j<n)
     {
      if((counter==Frame_size)&&(!ifexist(&p,a[j])))//if all frames are filled and fault occurs
      {
      deque(&p);
      enque(&p,a[j]);
                     j++;
                     fault=fault+1;
   
                                       
     }
                                       
     if(ifexist(&p,a[j]))//if no fault occurs
     j++;
   
     if((counter!=Frame_size)&&(!ifexist(&p,a[j])))//if all frame not filled and fault occurs
     {
     enque(&p,a[j]);
     j++;
     fault=fault+1;
     counter=counter+1;
     }                                  
                                       
                                         }                                                    
      printf("\nNumber of Page fault occurs %d",fault);
      getch();
   
   
   
      }

Thursday, 26 September 2013

DFS Search

//Here we have used the Adjacnet Matrix method hence time complexity for the DFS is O(V^2)
//Since whole matrix of V^2 has to be searched for the adjacnet nodes
//If Adjacent list would have been used in that case complexiy would have been 0(V+E) since only only V vertices had been passed and E edges would have searched for
//Not an optimal solution...
//But saves lots of space
//if A[u][v]=1 it implies that there exist a path from u to v hence v is adjacent to u

DFS:A>B>D>F>C>G>E

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int V;//Number of vertices

int E;//number of Edges..not used since adjacent matrix  representation has been used

int A[10][10];//Adjacent matrix

int visited[10]={0,0,0,0,0,0,0,0,0,0};//visited array...initially all are set to zero since no node has been visited

void dfs(int u)
{
        visited[u]=1;//whatever node is passed to node is consider to be visited hence we make it visited
        printf("%c\t",(65+u));//printing the node visited..and nodes are in character form A,B,C..
        int i;
        for( i=0;i<V;i++)
        if(!visited[i]&&A[u][i])//if node is not visited and it is adjacent to the node passed as argument to the function then only we will visit it..the first adjacent node will be visited and the remaing adjacent node will be visited recursively
        {    
        dfs(i);
        }
}

main()
       {
           
             puts("Enter the number of Vertices and Graph");
             scanf("%d",&(V));
             scanf("%d",&(E));
           
           
             int i;
             int j;
             puts("Enter the Adjacent Matrix Row wise");
             for(i=0;i<V;i++)
             {
                for(j=0;j<V;j++)
                scanf("%d",&(A[i][j]));
             }                                
              i=0,j=0;        
              puts("Adjacent Matrix");        
             for(i=0;i<V;i++)
             {
                                 
                for(j=0;j<V;j++)
                printf("%d\t",(A[i][j]));
                printf("\n");
                   
             }
         
             dfs(0);//Here since graph is single componet we can take only one vertex and start dfs else in separate component then we have to do dfs for all the nodes

 getch();          
}

Tuesday, 10 September 2013

Sjf preemptive...with idle time

//first all the processes are taken in the array of structure  P[]
//THEN a[] contains the burst time of all the process in order of their arival
//min function calculates the minimum value in the array a[], beteen the  index number 0 to j where j is number of process which have arived
//maximum valu of j can be equal to number of process
//if the a[i] is 0 means burst time remain is 0 and hence that procee need not be processed hence we will ignore it
// if a[i] is 0 till i to j and j is not equal to the number of process then cpu is idle all the arrived porcess have been finished and waiting for new process
// if a[i] is 0 for all the process and j=number of process then all process have been finished
// Moment burst time of the process becomes 0 at that time of interval value of cl(clock) become completition time of that process




#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct process
{
       int at;
       int bt;
       int rt;
       int  c;//process completiotion time
       int wt;
       int trt;
       };
     
     
       int min(int *a,int j)
       {
           int min;
        int i=0;
        int flag=0;
        int pos;
        for(i=0;i<=j;i++)
        {
        if(a[i]==0)
        continue;
        min=a[i];
        pos=i;
        flag=1;
        break;
        }
        if(flag==0 &&j!=3)
        return -2;
        else if(flag==0)
        return -1;
        else
        for(i=0;i<=j;i++)
        {
        if(a[i]==0)
        continue;
        if(a[i]<min)
        {
        min=a[i];
        pos=i;
        }
       }
       return pos;
       }
void display(struct process *p)

    int i=1;
    for(i=0;i<4;i++)
    {
        printf("Arrival time%d  ",p[i].at);
        printf("Burst Time%d  ",p[i].bt);
        printf("comlete Time%d  ",p[i].c);
        printf("waiting Time%d  ",(p[i].c-p[i].at-p[i].bt));
        printf("\n\n");
}
      
  
}   
main()
{
      struct process p[100];
      int b[100];
      int a[4];//since number of process taken are four
      puts("Enetr the process arival time and burst time");
      int i=0;
      int j=0;
      while(1)
      {
    
      scanf("%d",&(p[i].at));
      scanf("%d",&(p[i].bt));
      a[j]=p[i].bt;
      j++;
      i++;
      if(i==4)
      break;
      }
    
      //array a[] contains the burst time of the process
    
      //array p[] contains the proceess
      

      int x=0;
      int y=0;
      int m;
       j=0;
       i=0;
       int cl=0;
       int pid=2;
       while(cl<p[0].at)
       {
       printf("idle  %d---",cl);
       cl++;
       printf("%d\n",cl);
    }
      while(x!=-1)
      {
        a[x]=a[x]-1;
        cl++;
        if(a[x]==0)
        p[x].c=cl;
      
        if(cl<p[i+1].at)
    {  
        x=min(a,j);
        if(x==-2)
        {
                 while(cl<p[i+1].at)
                 {
                 printf("idle  %d---",cl);
       cl++;
       printf("%d\n",cl);
                 }
               
         }
      
    }
        if(cl==p[i+1].at)
        {j++;
        x=min(a,j);
        i++;
         }
         if(i==4)
         x=min(a,3);
  
      
}  
      
    
    
    

      printf("\n\n");
     display(p);
      getch();
      }

Monday, 9 September 2013

SJF-Preemetive---Only gantt chart

Following code produces Gantt chart for the SJF-preemtive schedulign.....works for 4 process.
In a Array  process one is represented by 0 and so on

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct process
{
       int at;
       int bt;
       int rt;
       int  c;
       int wt;
       int trt;
       };
      
      
       int min(int *a,int j)
       {int min;
        int i=0;
        int flag=0;
        int pos;
        for(i=0;i<=j;i++)
        {
        if(a[i]==0)
        continue;
        min=a[i];
        pos=i;
        flag=1;
        break;
        }
        if(flag==0)
        return -1;
        else
        for(i=0;i<=j;i++)
        {
        if(a[i]==0)
        continue;
        if(a[i]<min)
        {
        min=a[i];
        pos=i;
        }
       }
       return pos;
       }
void display(struct process *p)
{  
    int i=1;
    while(1)
    {
        printf("Arrival time%d  ",p[i].at);
        printf("Burst Time%d  ",p[i].bt);
        printf("Response Time%d  ",p[i].rt);
        printf("comlete Time%d  ",p[i].c);
        printf("waiting Time%d  ",(p[i].rt-p[i].at));
        printf("\n\n");
        i++;
        if(i==5)
        break;
    }
}    
main()
{
      struct process p[100];
      int b[100];
      int a[4];//since number of process taken are four
      puts("Enetr the process arival time and burst time");
      int i=0;
      int j=0;
      while(1)
      {
      i++;
      scanf("%d",&(p[i].at));
      scanf("%d",&(p[i].bt));
      a[j]=p[i].bt;
      j++;
      if(i==4)
      break;
      }
      int x=0;
      int y=0;
       j=0;
      while(x!=-1)
      {
        a[x]=a[x]-1;
        b[y]=x;
        y++;
        if(j!=3)
        j++;
        x=min(a,j);
     
      }
      printf("\n\n");
      for(i=0;i<y;i++)
      printf("%d\n",b[i]);
      getch();
      }

Sunday, 8 September 2013

FCFS ALGORITHM

First we tried to calculate the response time and complete time
for the first process response time is equal to arrival time and complete time is equal to the sum of response time and burst time.
For  next process 
                            if arrival time is equal to the complete time of the previous process then there will be  no idle time and response time is equal to the complete time of the previous process
                           if arrival time is greater than the complete time of the previous process then there will be the idle time and the response time will equal to the arival time of the porcess
                           if arrival time is less than the complete time of the previous process then there will again no idle time and response time is equal to to the complete time of the previous time

For all the above case 
complete time =response+burst time
waiting time=response time-arrival time


#include<stdio.h>
#include<stdlib.h>

struct process
{
       int at; //arival time
       int bt; //burst time
       int rt; //response time
       int  c; //completed time
       int wt; //waiting time
       int trt;
       };  //structure to hold all the attributes associated with process

void display(struct process *p)//Function to display all the attributes of the process
{  
    int i=1;
    while(1)
    {
        printf("Arrival time%d  ",p[i].at);
        printf("Burst Time%d  ",p[i].bt);
        printf("Response Time%d  ",p[i].rt);
        printf("comlete Time%d  ",p[i].c);
        printf("waiting Time%d  ",(p[i].rt-p[i].at));
        printf("\n\n");
        i++;
        if(i==4)
        break;
    }
}    
void fcfs(struct process *p)//first position of the array is ignored and counting has started from index value one
{
     if((p[1].at)>0)
      printf("Idle   0-%d\n",p[1].at);
      p[1].rt=p[1].at;
      p[1].wt=0;
      p[1].c=(p[1].at+p[1].bt);
      int i=2;
      while(1)
      {
     
             if(p[i].at==p[i-1].c)
             p[i].rt=p[i-1].c;
            
             if((p[i].at)>(p[i-1].c))
             {
             printf("Idle   %d-%d\n",p[i-1].c,p[i].at);
             p[i].rt=p[i].at;
             p[i].c=p[i].rt+p[i].bt;
              }
             
              if(p[i].at<p[i-1].c)
              {
              p[i].rt=p[i-1].c;
              p[i].c=p[i].rt+p[i].bt;
              }
             
              i++;
              if(i==4)
              break;
        }
 }
main()
{
      struct process p[100];
      puts("Enetr the process arival time and burst time");
      int i=0;
      while(1)
      {
      i++;
      scanf("%d",&(p[i].at));
      scanf("%d",&(p[i].bt));
      if(i==3)
      break;
      }
      fcfs(p);
      display(p);
      }

Wednesday, 4 September 2013

Cookie handling through Javascript

Following Code will work only in mozilla...for chrome browser need to be handled explicitly

<html>
<head>

</head>

<body>
<button type "button"  onclick="z()">A</button>
<button type "button"  onclick="x()">B</button>
<p id="m">A 0</p>
<p id="g">B 0</p>

<script>
var b=document.cookie
document.write(b)
if(b=="")
{
var i=0
var j=0;
}
else
{
var a=document.cookie
var i=parseInt(a.charAt(0))
var j=parseInt(a.charAt(2))
}
function z()
{
i=i+1;
document.getElementById("m").innerHTML="A: "+i
document.cookie=i+" "+(j+0)
}
function x()
{
j=j+1;
document.getElementById("g").innerHTML="B: "+j
document.cookie=i+" "+(j+0)
}
</script>

</body>
</html>