Wednesday 25 April 2012

OS Programs


// FCFS AVGWAIT
#include<iostream>
using namespace std;
class avg
{
 int a[10],s,t,n,w,tt;
public:
void read();
void display();
void awt();
void att();
};
void avg::read()
{
        cout<<"enter no of members at theatre counter \n";
        cin>>n;
        cout<<"enter number of tickets want to purchase \n";
        for(int i=0;i<n;i++)
        {
        cout<<"a["<<i<<"]=";
        cin>>a[i];
        }
}
void avg::awt()
{       s=0;t=0;
        for(int i=0;i<n-1;i++)
        {
                s=s+a[i];
                t=t+s;
        }
cout<<"sum of awt is="<<t<<"\n";
        w=t/n;
}
void avg::att()
{
        t=0;s=0;
        for(int i=0;i<n;i++)
        {
                t=t+a[i];
                s=s+t;
        }
cout<<"sum of att is="<<s<<"\n";

        tt=s/n;
}
void avg::display()
{
        cout<<"no of tickets each person will purchase \n";
        for(int i=0;i<n;i++)
        cout<<"a["<<i<<"]="<<a[i];
cout<<"average waiting time is"<<w<<"\n";
        cout<<"average turn over time is"<<tt<<"\n";
}
int main()
{
        avg ob;
        ob.read();
        ob.awt();
        ob.att();
        ob.display();
return 0;
}





// SJF  sjf1.cpp
#include<iostream>
using namespace std;
class sjf
{
 int a[10];
float w,tt,s,t,n;
public:
void read();
void display();
void awt();
void att();
void sort();
};
void sjf::read()
{
        cout<<"enter no of processors \n";
        cin>>n;
        cout<<"enter the cpu burst time for each processor \n";
        for(int i=0;i<n;i++)
        {
        cout<<"a["<<i<<"]=";
        cin>>a[i];
        }
}
void sjf::sort()
{
int x;
cout<<"before sorting\n";
        for(int i=0;i<n;i++)
        cout<<"a["<<i<<"]="<<a[i];
        cout<<"after sorting\n";
        for(int j=0;j<n-1;j++)
        {
        for(int i=0;i<n-j-1;i++)
        {
                if(a[i]>a[i+1])
                {
                x=a[i];
                a[i]=a[i+1];
                a[i+1]=x;
                }
        }
}
for(int i=0;i<n;i++)
        cout<<"a["<<i<<"]="<<a[i];
}
void sjf::awt()
{       s=0;t=0;
        for(int i=0;i<n-1;i++)
        {
                s=s+a[i];
                t=t+s;
        }
cout<<"sum of awt is="<<t<<"\n";
        w=t/n;
}
void sjf::att()
{
        t=0;s=0;
 for(int i=0;i<n;i++)
        {
                t=t+a[i];
                s=s+t;
        }
cout<<"sum of att is="<<s<<"\n";

        tt=s/n;
}
void sjf::display()
{
        cout<<"the cpu burst time for each processor is \n";
        for(int i=0;i<n;i++)
        cout<<"a["<<i<<"]="<<a[i];
cout<<"average waiting time is"<<w<<"\n";
        cout<<"average turn over time is"<<tt<<"\n";
}
int main()
{
        sjf ob;
        ob.read();
        ob.sort();
        ob.awt();
        ob.att();
        ob.display();
return 0;
}




// Sjf With Arrival Time

#include<iostream>
using namespace std;
class avg
{
 int a[10],b[10];
 float w,tt,s,t,n;
 int n,Bu[20];
 float Twt,Awt,A[10],Wt[10],w;
public:
      void Getdata();

       void Sjf();
      void SjfP();
        void display();

};

void avg::Getdata()
{
        int i;
        cout<<"Enter the no of processes:";
        cin>>n;
        for(i=1;i<=n;i++)
        {
                cout<<"Enter The BurstTime for Process p"<<i<<"=        ";
                cin>>Bu[i];
        }
}
void avg::Sjf()
{
        int i,j,temp,B[10];
        Twt=0.0;
        for(i=1;i<=n;i++)
        {
                B[i]=Bu[i];
                cout<<"Burst time for process p"<<i<<"= ";
                cout<<B[i];
        }
        for(i=n;i>=1;i--)
        {
                for(j=1;j<=n;j++)
                {
                        if(B[j-1]>B[j])
                        {
                                temp=B[j-1];
                                B[j-1]=B[j];
                                B[j]=temp;
                        }
                }
        }

        Wt[1]=0;
        for(i=2;i<=n;i++)
        {
                Wt[i]=B[i-1]+Wt[i-1];
        }
        for(i=1;i<=n;i++)
                Twt=Twt+Wt[i];
        Awt=Twt/n;
        cout<<"Total   Weighting Time="<<Twt;
        cout<<"Average Weighting Time="<<Awt<<"";
}

void cpuschedule::SjfP()
{
        int i,j,m,Wt[10],k,B[10],A[10],Tt=0,Wtm[10],temp;
        char S[20],start[20];
        int max=0,Time=0,min;
        float Twt=0.0,Awt;
        for(i=1;i<=n;i++)
        {
                B[i]=Bu[i];
                cout<<"Burst time for process P"<<i<<"= "<<B[i];
                if(B[i]>max)
                        max=B[i];
                Wt[i]=0;
                S[i]='T';
                start[i]='F';
                Tt=Tt+B[i];
                cout<<"Enter the Arrival Time for"<<i<<"th process=     ";
                cin>>A[i];
                if(A[i]>Time)
                        Time=A[i];
        }
        cout<<"Max="<<max;
        int w=0,flag=0,t=0;
        i=1;
        while(t<Time)
        {
                if(A[i]<=t && B[i]!=0)
                {
                        if(flag==0)
                        {
                                Wt[i]=Wt[i]+w;

                                cout<<"Wt["<<i<<"]="<<Wt[i];
                        }
                        B[i]=B[i]-1;
                        if(B[i]==0)
                                S[i]='F';
                        start[i]='T';
                        t++;
                        w=w+1;
                        if(S[i]!='F')
                        {
                                j=1;flag=1;
                                while(j<=n && flag!=0)
                                {
                                        if(S[j]!='F' && B[i]>B[j] && A[j]<=t && i!=j )
                                        {
                                                flag=0;
                                                Wt[i]=Wt[i]-w;
                                                i=j;
                                        }
                                        else
                                        {
                                                flag=1;
                                        }
                                        j++;
                                }
                        }
                        else
                        {
                                i++;
                                j=1;
                                while(A[j]<=t &&j<=n)
                                {
                                        if(B[i]>B[j] && S[j]!='F')
                                        {
                                                flag=0;
                                                i=j;
                                        }
                                        j++;
                                }
                        }
                }
                else
                        if(flag==0)
                        i++;
        }


        cout<<"Printing remaining burst time";
        for(i=1;i<=n;i++)
                cout<<"B["<<i<<"]="<<B[i];
        cout<<"";

        while(w<Tt)
        {
                min=max+1;
                i=1;
                while(i<=n)
                {
                        if(min>B[i] && S[i]=='T')
                        {
                                min=B[i];
                                j=i;
                        }
                        i++;
                }
                i=j;
                if(w==Time && start[i]=='T')
                {
                        w=w+B[i];
                        S[i]='F';
                }
                else
                {
                        Wt[i]=Wt[i]+w;
                        w=w+B[i];
                        S[i]='F';
                }
        }

cout<<"Weight info";

        for(i=1;i<=n;i++)
                cout<<"WT["<<i<<"]="<<Wt[i];
cout<<"after subtracting arrival time";
        for(i=1;i<=n;i++)
        {
                Wt[i]=Wt[i]-A[i];
                cout<<"WT["<<i<<"]="<<Wt[i];
        }
        for(i=1;i<=n;i++)
                Twt=Twt+Wt[i];
        Awt=Twt/n;
        cout<<"Average Weighting Time="<<Awt;
}
int main()
{
        avg ob;
        ob.getdata();

        b.sjf();
        ob.sjfp();
        ob.display();
return 0;
}




// PRIORITY      priority.cpp
#include<iostream>
using namespace std;

class schedule
{
        int n,Bu[20];
        float Twt,Awt,Wt[10],w,Att,Ttt;
public:
        //Getting the No of processes & burst time
        void Getdata();
        void Priority();
};
void schedule::Getdata()
{
        int i;
        cout<<"Enter the no of processes:";
        cin>>n;
        for(i=1;i<=n;i++)
        {
                cout<<"Enter The BurstTime for Process p"<<i<<"=        ";
                cin>>Bu[i];
        }
}


void schedule::Priority()
{
        int i,B[10],P[10],j;
        w=0.0;
        int max;
        Twt=0.0;
        max=1;
        for(i=1;i<=n;i++)
        {
                B[i]=Bu[i];
                cout<<"Burst time for process p"<<i<<"= ";
                cout<<B[i];
                cout<<"Enter the priority for process P"<<i<<"= ";
                cin>>P[i];
                if(max<P[i])
                        max=P[i];
        }
        j=1;
        while(j<=max)
        {
                i=1;
                while(i<=n)
                {
                        if(P[i]==j)
                        {
                                Wt[i]=w;
                                w=w+B[i];
                        }
                        i++;
                }
                j++;
        }

        //calculating average weighting Time
        for(i=1;i<=n;i++)
                Twt=Twt+Wt[i];
        Awt=Twt/n;
        cout<<"Total   Weighting Time="<<Twt<<"";
        cout<<"Average Weighting Time="<<Awt<<"";
        //calculating turn over time
        for(i=1;i<=n+1;i++)
        Ttt=Ttt+Wt[i];

Att=Ttt/n;
         cout<<"Total   Turn over Time="<<Ttt<<"";
        cout<<"Average Turn over Time="<<Att<<"";

}

int main()
{
        int ch,cho;
        schedule c;
        do
        {
                cout<<"                          MENU\n";
                cout<<"1.Getting BurstTime";

                cout<<"2.Priority";
                cout<<"3.EXIT";
                cout<<"Enter your choice";
                cin>>ch;
                switch(ch)
                {
                case 1:
                        c.Getdata();
                        break;
                case 2:
                        cout<<"PRIORITY SCHEDULING";
                        c.Priority();
                        break;
                case 3:
                        break;
                }
        }while(ch<=2);
}





// RoundR.cpp


#include<iostream>
using namespace std;
class RoundR
{
         int n,Bu[20];
        float Twt,Awt,A[10],Wt[10],w;
        public:
        void Getdata();
        void roundrobin();

};
void RoundR::Getdata()
{
        int i;
        cout<<"Enter the no of processes:";
        cin>>n;
        for(i=1;i<=n;i++)
        {
                cout<<"Enter The BurstTime for Process p"<<i<<"=        ";
                cin>>Bu[i];
        }
}
void RoundR::roundrobin()
{

        int i,j,tq,k,B[10],Rrobin[10][10],count[10];
        int max=0;
        int m;
        Twt=0.0;
        for(i=1;i<=n;i++)
        {
                B[i]=Bu[i];
                cout<<"Burst time for process p"<<i<<"= ";
                cout<<B[i];
                if(max<B[i])
                        max=B[i];
                Wt[i]=0;
        }
        cout<<"Enter the Time Quantum=";
        cin>>tq;
         m=max/tq+1;
        for(i=1;i<=n;i++)
        {
                for(j=1;j<=m;j++)
                {
                        Rrobin[i][j]=0;
                }
        }
        i=1;
        while(i<=n)
        {
                j=1;
                while(B[i]>0)
                {
                        if(B[i]>=tq)
                        {
                                B[i]=B[i]-tq;
                                Rrobin[i][j]=tq;
                                j++;
                        }
                        else
                        {
                                Rrobin[i][j]=B[i];
                                B[i]=0;
                                j++;
                        }
                }
                count[i]=j-1;
                i++;
        }

        cout<<"Display";
        for(i=1;i<=n;i++)
        {
                for(j=1;j<=m;j++)
                {
                        cout<<"Rr["<<i<<","<<j<<"]="<<Rrobin[i][j];
                        cout<<" ";
                }
                cout<<" ";
        }
        //calculating weighting time
        int x=1;
        i=1;
        while(x<=n)
        {
                for(int a=1;a<x;a++)
                {
                        Wt[x]=Wt[x]+Rrobin[a][i];
                }
                i=1;
                int z=x,tt;
                j=count[z];
                k=1;
                while(k<=j-1)
                {
                        if(i==n+1)
                        {
                                i=1;
                                k++;
                        }
                        else
                        {
                                if(i!=z)
                                {
                                        Wt[z]=Wt[z]+Rrobin[i][k];
                                         }
                                i++;
                        }
                }
               x++;
        }
        for(i=1;i<=n;i++)
                cout<<"Weighting Time for process P"<<i<<"="<<Wt[i];
        //calculating Average Weighting Time
        for(i=1;i<=n;i++)
                Twt=Twt+Wt[i];
        Awt=Twt/n;
        cout<<"Total   Weighting Time="<<Twt;
        cout<<"Average Weighting Time="<<Awt<<"";

}
int main()
{
RoundR ob;
ob.Getdata();
ob.roundrobin();
return 0;
}






//  ROUNDROBIN          roundrobin.cpp




#include<iostream>
using namespace std;
class roundrobin
{
public:
 int a[100],b[100],c[100],d[100],e[100],m,p;
  void getinfo();
  void calculations();
float n;
};
void roundrobin::getinfo()
{
 cout<<"enter the no of processes\n";
 cin>>n;
 cout<<"enter each process burstime\n";
 for(int i=0;i<n;i++)
 {
         cin>>a[i];
         d[i]=a[i];
 }
 cout<<"enter the quantum time\n";
 cin>>m;
}
void roundrobin::calculations()
{
 int s=0;
for(int i=0;i<n;i++)
c[i]=0;
for(int i=0;i<n;i++)
 {
   if(a[i]%m==0)
   s=s+a[i]/m;
   else
   s=s+(a[i]/m+1);
 }

cout<<" the number titrations  " << s <<"\n";
int i=0,j=0;
 while(i<s)
 {
   if(j==n)
    {
     j=0;
    }
   else
    {
        if(a[j]==0)
         j++;
        else if(a[j]>m)
        {
           b[i++]=m;
           a[j++]=a[j]-m;

        }
        else if(a[j]==m)
        {
          b[i++]=m;
          int sum=0;
          for(int k=0;k<i;k++)
          sum=sum+b[k];
          c[j]=sum-d[j];
          e[j]=sum;
          a[j++]=0;

        }
        else if(a[j]<m)
        {
          b[i++]=a[j];
          int sum=0;
          for(int k=0;k<i;k++)
          sum=sum+b[k];
          c[j]=sum-d[j];
          e[j]=sum;
          a[j++]=0;

        }
     }
}
cout<<"\n";
for(int i=0;i<s;i++)
cout<<b[i]<<"\t";
cout<<"\n";
float awt,att; int wt=0,tt=0;
for(int i=0;i<n;i++)
wt=wt+c[i];
awt=wt/n;
cout<<"average waiting time is:"<<awt<<"\n";
for(int i=0;i<n;i++)
tt=tt+e[i];
att=tt/n;
cout<<"\naverage turn around time is:"<<att<<"\n";
}

int main()
{
 roundrobin r;
 r.getinfo();
 r.calculations();
}





// vi bankers.cpp


#include<iostream>
using namespace std;
class bankalg
{
        int **alloc;
        int **max;
        int *work;
        int *avail;
        int **need;
        int *safe;
        int *flag;
        int p,r;
        public:
        void read();
        void safe_sequence();
        void request_resource();
};
void bankalg::read()
{
        int i,j;
        cout<<"\nEnter the total number of processes\n";
        cin>>p;
        cout<<"\nEnter the number of resource types\n";
        cin>>r;
        alloc=new int* [p];
        max=new int* [p];
        need=new int* [p];
        avail=new int[r];
        work=new int[r];
        for(i=0;i<p;i++)
        {
                alloc[i]=new int[r];
                max[i]=new int[r];
                need[i]=new int[r];
        }
        cout<<"\nEnter the allocated resources of each process\n";
        for(i=0;i<p;i++)
        {
                cout<<"P"<<i<<":";
                for(j=0;j<r;j++)
                        cin>>alloc[i][j];
        }
        cout<<"\nEnter the Maximum number of resources for each process\n";
        for(i=0;i<p;i++)
        {
                cout<<"P"<<i<<":";
                for(j=0;j<r;j++)
                        cin>>max[i][j];
        }
        cout<<"\nEnter the available resources of each type\n";
        for(i=0;i<r;i++)
                cin>>avail[i];
        for(i=0;i<r;i++)
                work[i]=avail[i];
}
void bankalg::safe_sequence()
{
        int i,j;
        safe=new int[p];
        flag=new int[p];
        for(i=0;i<p;i++)
        {
                flag[i]=0;

        }

        for(i=0;i<p;i++)
                for(j=0;j<r;j++)
                        need[i][j]=max[i][j]-alloc[i][j];
        int l=0;
        for(int z=0;z<(p*p);z++)
        {
        for(int i=0;i<p;i++)
        {       if(flag[i]==0)
                {

                j=0;
                while(j<r)
                {       if(need[i][j]<=avail[j])
                        {
                                if(j==r-1)
                                {       flag[i]=1;
                                        for(int k=0;k<r;k++)
                                                avail[k]+=alloc[i][k];
                                        safe[l++]=i;
                                        cout<<"\n"<<i<<"\n";
                                } j++;

                        }
                        else break;
                }
                }
        }
        for(int i=0;i<p;i++)
        {
                if(flag[i]==1)
                {       if(i==r-1)
                        {
                                cout<<"\nThe safe sequence is:\n";
                                for(j=0;j<p;j++)
                                        cout<<"P"<<safe[j]<<" ";
                                return;
                       }
                }
        }
        }
        cout<<"\nSystem is not in safe state\n";
}
void bankalg::request_resource()
{
        int m;
        int *request=new int[r];
        cout<<"Enter the the new request:\nProcess which is requesting:";
        cin>>m;
        cout<<"Request:";
        for(int i=0;i<r;i++)
                cin>>request[i];
        int i=0;
        while(i<r)
        {
                if(request[i]<=need[m][i])
                        i++;
                else
                {
                        cout<<"Error.";
                        exit(1);
                }
        }
        i=0;
        while(i<r)
        {
                if(request[i]<avail[i])
                        i++;
                else
                        break;
        }
        if(i==3)
        {
                for(int i=0;i<r;i++)
                        avail[i]=work[i];
                for(int i=0;i<r;i++)
                {
                        avail[i]=avail[i]-request[i];
                        alloc[m][i]+=request[i];
                        need[m][i]-=request[i];
                }
                safe_sequence();
        }
        else
                cout<<"Request cannot be granted.";
}
int main()
{
        int choice;
        bankalg ob;
        ob.read();
        ob.safe_sequence();
        cout<<"\nIf you want to enter a new request press 1. Else press 2.\n";
        cin>>choice;
        switch(choice)
        {
                case 1:ob.request_resource();
                        break;
                case 2:exit(1);
        }
}


// Detection algorithm



#include<iostream>
using namespace std;
class bankalg
{
        int **alloc;
        int **request;
        int *work;
        int *avail;
        int *safe;
        int *flag;
        int p,r;
        public:
        void read();
        void safe_sequence();
        void request_resource();
};
void bankalg::read()
{
        int i,j;
        cout<<"\nEnter the total number of processes\n";
        cin>>p;
        cout<<"\nEnter the number of resource types\n";
        cin>>r;
        alloc=new int* [p];
        request=new int* [p];
        avail=new int[r];
        work=new int[r];
        for(i=0;i<p;i++)
        {
                alloc[i]=new int[r];
                request[i]=new int[r];
        }
        cout<<"\nEnter the allocated resources of each process\n";
        for(i=0;i<p;i++)
        {
                cout<<"P"<<i<<":";
                for(j=0;j<r;j++)
                        cin>>alloc[i][j];
        }
        cout<<"\nEnter the request of each process\n";
        for(i=0;i<p;i++)
        {
                cout<<"P"<<i<<":";
                for(j=0;j<r;j++)
                        cin>>request[i][j];
        }
        cout<<"\nEnter the available resources of each type\n";
        for(i=0;i<r;i++)
                cin>>avail[i];
        for(i=0;i<r;i++)
                work[i]=avail[i];
}
void bankalg::safe_sequence()
{
        int i,j;
        safe=new int[p];
        flag=new int[p];
        for(i=0;i<p;i++)
        {
                flag[i]=0;

        }
        int l=0;
        for(int z=0;z<(p*p);z++)
        {
        for(int i=0;i<p;i++)
        {       if(flag[i]==0)
                {

                j=0;
                while(j<r)
                {       if(request[i][j]<=avail[j])
                        {
                                if(j==r-1)
                                {       flag[i]=1;
                                        for(int k=0;k<r;k++)
                                                avail[k]+=alloc[i][k];
                                        safe[l++]=i;
                                        cout<<"\n"<<i<<"\n";
                                } j++;

                        }
                        else break;
                }
                }
        }
        for(int i=0;i<p;i++)
        {
                if(flag[i]==1)
                {       if(i==r-1)
                        {
                                cout<<"\nThe safe sequence is:\n";
                                for(j=0;j<p;j++)
                                        cout<<"P"<<safe[j]<<" ";
                                return;
                       }
                }
                else break;
        }
        }
        cout<<"\nSystem is not in safe state\n";
}
void bankalg::request_resource()
{
        int *req=new int[r];
        int m;
        cout<<"Enter the the new request:\nProcess which is requesting:";
        cin>>m;
        cout<<"Request:";
        for(int i=0;i<r;i++)
        {
                cin>>req[i];
                request[m][i]+=req[i];
        }
        for(int i=0;i<r;i++)
                avail[i]=work[i];
        safe_sequence();

}
int main()
{
        int choice;
        bankalg ob;
        ob.read();
        ob.safe_sequence();
        cout<<"\nIf you want to enter a new request press 1. Else press 2.\n";
        cin>>choice;
        switch(choice)
        {
                case 1:ob.request_resource();
                        break;
                case 2:exit(1);
        }
}



//                    FIFO
#include<stdio.h>
main()
{
        int p[20],i,j=0,k,l=0,n,m,q[5],fault=0,z=0;
        printf("\nEnter num of pages:");
        scanf("%d",&n);
        printf("enter pages:");
        for(i=0;i<n;i++)
        scanf("%d",&p[i]);
        printf("\n enter num of frames:");
        scanf("%d",&m);
        for(i=0;i<m;i++)
          q[i]=-1;
        for(i=0;i<n;i++)
        {
          for(j=0;j<m;j++)
              if(p[i]==q[j])
                z++;
              if(z==0)
              {
                fault++;
                l=l%m;
                q[l]=p[i];
                l++;
                z=0;
                    for(k=0;k<m;k++)
                    printf("%d\t",q[k]);
                printf("\n");
             }
             z=0;
        }
        printf("Number of page faults:%d",fault);

}

//INPUT: 20 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 3
//INPUT2: 12 1 2 3 4 1 2 5 1 2 3 4 5 3


//                         LRU

#include<stdio.h>
main()
{
        int p[20],i,j=0,k,l=0,n,m,q[5],fault=0,z=0   ,x[3],max=0,c,b,a;
        printf("\n enter the num of pages:");
        scanf("%d",&n);
        printf("enter pages:");
        for(i=0;i<n;i++)
        scanf("%d",&p[i]);
        printf("enter no of frames:");
        scanf("%d",&m);
        for(i=0;i<m;i++)
                q[i]=-1;
        for(i=0;i<n;i++)
        {
                for(j=0;j<m;j++)
                    if(p[i]==q[j])
                        z++;
                if(z==0)
                {
                        fault++;
                        if(i>m-1)
                        {
                                for(a=0;a<m;a++)
                                        x[a]=0;
                                for(c=0;c<m;c++)
                                        for(b=i;b>=0;b--)
                                        {
                                                if(q[c]!=p[b])
                                                         x[c]++;
                                                else
                                                        break;
                                        }
                                max=x[0];l=0;
                                if(x[1]>max){ max=x[1];l=1;}
                                if(x[2]>max){ max=x[2];l=2;}
                        }
                        q[l]=p[i];
                        l++;
                        z=0;
                        for(k=0;k<m;k++)
                                printf("%d\t",q[k]);
                        printf("\n");
                }
                z=0;
        }
        printf("no. of page faults:%d",fault);
}





// PAGING            paging.c


#include<math.h>
#include<stdio.h>
int split(int *c,int n)
{
 int a,b,i,k=0,sum=0;
 if(n==1)
 {
   a=0;b=1;
 }
 else
 {
  a=2;b=3;
 }
 for(i=b;i>=a;i--)
 {
  sum=sum+(c[i]*pow(2,k));
  k++;
 }
 return(sum);
}
void binary(int sum,int *c)
{
 int d,i=3,n;
 n=sum;
 while(n>0)
 {
   d=n%2;
   c[i]=d;
   n=n/2;
   i--;
 }
}
int  main()
 {
  int a[10],k,i,j,n,temp1,temp2,b[10],c[4],d[32];

  for(i=0;i<16;i++)
    a[i]=i+65;
  for(i=0;i<32;i++)
    d[i]=32;
   printf("\n\n how many values do u want to enter in page table:");
  scanf("%d",&n);
  printf("\n\n enter the page table\n\n");
  for(i=0;i<n;i++)
   scanf("%d",&b[i]);
  for(i=0;i<16;i++)
  {
    for(j=0;j<4;j++)
    c[j]=0;
    binary(i,&c[0]);
    temp1=split(&c[0],1);
    temp2=split(&c[0],3);
    n=(b[temp1]*4)+temp2;
    d[n]=i+65;
  }
  printf("\n\n\n\t\tpage table\n\n");
  for(i=0;i<32;i++)
  {
   printf("\n address %d \t value %c",i,d[i]);

 }
}





//  OPTIMAL        optimal.c
#include<stdio.h>
main()
{
        int p[20],i,j=0,k,l=0,n,m,q[5],fault=0,z=0,x[3],max=0,c,b,a;
        printf("\n enter the num of pages:");
        scanf("%d",&n);
        printf("enter pages:");
        for(i=0;i<n;i++)
                scanf("%d",&p[i]);
        printf("enter no of frames:");
        scanf("%d",&m);
        for(i=0;i<m;i++)
               q[i]=-1;
        for(i=0;i<n;i++)
        {
                for(j=0;j<m;j++)
                        if(p[i]==q[j])
                                z++;
                if(z==0)
                {
                        fault++;
                        if(i>m-1)
                        {
                                for(a=0;a<m;a++)
                                x[a]=0;
                                for(c=0;c<m;c++)
                                        for(b=i;b<n;b++)
                                        {
                                                if(q[c]!=p[b])
                                                x[c]++;
                                                else
                                                break;
                                        }
                                max=x[0];l=0;
                                if(x[1]>max){ max=x[1];l=1;}
                                if(x[2]>max){ max=x[2];l=2;}
                        }
                                        q[l]=p[i];
                                        l++;
                                        z=0;
                                        for(k=0;k<m;k++)
                                                printf("%d\t",q[k]);
                        printf("\n");
                }
                z=0;
        }
        printf("No. of page faults:%d",fault);
}







//                          MFT
 #include<stdio.h>
main()
{
        int ms,choice,size,nb,i,j,frag;
        static int sz[20][2],nj,s;
        int count=0;
        static int jobs[20][3];
        printf("enter memory space:\n");
        scanf("%d",&ms);
        printf("enter your choice:");
        printf("\n enter \n 1.equal partition \n 2.unequal partition");
        scanf("%d",&choice);
        if(choice==1)
        {
                printf("enter size of each block:");
                scanf("%d",&s);
                nb=ms/s;
                for(i=0;i<nb;i++)
                        sz[i][0]=s;
                printf("total no. of blocks: %d\n",nb);
        }
        else
        {
        printf("enter no. of blocks:\n");
        scanf("%d",&nb);
        for(i=0;i<nb;i++)
          {
                printf("enter size of %d block:\n",i);
                scanf("%d",&sz[i][0]);
          }
        }
        printf("enter no. of jobs:\n");
        scanf("%d",&nj);
        printf("enter job info \n 1.job id \n 2.job size\n");
        for(i=0;i<nj;i++)
                scanf("%d%d",&jobs[i][0],&jobs[i][1]);
        frag=0;
        for(i=0;i<nj;i++)
        {
                        if(jobs[i][1]<sz[i][0])
                                {
                                jobs[i][2]=2;
                                frag+=sz[i][0]-jobs[i][1];
                                count+=1;
                                }
        }
        printf("total internal fragmentation is %d\n",frag);
        printf("no. of free blocks:%d\n",nb-count);
        printf("the jobs which are not allocated:");
        if(count==nj)
                printf("0");
        for(i=0;i<nj;i++)
        {
                if(jobs[i][2]!=2)
                        printf("\n job id-> %d \n job size-> %d",jobs[i][0],jobs[i][1]);
        }
}







// MVT  mvt.c


#include<stdio.h>
main()
{
        static int jobs[20][2],flag[10],i,k,nj,nb=0,tms,ch;
        printf("Enter no. of jobs:");
        scanf("%d",&nj);
        printf("Enter total memory space:");
        scanf("%d",&tms);
        printf("Enter job info:\n1.Job id\n2.Job size\n");
        for(i=0;i<nj;i++)
                scanf("%d%d",&jobs[i][0],&jobs[i][1]);
        for(i=0;i<nj;i++)
        {
                if(tms>=jobs[i][1])
                {
                        nb=nb+1;
                        tms=tms-jobs[i][1];
                        flag[i]=1;
                }
        }
        printf("Free space after allocation is %d",tms);
        printf("\nThe allocated jobs are:\n");
        for(i=0;i<nj;i++)
                if(flag[i]==1)
                        printf("%d->%d\n",jobs[i][0],jobs[i][1]);
        printf("\nJobs which are not allocated are:\n");
        for(i=0;i<nj;i++)
                if(flag[i]==0)
                        printf("%d->%d\n",jobs[i][0],jobs[i][1]);
        if(nb!=nj)
        {
                while(1)
                {
                        printf("Enter the job id to deallocate:\n");
                        scanf("%d",&k);
                        for(i=0;i<nj;i++)
                                if(jobs[i][0]==k)
                                        if(flag[i]==1)
                                        {
                                                tms=tms+jobs[i][1];
                                                flag[i]=2;
                                                printf("The deallocated job is \n%d->%d",jobs[i][0],jobs[i][1]);
                                        }
                        for(i=0;i<nj;i++)
                                if(tms>=jobs[i][1])
                  if(flag[i]==0)
                                        {
                                                tms=tms-jobs[i][1];
                                                flag[i]=1;
                                        }

                        printf("\nThe allocated jobs are:\n");
                        for(i=0;i<nj;i++)
                                if(flag[i]==1)
                                        printf("%d->%d\n",jobs[i][0],jobs[i][1]);
                        printf("Jobs which are not allocated are:");
                        for(i=0;i<nj;i++)
                                if(flag[i]==0||flag[i]==2)
                                        printf("%d->%d\n",jobs[i][0],jobs[i][1]);
                         printf("\nFree space after allocating is %d",tms);
   printf("\nDo you want to deallocate\n1.Yes\n2.No\n");
                        scanf("%d",&ch);
                        if(ch==2)
                                break;
                }
        }
        printf("Allocated jobs are:\n");
        for(i=0;i<nj;i++)
                if(flag[i]==1)
                        printf("%d->%d\n",jobs[i][0],jobs[i][1]);
}






No comments:

Post a Comment