Chapter1to3(Bankers Algo)

download Chapter1to3(Bankers Algo)

of 25

Transcript of Chapter1to3(Bankers Algo)

  • 8/6/2019 Chapter1to3(Bankers Algo)

    1/24

    CHAPTER 1

    BANKERS ALGORITHM

    1.1 Introduction

    It is a resource allocation & deadlock avoidance algorithm developed by Edsger Dijkstra that

    tests for safety by simulating the allocation of pre-determined maximum possible amounts of all

    resources, and then makes a "safe-state" check to test for possible deadlock conditions for all

    other pending activities, before deciding whether allocation should be allowed to continue.

    The Banker's algorithm is run by the operating system whenever a process requests resources.

    The algorithm avoids deadlock by denying or postponing the request if it determines that

    accepting the request could put the system in an unsafe state (one where deadlock could occur).

    When a new process enters a system, it must declare the maximum number of instances of each

    resource type that may not exceed the total number of resources in the system. Also, when a

    process gets all its requested resources it must return them in a finite amount of time.

    The Banker's Algorithm derives its name from the fact that this algorithm could be used in a

    banking system to ensure that the bank does not run out of resources, because the bank would

    never allocate its money in such a way that it can no longer satisfy the needs of all its customers.

    By using the Banker's algorithm, the bank ensures that when customers request money the bank

    never leaves a safe state. If the customer's request does not cause the bank to leave a safe state,

    the cash will be allocated, otherwise the customer must wait until some other customer deposits

    enough.

    1.2Features of Bankers Algorithmy Multiple instances.y Each process must claim maximum use in advance.y When a process requests a resource it may have to wait.y When a process gets all its resources it must return them in a finite amount of time.

  • 8/6/2019 Chapter1to3(Bankers Algo)

    2/24

    1.3 Resources allocation criteriaFor the Banker's algorithm to work, it needs to know three things:

    y How much of each resource each process could possibly request.

    y How much of each resource each process is currently holding.y How much of each resource the system currently has available.

    Resources may be allocated to a process only if it satisfies the following conditions:

    y Request Max, else set error condition as process has crossed maximum claim madeby it.

    y Request Available, else process waits until resources are available.1.4Unsafe StatesA state is considered safe if it is possible for all processes to finish executing (terminate). Since

    the system cannot know when a process will terminate, or how many resources it will have

    requested by then, the system assumes that all processes will eventually attempt to acquire their

    stated maximum resources and terminate soon afterward. This is a reasonable assumption in

    most cases since the system is not particularly concerned with how long each process runs (at

    least not from a deadlock avoidance perspective). Also, if a process terminates without acquiring

    its maximum resources, it only makes it easier on the system.

    Given that assumption, the algorithm determines if a state is safe by trying to find a hypothetical

    set of requests by the processes that would allow each to acquire its maximum resources and then

    terminate (returning its resources to the system). Any state where no such set exists is an unsafe

    state.

  • 8/6/2019 Chapter1to3(Bankers Algo)

    3/24

    1.5 Requests

    When the system receives a request for resources, it runs the Banker's algorithm to determine if it

    is safe to grant the request. The algorithm is fairly straight forward once the distinction between

    safe and unsafe states is understood.

    y Can the request be granted?If not, the request is impossible and must either be denied or put on a waiting list

    y Assume that the request is grantedy Is the new state safe?

    If so grant the request

    If not, either deny the request or put it on a waiting list

    Whether the system denies or postpones an impossible or unsafe request is a decision specific to

    the operating system.

    1.6 Limitations

    Like other algorithms, the Banker's algorithm has some limitations when implemented.

    Specifically, it needs to know how much of each resource a process could possibly request. In

    most systems, this information is unavailable, making it impossible to implement the Banker's

    algorithm. Also, it is unrealistic to assume that the number of processes is static since in most

    systems the number of processes varies dynamically. Moreover, the requirement that a process

    will eventually release all its resources (when the process terminates) is sufficient for the

    correctness of the algorithm, however it is not sufficient for a practical system. Waiting for hours

    (or even days) for resources to be released is usually not acceptable.

  • 8/6/2019 Chapter1to3(Bankers Algo)

    4/24

    CHAPTER 2

    BANKERS RELATED ALGORITHMS

    2.1 Algorithms

    Two algorithms need to be discussed:

    y Safety state check algorithmy Resource request algorithm

    2.2 Safety State Check Algorithm

    Safety Algorithm: will try to find a safe sequence. Simulate evolution of system over time under

    worst case assumptions of resource demands.

    1. Work = Avail;Finish[i] = False for all i;

    2. Find i such that Finish[i] = False and Need[i]

  • 8/6/2019 Chapter1to3(Bankers Algo)

    5/24

  • 8/6/2019 Chapter1to3(Bankers Algo)

    6/24

    CHAPTER 4

    SCHEDULING ALGORITHMS

    4.1 First Come, First Served Scheduling.y This is the simplest scheduling algorithm.y The process that request the cpu first is allocated the cpu first.y The implementation is easily managed with FIFO queue.y When a process enters the ready queue, it is placed at the tail of the queue.y When the cpu is free, it is allocated the process at the head of the queue.y The average waiting time under this policy is often quite long.y The scheduling is diagrammatically shown by Gantt Chart.y FCFS suffers from CONVOY effect in which many small processes wait for a long

    process to finish.

    4.2 Shortest Job First Scheduling.y This algorithm associates with each process, the length of processs next cpu burst.y When the cpu is free, it is assigned to the process that has the smallest next cpu burst.y If the next cpu burst of two processes is same then FCFS is used to break the tie.y This is actually known as shortest next cpu burst algorithm.y The SJF scheduling algorithm is provably optimal as it gives the minimum waiting time

    for a given set of processes.

    y The real difficulty with this algorithm is knowing the length of next cpu burst.y SJF is used frequently in long term scheduling.y SJF algorithm can be preemptive or non preemptive.y A preemptive algorithm will terminate the currently executing process whenever a

    process with less next cpu burst comes to the queue.

    y A non preemptive algorithm will continue executing the current process.

  • 8/6/2019 Chapter1to3(Bankers Algo)

    7/24

    y This scheduling is sometimes called shortest remaining time first scheduling.

    4.3 Priority Schedulingy It is a general case of SJF algorithm.y In this , a priority is associated with each process and the cpu is allocated to the highest

    priority process.

    y Equal process are scheduled in the FCFS order.y Priorities are generally indicated by integer numbers like 0, 1 etc.y The lower the number , the higher is the priority.y A major problem with this algorithm is indefinite blocking or STARVATION.y

    A priority algorithm can leave some process to wait indefinitely for cpu as a high priorityprocess can take over a low priority process.

    y AGING is employed to solve starvation problem,, I which the priority of a process isincreased each time the process is blocked so at a particular time that process will run at

    the highest priority.

    4.4 Round Robin Schedulling.y

    This algorithm is designed especially for time sharing system.y It is similar to FCFS algorithm but preemption is added to switch between the process.y A small unit of time i.e TIME QUANTUM is defined.y A time quantum is generally 10 to 100 ms.y The ready queue is treated as circular queue.y The cpu scheduler goes around the ready queue, allocating the cpu to each process for

    atime interval of up to 1 time quantum.

    y To implement RR scheduling, the ready queue is kept as FIFO queue of processes.y New processes are added to the tail of the queue.y The cpu scheduler picks the first process from the ready queue, sets a timer to interrupt

    after 1 time quantum and diapatches the process.

    y In case the process have cpu burst of less than one time quanrum then the process willrelease the cpu voluntarily.

  • 8/6/2019 Chapter1to3(Bankers Algo)

    8/24

    y The scheduler will then process to the next process in the ready queue.y If the processs cpu burst is more than one time quantum then it is terminated after one

    time quantum and is placed at the tail of the queue.

    y The performance of RR algorithm heavily depends on the size of time quantumy If time quantum is quite large then the policy is same as FCFS, and if the time quantum is

    very short then the RR approach is called processor sharing and it creates the impression

    that each of the n processes has its own processor running at 1/n speed of the real

    processor.

  • 8/6/2019 Chapter1to3(Bankers Algo)

    9/24

  • 8/6/2019 Chapter1to3(Bankers Algo)

    10/24

    5.2The Optimal Page Replacement Algorithm

    The best possible page replacement algorithm is easy to describe but impossible to implement. It

    goes like this. At the moment that a page fault occurs, some set of pages is in memory. One of

    these pages will be referenced on the very next instruction (the page containing that instruction).

    Other pages may not be referenced until 10, 100, or perhaps 1000 instructions later. Each page

    can be labeled with the number of instructions that will be executed before that page is first

    referenced.

    The optimal page algorithm simply says that the page with the highest label should be removed.

    If one page will not be used for 8 million instructions and another page will not be used for 6

    million instructions, removing the former pushes the page fault that will fetch it back as far into

    the future as possible. Computers, like people, try to put off unpleasant events for as long as they

    can.

    The only problem with this algorithm is that it is unrealizable. At the time of the page fault, the

    operating system has no way of knowing when each of the pages will be referenced next. (We

    saw a similar situation earlier with the shortest job first scheduling algorithmhow can the

    system tell which job is shortest?) Still, by running a program on a simulator and keeping track

    of all page references, it is possible to implement optimal page replacement on the second run by

    using the page reference information collected during the first run.

    In this way it is possible to compare the performance of realizable algorithms with the best

    possible one. If an operating system achieves a performance of, say, only 1 percent worse than

    the optimal algorithm, effort spent in looking for a better algorithm will yield at most a 1 percent

    improvement.

    To avoid any possible confusion, it should be made clear that this log of page references refers

    only to the one program just measured and then with only one specific input. The pagereplacement algorithm derived from it is thus specific to that one program and input data.

    Although this method is useful for evaluating page replacement algorithms, it is of no use in

    practical systems. Below we will study algorithms that are useful on real systems.

  • 8/6/2019 Chapter1to3(Bankers Algo)

    11/24

    5.3 The First-In, First-Out (FIFO) Page Replacement Algorithm

    Another low-overhead paging algorithm is the FIFO (First-In, First-Out) algorithm. To illustrate

    how this works, consider a supermarket that has enough shelves to display exactly k different

    products. One day, some company introduces a new convenience foodinstant, freeze-dried,

    organic yogurt that can be reconstituted in a microwave oven. It is an immediate success, so our

    finite supermarket has to get rid of one old product in order to stock it.

    One possibility is to find the product that the supermarket has been stocking the longest (i.e.,

    something it began selling 120 years ago) and get rid of it on the grounds that no one is

    interested any more. In effect, the supermarket maintains a linked list of all the products it

    currently sells in the order they were introduced. The new one goes on the back of the list; the

    one at the front of the list is dropped.

    As a page replacement algorithm, the same idea is applicable. The operating system maintains a

    list of all pages currently in memory, with the page at the head of the list the oldest one and the

    page at the tail the most recent arrival. On a page fault, the page at the head is removed and the

    new page added to the tail of the list. When applied to stores, FIFO might remove mustache wax,

    but it might also remove flour, salt, or butter. When applied to computers the same problem

    arises. For this reason, FIFO in its pure form is rarely used.

    5.4 The Least Recently Used (LRU) Page Replacement Algorithm

    A good approximation to the optimal algorithm is based on the observation that pages that have

    been heavily used in the last few instructions will probably be heavily used again in the next few.

    Conversely, pages that have not been used for ages will probably remain unused for a long time.

    This idea suggests a realizable algorithm: when a page fault occurs, throw out the page that has

    been unused for the longest time. This strategy is called LRU (Least Recently Used) paging.

    Although LRU is theoretically realizable, it is not cheap. To fully implement LRU, it is

    necessary to maintain a linked list of all pages in memory, with the most recently used page at

    the front and the least recently used page at the rear. The difficulty is that the list must be

    updated on every memory reference. Finding a page in the list, deleting it, and then moving it to

  • 8/6/2019 Chapter1to3(Bankers Algo)

    12/24

    the front is a very time consuming operation, even in hardware (assuming that such hardware

    could be built).

    However, there are other ways to implement LRU with special hardware. Let us consider the

    simplest way first. This method requires equipping the hardware with a 64-bit counter, C, that is

    automatically incremented after each instruction. Furthermore, each page table entry must also

    have a field large enough to contain the counter. After each memory reference, the current value

    ofC is stored in the page table entry for the page just referenced. When a page fault occurs, the

    operating system examines all the counters in the page table to find the lowest one. That page is

    the least recently used.

    Now let us look at a second hardware LRU algorithm. For a machine with n page frames, the

    LRU hardware can maintain a matrix of n n bits, initially all zero. Whenever page frame k is

    referenced, the hardware first sets all the bits of row k to 1, then sets all the bits of column k to 0.

    At any instant, the row whose binary value is lowest is the least recently used, the row whose

    value is next lowest is next least recently used, and so forth. The workings of this algorithm are

    given in Fig. 4-3 for four page frames and page references in the order

    0 1 2 3 2 1 0 3 2 3

    After page 0 is referenced, we have the situation ofFig. 4-3(a). After page 1 is reference, we

    have the situation ofFig. 4-3(b), and so forth.

  • 8/6/2019 Chapter1to3(Bankers Algo)

    13/24

    OUTPUT

    Please choose the algorithm from the menu

    1. Scheduling2. Page Replacement3. Bankers4. Exit

    3

    You have chosen BANKERS ALGORITHM for implementation

    5. Input6. New Request7. Safe State or Not8. Print9. Exit

    Enter your choice: 5

    Enter number of processes: 5

    Enter number of resources: 3

    Enter the available resources:

    For resource type 0: 10

    For resource type 1: 5

    For resource type 2: 7

    EnterMax and Allocated resource for P0:

    Enter the Max of Resource 0: 7

  • 8/6/2019 Chapter1to3(Bankers Algo)

    14/24

    Enter the Max of Resource 0: 0

    Enter the Max of Resource 1: 5

    Enter the Max of Resource 1: 1

    Enter the Max of Resource 2: 3

    Enter the Max of Resource 2: 0

    EnterMax and Allocated resource for P1:

    Enter the Max of Resource 0: 3

    Enter the Max of Resource 0: 2

    Enter the Max of Resource 1: 2

    Enter the Max of Resource 1: 0

    Enter the Max of Resource 2: 2

    Enter the Max of Resource 2: 0

    EnterMax and Allocated resource for P2:

    Enter the Max of Resource 0: 9

    Enter the Max of Resource 0: 3

    Enter the Max of Resource 1: 0

    Enter the Max of Resource 1: 0

    Enter the Max of Resource 2: 2

    Enter the Max of Resource 2: 2

    EnterMax and Allocated resource for P3:

  • 8/6/2019 Chapter1to3(Bankers Algo)

    15/24

    Enter the Max of Resource 0: 2

    Enter the Max of Resource 0: 2

    Enter the Max of Resource 1: 2

    Enter the Max of Resource 1: 1

    Enter the Max of Resource 2: 2

    Enter the Max of Resource 2: 1

    EnterMax and Allocated resource for P4:

    Enter the Max of Resource 0: 4

    Enter the Max of Resource 0: 0

    Enter the Max of Resource 1: 3

    Enter the Max of Resource 1: 0

    Enter the Max of Resource 2: 3

    Enter the Max of Resource 2: 2

    5. Input6. New Request7. Safe State or Not8. Print9. Exit

    Enter your choice: 8

    Number of processes: 5

    Number of resources: 3

  • 8/6/2019 Chapter1to3(Bankers Algo)

    16/24

    Pid: Max Allocated Need

    P0: 7 5 3 0 1 0 7 4 3

    P1: 3 2 2 2 0 0 1 2 2

    P2: 9 0 2 3 0 2 6 0 0

    P3: 2 2 2 2 1 1 0 1 1

    P4: 4 3 3 0 0 2 4 3 1

    Available: 3 3 2

    5. Input6. New Request7. Safe State or Not8. Print9. Exit

    Enter your choice: 7

    The system is in safe state P1, P3, P4, P0, P2

    5. Input6. New Request7. Safe State or Not8. Print9. Exit

    Enter your choice: 6

    Requesting process id: 1

    Number of request for resource 0: 1

    Number of request for resource 1: 0

  • 8/6/2019 Chapter1to3(Bankers Algo)

    17/24

    Number of request for resource 2: 2

    The system is in safe state P1, P3, P4, P0, P2

    Request Committed

    5. Input6. New Request7. Safe State or Not8. Print9. Exit

    Enter your choice: 6

    Requesting process id: 4

    Number of request for resource 0: 3

    Number of request for resource 1: 3

    Number of request for resource 2: 0

    Lack of resources: Process State Wait

    5. Input6. New Request7. Safe State or Not8. Print9. Exit

    Enter your choice: 6

    Requesting process id: 0

    Number of request for resource 0: 0

    Number of request for resource 1: 2

  • 8/6/2019 Chapter1to3(Bankers Algo)

    18/24

    Number of request for resource 2: 0

    The system is not in safe state

    Granting leads to unsafe state: Request Denied

    5. Input6. New Request7. Safe State or Not8. Print9. Exit

    Enter your choice: 9

    Please choose the algorithm from the menu

    1. Scheduling2. Page Replacement3. Bankers4. Exit

    1

    You have chosen CPU SCHEDULING ALGORITHMS for implementation

    5. Input6. First come first serve scheduling7. Shortest job first scheduling8. Round robin scheduling9. Exit

    Enter your choice: 5

    THIS IS DATA ENTRY FORCPU SCHEDULING ALGORITHMS

    Enter the number of process: 3

  • 8/6/2019 Chapter1to3(Bankers Algo)

    19/24

    Enter the burst time for process P1: 24

    Enter the burst time for process P2: 3

    Enter the burst time for process P3: 3

    5. Input6. First come first serve scheduling7. Shortest job first scheduling8. Round robin scheduling9. Exit

    Enter your choice: 6

    This is the FIRST COME FIRST SERVED algorithm

    Burst time for process P1= 24

    Burst time for process P2= 3

    Burst time for process P3= 3

    Total waiting time= 51.000000

    Average waiting time= 17.000000

    Average turnaround time= 27.00000

    5. Input6. First come first serve scheduling7. Shortest job first scheduling8. Round robin scheduling9. Exit

    Enter your choice: 7

    This is the SHORTEST JOB FIRST SERVED algorithm

  • 8/6/2019 Chapter1to3(Bankers Algo)

    20/24

    Burst time for process P1= 24

    Burst time for process P2= 3

    Burst time for process P3= 3

    Total waiting time= 9.000000

    Average waiting time= 3.000000

    Average turnaround time= 13.00000

    5. Input6. First come first serve scheduling7. Shortest job first scheduling8. Round robin scheduling9. Exit

    Enter your choice: 8

    This is the ROUND ROBIN SCHEDULING algorithm

    Burst time for process P1= 24

    Burst time for process P2= 3

    Burst time for process P3= 3

    Enter time quantum: 4

    Total waiting time= 17.000000

    Average waiting time= 5.666667

    Average turnaround time= 15.666667

    5. Input6. First come first serve scheduling

  • 8/6/2019 Chapter1to3(Bankers Algo)

    21/24

    7. Shortest job first scheduling8. Round robin scheduling9. Exit

    Enter your choice: 9

    Please choose the algorithm from the menu

    1. Scheduling2. Page Replacement3. Bankers4. Exit

    2

    You have chosen PAGE REPLACEMENT ALGORITHM for implementation

    5. Least Recently Used Page Replacement Algorithm6. First In First Out Page Replacement Algorithm7. Optimal Page Replacement Algorithm8. Exit

    Enter your choice: 5

    You have chosen Least Recently Used Page Replacement Algorithm for implementation

    2 -1 -1

    2 3 -1

    2 3 -1

    2 3 1

    2 5 1

    2 5 1

    2 5 4

  • 8/6/2019 Chapter1to3(Bankers Algo)

    22/24

    2 5 4

    3 5 4

    3 5 2

    3 5 2

    3 5 2

    Number of page faults: 4

    5. Least Recently Used Page Replacement Algorithm6. First In First Out Page Replacement Algorithm7. Optimal Page Replacement Algorithm8. Exit

    Enter your choice: 6

    You have chosen First In First Out Page Replacement Algorithm for implementation

    2 -1 -1

    2 3 -1

    2 3 -1

    2 3 1

    5 3 1

    5 2 1

    5 2 4

    5 2 4

    3 2 4

    3 2 4

    3 5 4

    3 5 2

    Number of Page Faults: 6

  • 8/6/2019 Chapter1to3(Bankers Algo)

    23/24

    5. Least Recently Used Page Replacement Algorithm6. First In First Out Page Replacement Algorithm7. Optimal Page Replacement Algorithm8. Exit

    Enter your choice: 7

    You have chosen Optimal Page Replacement Algorithm for implementation

    2 -1 -1

    2 3 -1

    2 3 -1

    2 3 1

    2 3 5

    2 3 5

    4 3 5

    4 3 5

    4 3 5

    2 3 5

    2 3 5

    2 3 5

    Number of page Faults : 3

    5. Least Recently Used Page Replacement Algorithm6. First In First Out Page Replacement Algorithm7. Optimal Page Replacement Algorithm8. Exit

    Enter your choice: 8

    Please choose the algorithm from the menu

  • 8/6/2019 Chapter1to3(Bankers Algo)

    24/24

    1. Scheduling2. Page Replacement3. Bankers4. Exit

    4