Showing posts with label Banker’s algorithm. best fit algorithm. Show all posts
Showing posts with label Banker’s algorithm. best fit algorithm. Show all posts

Monday, August 13, 2012

operating system program b.tech.


EXPERIMENT 1
AIM: Write a program for FCFS(first come first serve)

TABLE:


PROCESS

BURST TIME
WAITING TIME
TURN AROUND TIME
P1
24
0
24
P2
3
24
24
P3
3
27
30




GRANTT CHART:
P1
P2
P3
0                                                              24                                                           27                                           30






PROGRAM:
#include<iostream.h>
#include<conio.h>
class fcfs
{
  int b_time[10];
  int w_time[10];
  int n_jobs[10];
  float avg;
  public:
  fcfs()
  {
  n_jobs=0;
  avg=0;
  }
  void job_entry(int n)
  {
  n_jobs=n;
  cout<<"\n enter the burst time of each job:";
  cout<<"\n";
  for(int i=0;i<n_jobs;i++)
  {
  cout<<"\n job"<<i+1<<":";
  cin>>b_time[i];
  }
  }
  void wait_time(void)
  {
  for(int i=0;i<n_jobs;i++)
  {
  id_time[i]=0;
  for(int j=0;j<1;j++)
  {
  w_time[i]=w_time[i]+t_time[j];
  }
  avg=avg+w_time[i];
  }
  avg=avg/n_jobs;
  cout<<"\n\t grant chart";
  for(i=0;i<n_jobs;i++)
  {
  cout<<b_time<<" ";
  cout<<"\n waiting time \t";
  for(i=0;i<n_jobs;i++)
  {
  cout<<w_time[i]<<" ";
  cout<<"\n\n avg wait time is: "<<avg<<"mfs":
  }
  };
  int main()
  {
  fcfs f;
  int n;
  cout<<"\n\n\t fcfs";
  cout<<"how many job you entered";
  cin>>n;
  f.job_entry(n);
  f.waist_time();
  getch();
  }


































INPUT/OUTPUT:


Fcfs show how many job you can enter: 3
Enter the burst time of each the job:
Job 1: 20
Job2: 25
Job3: 30
Group t chart-4096::-30745

Avg wait time is: -1365.333374mfs-28807
Avg wait time is:-1365.333374mfs925
Avg wait time is:-1365.333347mfs























RESULT:   Thus the program for FCFS has been successfully executed.










EXPERIMENT  02


AIM: To write a program for shortest job fist(SJF)




PROGRAM:
#include<stdio.h>
#include<iostream.h>
#include<conio.h>

  class sjf
  {
  int b_time[10];
  int w_time[10];
  int n_jobs;
  float avg;
  public:
  sjf()
  {
  int n_jobs=0;
  avg=0;
  }
  void job_entry(int n)
  {
  n_jobs=n;
  cout<<"enter the burst time for each job";
  cout<<"\n";
  for(int i=0;i<n_jobs;i++)
  {
  cout<<"\n jobs"<<i+1<<":";
  cin>>b_time[i];               
  }
  sorting(b_time,n);
  }
  void sorting(int b_time[],int n)
  {
  for(int i=0;i<n;i++)
  {
  for(int j=i+1;j<n;j++)
  {
  if(b_time[j]<b_time[i])
  {
  int temp=b_time[i];
  b_time[i]=b_time[j];
  b_time[j]=temp;
  }
  }
  }

  void wait_time(void)
  {
  for(int i=0;i<n_jobs;i++)
  {
  w_time[i]=0;
  for(int j=0;j<i;j++)
  {
  w_time[i]=w_time[i]+b_time[j]
  }
  avg=avg+w_time[i];
  }
  cout<<"\n\t\t grant chart";
  cout<<"\n burst time\t\t";
  for(i=0;i<n_jobs;i++)
  cout<<" "<<b_time[i]<<" ";
  cout<<"\n waiting time \t\t";
  for(i=0;i<n_jobs;i++)
  cout<<"\n\n the averag waiting time is: "<<avg<<"ms";
  }
  };
  int main()
  {
  sjf s;
  int n;
  clrscr();
  cout<<"\n\n\t shortest job first";
  cout<<"\n\n how many jobs going to be executed";
  cin>>n;
  s.job_entry();
  s.wait_time();
  getch();
  }
 












INPUT/OUTPUT:

Shortest job first
How many jobs you enter 3
Job1: 6
Job2: 4
Job3: 8



Grant chart
Burst time                                                6:4:8
Waiting time                                           0:4:10
The average waiting time is                 4.66666667ms


















RESULT:    Thus the program for SJF can be executed successfully.












EXPERIMENT 03

AIM: Write the program for priority scheduling




Table:
PROCESS
BURST TIME
PRIORITY

P1
10
3
P2
1
1
P3
2
4
P4
1
5
P5
5
2




PROCESS
BURST TIME
WAIT TIME
TURN AROUND TIME

P1
10
6
16
P2
1
0
1
P3
2
16
18
P4
1
18
19
P5
5
1
6








PROGRAM:

#include<iostream.h>
#include<conio.h>
class priority
{
public:
int p[25],ser[25],pri[25],pri1[25],wait[25],i,j,j,n;
double awt,fat,s;
void get()
{
cout<<"\n enter job:";
cin>>n;
for(i=0;i<n;i++)
{                   
cout<<"\n enter the burst time for process"; p["<<i+1<<"];
cin>>ser[i];
cout<<"\n enter the priority level"; p["<<i+1<<"];
cin>>pri[i];
pri[i]=pri[i];
p[i]=i;
}
}
void calculate()
{
int temp;
for(i=0;i<n;i++)
{
for(j=0;j<=(n-i-1);j++)
{
if(pri[j]>=pri[j+1])
{
temp=pri[j];
pri[j]=pri[j+1];
pri[j+1]=temp;
}
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(pri[i]==pri[j])
{
if(i==0)
{
wait[i]=0;
awt=0;
tat=ser[i];
temp=ser[j];
}
else
{
wait[i]=wait[i+1]+temp;
temp=ser[i];
awt+=wait[i];
tat+=ser[i]+wait;
}
}
}
awt=awt/n;
tat=tat/n;
}
void put()
{
wait[0]=0;
cout<<"\n\n scheduling order";
cout<<"\n PID"<<"\t priority"<<"\t service temp"<<"\t wait time";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(pri[i]==pri[j])
cout<<"\n"<<p[j]+1<<"\t"<<pri[i]<<"\t\t"<<ser[j]<<"\t\t"<<wait[i];
}
}
cout<<"\n\n average waiting time is: "<<awt<<"ms";
cout<<"\n average turn aound time is: "<<tat<<"ms";
}
};
void main()
{
priority p;
clrscr();
p.get();
p.calculate();
p.put();
getch();
}









INPUT/OUTPUT:

Enter the no. of jobs=3
Enter  the burst time for the process p[1]=6
Enter the priority of process p[1]=3
Enter the burst time for process p[2]=6
Enter the priority of process p[2]=1
Enter the burst time for the process p[3]=7
Enter the priority of process[3]=2

Scheduling order
PID                                    priority                            service time                                      wait time
2                                              1                                              6                                                              0
2                                         1                                          6                                                       6
3                                         2                                          7                                                    12


Average time: 8ms
Average time around time:  16.333333ms















RESULT:   Thus the program for the priority scheduling has been executed successfully.













EXPERIMENT -04

AIM:  Write a program for robin scheduling

TABLE:


PROCESS


BURST TIME
P1
24
P2
7
P3
3






GRANTT CHART:

P1
P2
P3
P1
P2
P1
P1
P1
P1
P1
0           4             8             11           15          18          22           26          30         34      38
PROGRAM

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class round
{
int wt,st[20],com[20],brust[20],executed[20];
int tq,n;
float avg;
            public:
            void getdata();
            void calculate();
};
            void round::getdata()
            {
            cout<<"\n ENTER THE NUMBER OF PROCESS:";
            cin>>n;
            cout<<"\n ENTER THE BURST TIME FOR EACH PROCESS:\n";
            for(int i=1;i<=n;i++)
                        {
                        cout<<"\n JOB"<<i<<":";
                        cin>>brust[i];
                        }
                        cout<<"\n ENTER THE TIME QUANTUM:";
                        cin>>tq;
            }
            void round::calculate()
            {
            int flag,i;
            avg=0,wt=0;
            for(i=1;i<=n;i++)
            executed[i]=0;
            flag=1;
            cout<<"\n PROCESS  \tSTARTING TIME \tCOMPLETION TIME";
            while(flag==1)
            {
            flag=0;
            for(i=1;i<=n;i++)
                        {
                        if(brust[i]>tq)
                        {
                        st[i]=wt;
                        if(executed[i]==1)
                        avg=avg+st[i]-com[i];
                        else
                        avg=avg+st[i];
                        wt=wt+tq;
                        com[i]=wt;
                        brust[i]=brust[i]-tq;
                        flag=1;
                        executed[i]=1;
                        cout<<"\n PROCESS"<<":"<<i<<"\t"<<st[i]<<"\t\t\t"<<com[i];
                        }
                        else if(brust[i]>0)
                                    {
                                    st[i]=wt;
                                    if(executed[i]==1)
                                    avg=avg+st[i]-com[i];
                                    else
                                    avg=avg+st[i];
                                    wt=wt+brust[i];
                                    com[i]=wt;
                                    cout<<"\n PROCESS"<<":"<<i<<"\t"<<st[i]<<"\t\t\t"<<com[i]<<"completed";
                                    brust[i]=0;
                                    executed[i]=1;
                                    }
                        }
            }
            avg=avg/n;
            cout<<"\n\n THE AVERAGE WAITING TIME IS:"<<avg<<"\ms";
            }
            void main()
            {
            clrscr();
            round r;
            cout<<"\n\n\t\t\t ROUND ROBIN SCHEDULING \t\t\t:";
            r.getdata();
            r.calculate();
            getch();
            }




INPUT:

Enter the no of jobs=3
Enter the burst time for process p[1]=3
Enter the priority process p[1]: 3
Enter the burst time for  process p[2]=8
Enter the priority process p[2]:1
Enter the burst time for the process p[3]:6
Enter the priority process p[3]:2
                                                       

OUTPUT:

SCHEDULING
PID
PRIORITY
SERVICE TIME
WAIT TIME
2
1
8
0
3
2
6
8
1
3
10
16

Average waiting time: 8ms
Average turnaround time: 12.66667 ms







RESULT:  Thus the program for robin scheduling has been executed successfully

EXPERIMENT-05

Aim:               To write a C++ program for implementing Banker’s algorithm.







TABLE:
                                                                                                                                                Available=3

PROCESS

MAX NEED
ALLOCATED
NEED
P0
10
5
5
P1
4
2
2
P2
9
2
7


















Program:

#include<iostream.h>
#include<conio.h>

class BankerAlgo
{
            private:
                        int allocated[50][50],max[50][50],available[50];
                        int need[50][50];
                        int flag[50];
                        int m,n;
                        int i,j;

            public:

            BankerAlgo()
            {
                         cout<<"\n\a Enter the Row for Allocation and Maximum Matrix , Please !! : ";
                         cin>>m;
                         cout<<"\n\a Enter the Column for Allocation and Maximum Matrix , Please : ";
                         cin>>n;

                            /* Input the Allocation and Maximum matrix by the user */

                         cout<<"\n\a Allocation Matrix ("<<m<<" x "<<n<<")\n";
                         for(i = 0;i < m;i++)
                         for(j = 0;j < n;j++)
                                    cin>>allocated[i][j];

                         cout<<"\n\a Maximum Matrix ("<<m<<" x "<<n<<")\n";
                         for(i = 0;i < m;i++)
                         for(j = 0;j < n;j++)
                                    cin>>max[i][j];

                         cout<<"\n\a Available\n";
                         for(i = 0;i < n;i++)
                         {
                                    cin>>available[i];
                         }

            }

            void MakeNull()
            {
                        for(i = 0;i < 50;i++)
                                    flag[i] = 0;
            }



                        /* Subtraction - Allocating the resource (C-A) */



            void Need()
            {
                        cout<<"\nNeed Matrix ["<<m<<" X "<<n<<"]\n";
                        for(i = 0;i < m;i++)
                        {
                                    for(j = 0;j < n;j++)
                                    {
                                         need[i][j] = max[i][j] - allocated[i][j];
                                         cout<<need[i][j]<<"\t";
                                    }
                                    cout<<"\n";
                        }
            }

                                    /* Validating the Resource Allocation */
            int Check(int column)
            {
                        for(i = 0;i < n;i++)
                                    if(need[column][i] > available[i])
                                                return 0;
                                                return 1;
            }


                          /* Adding to the Available Matrix */


            void AddResource(int column)
            {
                        for(int i = 0;i < n;i++)
                                    available[i] = available[i] + allocated[column][i];
            }

            void Process()
            {
                        int t,k,l;
                        int temp = 0;
                        MakeNull();

                        cout<<"\nSafety Sequence :";
                        for(k = 0;k < 3;k++)
                        {
                                    temp = 0;
                                    for(l = 0;l < m;l++)
                                    {
                                                if(flag[l] == 0)
                                                {
                                                            temp = 1;
                                                            if(Check(l) == 1)
                                                            {
                                                                        AddResource(l);
                                                                        flag[l] = 1;
                                                                        cout<<" P"<<l;
                                                            }
                                                }
                                    }
                        }
                        if(temp == 0)
                                    cout<<"\n\nSystem in Safe State";
                        else
                                    cout<<"\n\nSystem in UnSafe State";
            }
};

int main()
{
            clrscr();
            BankerAlgo B;
            B.Need();
            B.Process();
            getch();

}



INPUT/OUPUT


 Enter the Row for Allocation and Maximum Matrix , Please !! : 2

 Enter the Column for Allocation and Maximum Matrix , Please : 2

 Allocation Matrix (2 x 2)
1
2
3
4

 Maximum Matrix (2 x 2)
5
6
7
8

 Available
5
4

Need Matrix [2 X 2]
4       4
4       4

Safety Sequence : P0 P1

System in Safe State










RESULT:  Thus the program for bankers algorithm has been successfully executed.


EXPERIMENT NO. 6

AIM:  Write a program for Best Fit Algorithm



TABLE:

           

                                                                                                          











6KB





14KB




19KB



11KB




10KB














PROGRAM:

#include<iostream.h>
#include<conio.h>
void main()
{
int m[100],f,i,j,n,flag;
clrscr();
cout<<"enter the size of the array";
cin>>n;
for(i=0;i<n;i++)
{
cin>>m[i];
}
cout<<"enter the size of process";
cin>>f;
for(j=0;j<n;j++)
{
if(m[j]==f)
{
cout<<"process is matched";
flag=0;
break;
else
flag=1;
}
if(flag==1)
cout<<"process not matched";
}
getch();
}













INPUT/OUTPUT:

Enter the size of the array 5
6
14
19
11
10

Enter the process 19
Process is matched
















RESULT:   Thus the program for best fit algorithm has been executed successfully.
















EXPERIMENT NO. 7

AIM: Write a program for paging algorithm




TABLE:

0
0
1
7
2
5
3
9
4
3






















PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
clrscr();
int page[5];
int s,i,j,a,frame[10];
randomize();
for(i=0;i<10;i++)
{
asd:
a=random(5);
for(j=1;j<i;j++)
{
if(a==frame[j])
goto asd;
}
if((i%2)==1)
{
frame[i]=a;
page[a]=i;
}
else
{
frame[i]=111;
}
}
for(i=0;i<5;i++)
cout<<i<<"\n";
cout<<"\n page table \n";
for(i=0;i<5;i++)
{
cout<<i<<"\t"<<page[i]<<"\n";
}
cout<<"\t post page \n";
for(s=0;s<10;s++)
cout<<s<<"\t";
cout<<frame[s]<<"\n";
}
getch();


INPUT/OUTPUT:



0                                            2
1                                            111
2                                            3
3                                            111
4                                            0
5                                            111
6                                            1
7                                            111
8                                            1
9                                            111

















RESULT:    Thus the program for paging has been successfully executed.
















EXPERIMENT  08
AIM:  Write a program for page replacement

PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int aa=0,frame[3],i,a[5],pt=0;
cout<<"enter the element of the string";
for(i=0;i<3;i++)
frame[i]=-1;
for(i=0;i<5;i++)
cin>>a[i];
for(i=0;i<5;i++)
{
if(pt==3)
{
pt=0;
}
if(a[i]==frame[pt])
{
frame[pt]=a[i];
}
frame[pt]=a[i];
aa++;
{
pt++;
}                                          
printf("\n page fault is %d",aa);
getch();
}


INPUT/OUTPUT:
Enter the elements in the string
1   2   3   2   1
Page fault is 3













RESULT:  Thus the program for page replacement has been successfully executed.











EXPERIMENT – 9

AIM:  Write a program for producer consumer problem


PROGRAM:

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class pcp
{
public:
  int count;
  int buffer[6];

pcp()
{
count=0;
}
~pcp()
{
count=0;
}
void insert();
void remove();
int full();
};
int pcp::full()
{
  if (count==5)
  {
    return(1);
  }
  else
  {
  return(0);
  }
}

void pcp::insert()
{
  if(full())
  {
  cout<<"no data can be inserted";
  }
  else
  {
  cout<<"enter data to be inserted";
  cin>>buffer[count];
  count++;
  }
  }

    void pcp::remove()
    {
    int a;
    if (full())
    {
    for(int i=0;i<5;i++)
    {
    a=buffer[count];
    cout<<"element remove is: "<<a<<endl;
    count--;
    }
    }
    else
    {
    cout<<"not pssible";
    }
    }
    void main()
    {
    int n;
    pcp z;
    clrscr();
    while(1)
    {
    cout<<"\n operation";
    cout<<"\n 1. add data";
    cout<<"\n 2. remove data";
    cout<<"\n 3. exit";
    cout<<"\n enter your choice";
    cin>>n;
    switch(n)
    {
      case 1:
      z.insert();
      break;
      case 2:
      z.remove();
      break;
      case 3:
      exit(1);

      cout<<"choice is invalid";
      }
    }
    getch();
    }

































INPUT/OUTPUT:


Operations
1.add
2.remove
3.exit

Enter your choice 1
Enter data to be inserted 2
Enter your choice 2
Not possible























RESULT:   Thus the program for producer consumer problem has been executed successfully