19
Team Project Presentation The implementation of Banker’s algorithm, data structure and parser Ikwhan Chang (010754107) Group #10 2-Dec 2016 1

The implementation of Banker's algorithm, data structure and its parser

Embed Size (px)

Citation preview

Page 1: The implementation of Banker's algorithm, data structure and its parser

Team Project Presentation

The implementation of Banker’s algorithm, data structure and parser

Ikwhan Chang (010754107) Group #10

2-Dec 2016

1

Page 2: The implementation of Banker's algorithm, data structure and its parser

INDEX

• The Definition of Deadlock and Banker’s Algorithm

• Deadlock

• Safe State & Unsafe State

• Banker’s algorithm

• Implementation

• Approach

• UML & Actor model

• Program Design

• Demo

2

Page 3: The implementation of Banker's algorithm, data structure and its parser

Deadlock3

• A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set.

Page 4: The implementation of Banker's algorithm, data structure and its parser

Four Conditions for Deadlock

1) Mutex Exclusion: each resource can be assigned or used only one process at a request

time

2) Hold and Wait: The process can capture the currently needed resources and request

the new resources needed.

3) No Preemption: Resources already allocated are not forcibly deprived by other

processes.

4) Circular Wait: This situation is intertwined in the shape of a circle.

4

Page 5: The implementation of Banker's algorithm, data structure and its parser

Deadlock Prevention Vs. AvoidanceDeadlock Prevention

• Preventing deadlocks by constraining how requests for resources can be made in the system and how

they are handled (system design).

• The goal is to ensure that at least one of the necessary conditions for deadlock can never hold.

Deadlock Avoidance - Banker’s algorithm

• The system dynamically considers every request and decides whether it is safe to grant it at this point,

• The system requires additional a priori information regarding the overall potential use of each resource for each process.

• Allows more concurrency.

• => Similar to the difference between a traffic light and a police officer directing traffic.

5

Page 6: The implementation of Banker's algorithm, data structure and its parser

Safe State vs Unsafe StateSafe state

• Status in which the task can be completed

• Safety Sequence: The order in which all processes can complete tasks using available resources

• => If you find the safety sequence, then we can call it is Safe

Unsafe State

• The state that a deadlock can occur

• We cannot find the Safety Sequence

• Deadlock does not occur in safe state, but it can change from safe state to unsafe state.

• The situation where you do not switch from unsafe to deadlock is partial return, if you return in full and go from unsafe to safe.

• If the next state is unsafe, deny resource allocation

6

Page 7: The implementation of Banker's algorithm, data structure and its parser

Banker’s algorithm - DefinitionSuppose we know the “worst case” resource needs of processes in advance

• A bit like knowing the credit limit on your credit cards. (This is why they call it the Banker’s Algorithm)

Observation: Suppose we just give some process ALL the resources it could need…

• Then it will execute to completion.

• After which it will give back the resources.As a result:

• A process pre-declares its worst-case needs

• Then it asks for what it “really” needs, a little at a time

• The algorithm decides when to grant requestsIt delays a request unless:

• It can find a sequence of processes such that it could grant their outstanding need.

• So they would terminate. letting it collect their resources and in this way it can execute everything to completion

7

Page 8: The implementation of Banker's algorithm, data structure and its parser

Banker’s algorithm - Notation

* N: the number of processes on the system* M: the number of resources present in the system

- Available [1: m]: Indicate which resource i is

- Max [1: n, 1: m]: Expression of the maximum number of resources of type j of process i

- Allocation [1: n, 1: m]: Indicates whether process i has received a resource of type j

- Need [1: n, 1: m]: Max - Allocation. Express how many more can be allocated in the future

- Vectors X, Y- Work array: an array containing the intermediate result of a resource

- Finish array: a boolean array indicating which process has terminated

8

Page 9: The implementation of Banker's algorithm, data structure and its parser

Banker’s algorithm - Safety Check

Step 1. Assign the resources currently available for work. Finish Set all values of array to false (which means that no processes have

finished yet)Step 2. Find a process i that does not finish its current run and can satisfy all of the resources it needs the most.Step 3. Terminate the process and return the resource to finish.

Step 4. When all finish [] entry values are true, the algorithm is ended

How to perform a resource grant using Safety CheckStep 1. Record the newly requested request of each process in the request arrayStep 2. If a request in a process requests more than Maximum, it will not accept the requestStep 3. If the condition is less than the maximum value, once you have performed the safety check algorithm

=> The banker's algorithm is implemented in safety and performs deadlock avoidance.=> There are situations in which a process needs to manage a maximum request for each type, making expensive decisions to perform

and deadlock avoidance.

9

Page 10: The implementation of Banker's algorithm, data structure and its parser

Banker’s algorithm - Example10

5 processes P0 through P4; 3 resource types A (10 instances), B (5instances), and C (7 instances).

Snapshot at time T0:

Allocation Max Available

A B C A B C A B C

P0 0 1 0 7 5 3 3 3 2

P1 2 0 0 3 2 2

P2 3 0 2 9 0 2

P3 2 1 1 2 2 2

P4 0 0 2 4 3 3

Page 11: The implementation of Banker's algorithm, data structure and its parser

Banker’s algorithm - Example11

The content of the matrix. Need is defined to be Max – Allocation.

Need

A B C

P0 7 4 3

P1 1 2 2

P2 6 0 0

P3 0 1 1

P4 4 3 1

The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety criteria.

Page 12: The implementation of Banker's algorithm, data structure and its parser

Approach

Problem

• In our project, we implemented the parser of banker’s algorithm. Since banker’s algorithm is quite simple to implement, we realized that it is more important to define the data structure of banker’s algorithm as well as parse of it. Thus, we are focusing on the implementation of parser and data structure.

Development Environment

•Language: C

•Compiler : gcc with c++ 4.2.1 on Apple LLVM v. 8.0.0 target for x86_64-apple-darwin16.3.0

•IDE: Xcode

12

Page 13: The implementation of Banker's algorithm, data structure and its parser

Approach1) Defined a banker’s information structure

#define A 65 #define MAX_RESOURCE 100 #define MAX_PROCESS 100 #define MAX_RESOURCE_NAME_LEN 40

typedef struct _banker_info { char resource_name[MAX_RESOURCE][MAX_RESOURCE_NAME_LEN + 1]; int available[MAX_RESOURCE]; int instance_count[MAX_RESOURCE]; int allocation[MAX_PROCESS][MAX_RESOURCE]; int max[MAX_PROCESS][MAX_RESOURCE]; int need[MAX_PROCESS][MAX_RESOURCE]; int process_count; int resource_count; int sequence[MAX_PROCESS]; int sequence_count; int is_prepared;

} banker_info;

13

2) Implemented a scanner module to separate

tokens from files.

Token Types

• ENDFILE: the end of file, 

• ERROR: scan error

• ID: Alphabet exists more than one

• NUM: Decimal exists more than one

• COLON: “:” 

Page 14: The implementation of Banker's algorithm, data structure and its parser

Approach3) Program command

• load [file_name]: Read Banker’s information from file

• view: Display the information of Banker

• quit: Exit the program

14

4) Error type for reading the input

• When the allocation value of the process is

larger than the available value of the resource

• When the allocation value of the process is

larger than its max value

• When negative value is input to all values

• When unexpected input is found in the file

format

Page 15: The implementation of Banker's algorithm, data structure and its parser

User Scenario Read information from file

15

Page 16: The implementation of Banker's algorithm, data structure and its parser

User Scenario Display the result

16

Page 17: The implementation of Banker's algorithm, data structure and its parser

Program Design17

Page 18: The implementation of Banker's algorithm, data structure and its parser

Demo18

Page 19: The implementation of Banker's algorithm, data structure and its parser

Thanks!19

Design by Matthew, Chang http://matthewlab.com