1.)System consists of five process (p1, p2, p3, p4, p5) and three resources (r1, r2 ,r3). Resources type r1 has 10 instances,resources type r2 has 5 instances and r3 has 7 instances. The following snapshot of the system has been taken:




Content of the matrix need is calculated as need = max - allocation



Safe sequence: safe sequence is calculated as follows:

1)Need of each process is compared with available.If need < available, then the resources are allocated to that process and process will release the resources.

2) If Need is greater than available, next process need is taken for comparison.

3) If the above example,need of process p1 is (7,4,3) and available        is (3,3,2).    
                 need >= available
 so system will move for next process.


4)Need for process p2 is (1,1,2) and available (3,3,2),so

       need<= available(work)
(1,2,2) <= (3,3,2) =true

then finish[i] =true

request of p2 is granted and process pr is release the resources to the system.

work  := work+Allocation
work := (3,3,2)+(2,0,0)
          =(5,3,2)
This procedure is continued for all procedure.


5)Need for process p2 is (6,0,0) and available (5,3,2),so

       need<= available(work)
(6,0,0)<= (5,3,2) =false


6)process p4 need (0,1,1) is compared with available(5,3,2)

       need<= available
(0,1,1) <= (5,3,2) =true

then finish[i] =true
Available = Available +Allocation
                 =(5,3,2)+(2,1,1)
                 =(7,4,3) 


7)process p5 need (4,3,1) is compared with available(7,4,3)

       need< available
(4,3,1) < (7,4,3) =true

then finish[i] =true
Available = Available +Allocation
                 =(7,4,3)+(0,2,2)
                 =(7,4,5) 


8) One cycle is completed.again system takes all remaining process in sequence.so,process p1 need (7,4,3) is compared with new available (7,4,5).

need < available =true
(7,4,3) < (7,4,5)

 available = available + allocation
                =(7,4,5)+(0,1,0)=(7,5,5)


9)process p3 need (6,0,0) is compared with available(7,5,5)
need < available =true
 (6,0,0) < (7,5,5)

 available = available + allocation
                =(7,5,5)+(3,0,2)=(10,5,7)



SAFE SEQUENCE IS <p2,p4,p5,p1,p3>

Banker's algorithm



banker's algorithm is the deadlock avoidance algorithm. Banker's is named because the algorithm is  modeled after a banker who makes loads from a pool of capital and receives payments that are returned to that pool.

Algorithm is check to see if granting the request leads to an unsafe state.If it does,the request is denied.If granting the request leads to a safe state,it is carried out.

The Dijkstra proposed an algorithm to regulate resources allocation to avoid deadlocks.The banker's algorithm is the best known of the avoidance method.
By using avoidance method,the system is always kept in a safe state.
It is easy to check if a deadlock is created by granting a request,deadlock analysis method is used for this.

Deadlock avoidance uses the worst-case analysis method to check for future deadlock.


Safe state: 

There is at least one sequence of resources allocation to process that does not result in a deadlock.

System is in a safe state only if there exist a safe sequence .A safe state is not a deadlock state.
Deadlock state is an unsafe state.It does mean the system is in a deadlock.
As long as the state is safe,the resources manager can be guaranteed to avoid a deadlock.
If system remains in safe state after allocating resource then only OS allocates resources to process.


1.Allocation: Allocation is a table in which row represent process and column represents resources(R).

2.Max : max be the maximum number of resources that process requires during its execution.
  
   Need:It is current claim of a process,where a process's claim is                   equal to its maximum need minus its current allocation.
   Need= Max-Allocation  
  Available:Number of resources-sum of the allocation to all                              process


Weakness of Banker's algorithm

1) It requires that there be a fixed number of resources to allocate.
2) The algorithm requires that users state their maximum needs in advance.
3)Number of users must remain fixed.
4)The algorithm requires that the bankers grant all request within a finite time.
5)Algorithm requires that process returns all resources within a finite time.





what is unary operator?


unary operator = need for single operand like(++,--,etc.)


we know that this operator changes the sign of an operand when applied to a basic data item.

we will see how to overload this operator so that it can be applied to an object in much the same way as is applied to an int or float variable.

The unary minus when applied to an object should change the sign of each of its data items.


  • take one example using member function.


EXAMPLE:

#include<iostream>
using namespace std;
class ABC
{
int a,b,c;                              //data member
public:
void getdata(int x,int y,int z);     
void operator -();                   //overload - operator and no argument
void display();
};
void ABC :: getdata(int x,int y,int z)
{
a=x;                         //initialize with a,b,c
b=y;
c=z;
}
void ABC :: operator -()
{
a=-a;
b=-b;
c=-c;
}
void ABC :: display()
{
cout<<"\n a is:"<<a;
cout<<"\n b is:"<<b;
cout<<"\n c is:"<<c;
}
int main()
{
ABC t;
t.getdata(10,-20,-30);
t.display();
-t; 
t.display();
return 0;                        //either -t or t-

}



output:



Note

The function operator -() takes no argument.Then,what does this operator function do?it changes the sign of data members of the object t.Since this function is a member function of the same class,it can directly access the member of the object which activated it.

Remember,a statement like

t2=-t1;
will not work because,the operator-() does not return value.it can work if the function is modified to return an object.

it is possible to overload a unary minus operator using a friend function as follows:


Note

Note that the argument is passed by reference.it will not work if we pass argument by value because only a copy of the object that activated the call is passed to operator-().Therefore,the changes made insides the operator function will not reflect in the called object.
It is not uncommon that a class is derived from another derived  class as shown below figure.The class A serves as a base class for the derived class B,which in turn serves as a base class for the derived class C.
The class B is known as intermediate base class since it provides a link for the inheritance between A and C.The chain ABC is known as inheritance path.

A derived class with multilevel inheritance is declared follows:

class A{..........}                                     // Base class
class B : public A{........};                    //  B derived from A
class C : public B{.........};                   // C derived from B


This process can be extended to any number of level.

let us consider a simple example.Assume that the test results of a batch of students are stored in 3 different classes.class student stores the roll number,class test stores mark obtain in 2 subject and class result contains the total marks obtain in test.

The class result can inherit the details of the marks obtained in the test and the roll number of student through multilevel inheritance,

Example:

#include<iostream>
using namespace std;
class student
{
protected:
int rollnumber;
public:
void getdata(int);
    void put_number(void);     
};
void student :: getdata(int a)
{
rollnumber=a;
}
void student :: put_number()
{
cout<<"roll number is"<<rollnumber<<"\n";
}
class test : public student
{
protected:
float sub1;
float sub2;
public:
void get_marks(float x,float y);
void put_marks(void);
};
void test :: get_marks(float x,float y)
{
sub1=x;
sub2=y;
}
void test :: put_marks()
{
cout<<"\nmarks in sub1"<<sub1<<"\n";
cout<<"\nmarks in sub2"<<sub2<<"\n";
}
class result : public test
{
private:
float total;
public:
void display(void);
};
void result :: display(void)
{
total=sub1+sub2;
put_number();
put_marks();
cout<<"\n total="<<total<<"\n";
}
int main()
{
result student1;
student1.getdata(111);
student1.get_marks(75.0,59.5);
student1.display();
return 0;

}

Output: 


To define an additional task to an operator,we must specify what it means in relation to the class to which the operator is applied.This is done with the help of a special function,called operator function,which describe the task.The general form of an operator function is:

where return type is the type of value returned by the specified operation and op is the operator being overloaded.operator op is the function name,where operator is a keyword.

operator function must be either member functions(non static) or friend functions.A basic difference between them is that a friend function will have only one argument for unary operators and two for binary operators,while a member function has no argument for unary operators and only one for binary operators.

This is because the object used to invoke the member function is passes implicity and therefore is available for the member function.This is not the case with friend functions. Argument may be passed either by value or by reference .operator functions are declared in the class using prototypes as follows:

vector is a data type of class and may represent both magnitude and direction (as in physics and engineering)or a series of points called elements (as in mathematics).

šŸŽ‡The process of overloading involves the following steps:

1.create a class that define the data type that is to be used in the           overloading operation.

2.Declare the operation function operator op() in the public part of     the class.it may be either a member function or a friend function.

3.Define the operator function to implement the required                     operations.

overloaded operator functions can be invoked by expressions such as

op x or x op

for unary operators and

 x op y

 for binary operators.op x(or x op)would be interpreted as

operator op(x)

for friend functions.similarly,the expression x op y would be interpreted as either

x.operator op (y)

in case of member functions,or

operator op(x,y)

in case of friend function. when both the forms are declared,standard argument matching is applied to resolve any ambiguity.



different type of operator overloading:

1)  unary operator       click and go
As we know,the constructor play in important role in initializing object.We did not use them earlier in the derived classes for the sake of simplicity.

one important thing to note here is that, as long as no base class constructor takes any arguments,the derived class need not have a constructor function.However,if any base class contains a constructor with one or  more argument,then it is mandatory for the derived class to have a constructor and pass the argument to the base class constructor.

Remember,while applying inheritance we usually create objects using the derived class.Thus,it makes sense for the derived class to pass arguments to the base class constructor.

when both the derived and base classes contain constructors,the base constructor is executed first,and then the constructor in the derived class is executed. 

In case of multiple inheritance,the base classes are constructed in the order in which they appear in the declaration of the derived class.similarly,in a multilevel inheritance,the constructor will be executed in the order inheritance.

since,the derived class takes the responsibility of supplying initial values to its base classes.we supply the initial values that are required by the classes together,when a derived class object is declared.How are they passed to the base class constructor so that they can do their job? c++ supports a special argument passing mechanism for such situations.

The constructor of the derived class receives the entire list of values as its arguments and passes them on to the base constructors in the order in which they are declared in the derived class.The base constructors are called and executed before executing the statements in the body of the derived constructor.

The general form of defining a derived constructor is:





The header line of derived-constructor function contains two parts separated by a colon(:).The first part provides the declaration of the arguments that are passed to the derived-constructor and the second part lists the function calls to the base constructors.

base1(arglist1),base2(arglist2)...are function calls to base constructor base1(),base2(),....and therefore arglist1,arglist2..etc.
represent the actual parameters that are passed to the base constructor. arglist1 through arglistN are the argument declarations for base constructor base1 through baseN. arglistD provides the parameters that are necessary to initialize the member of the derived class.




EXAMPLE:

D(int a1, int a2, float b1 ,float b2, int d1);
A(a1, a2);     /* call to constructor A */
B(b1, b2);    /*call to constructor B */
{
     d=d1;       // executes its own body
}

A(a1,a2) invokes the base constructor A() and B(b1,b2) invokes another base constructor B(). The constructor D() supplies the values for these four argument.In addition,it has one argument of its own.The constructor D() has a total of five argument.D() may be invoked as follows:

.......
D objD(5,12,2.5,7.54,30);
.......

These values are assigned to various parameters by the constructor D() follows:

5 =a1
12=a2
2.5=a3
7.54=a4
30=d1






program with constructor in derived class:

#include<iostream>
using namespace std;
class A
{
private:
int x;
public:
A(int a)
{
x=a;
cout<<"A initialize \n";
}
void show1(void)
{
cout<<"x="<<x<<"\n";
}
};
class B
{
private:
float y;
public:
B(float b)
{
y=b;
cout<<"B initialize \n";
}
void show2(void)
{
cout<<"y="<<y<<"\n";
}
};
class C : public A,public B
{
private:
int m,n;
public:
C(int r,float s,int t,int u) : A(r),B(s)
{
m=r;
n=s;
cout<<"C initialize \n";
}
void show3(void)
{
cout<<"m="<<m<<"\n";
cout<<"n="<<n<<"\n";
}
};
int main()
{
C c(5,10.75,20,30);
cout<<"\n";
c.show1();
c.show2();
c.show3();
return 0;

}

output:


Let us consider a simple example to illustrate inheritance.shows a base class B and a derived class D.The class B contains one private data member,one public data member,and three public member functions.The class D contains one private data member and two public member functions.

coding:

#include<iostream>
using namespace std;
class B
{
int a;  //private:not in heritable
public:
int b;
void set_ab();
int getdata();
void show(void);
};
class D : public B
{
int c;
public:
void mul(void);
void display(void);
};

//---------------------

void B :: set_ab(void)
{
a=5;b=10;
}
int B :: getdata()
{
return a;
}
void B :: show()
{
cout<<"a:\n"<<a;
}
void D :: mul()
{
c=b*getdata();
}
void D :: display()
{
cout<<"a="<<getdata()<<"\n";
cout<<"b="<<b<<"\n";
cout<<"c="<<c<<"\n\n";
}
int main()
{
D d;
d.set_ab();
d.mul();
d.show();
d.display();
d.b=20;
d.mul();
d.display();
return 0;
}

outpost:

The class D is a public derivation of the base class B.Therefore,D inherits all the public members of B and retains their visibility.Thus,a public member of the base class B is also a public member of the derived class D.The private member of B cannot be inherited by D.The class D,in effect,will have more members than what it contains at the time of declaration as shown figure.

The program illustrates that the object of class D have access to all the public member of B .Let us have a look at the functions show() and mul():

void show()
{
  cout<<"a="<<a<<"\n";
}

void mul()
{
   c=b*getdata();
}

 Although the data member a is private in B and cannot be inherited,object of D are able to access it through an inherited member function of B.

class B
{
int a;  //private:not in heritable
public:
int b;
void get_ab();
int getdata();
void show(void);
};
class D : public B
{
int c;
public:
void mul(void);
void display(void);
};

The membership of this derived class D is shown figure.In private derivation.the public members of the basic class become private members of the derived class.Therefore,the object of D cannot have direct access to the public member functions of B.

          
                        
                                             Adding more member to a class(by private derivation)

d.set_ab();
d.getdata();
d.show();

will not work.However,these functions can be used inside mul() and display() like the normal functions as shown below:

void  mul()
{
        set_ab();
c=b*getdata();
}
void display()
{
        show();                //output value of `a`
cout<<"b="<<b<<"\n";
cout<<"c="<<c<<"\n\n";
}
Copyright © 2013 free coding