Sunteți pe pagina 1din 115

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

Define a thread with its advantages


Understand the models for multithreading
Study the critical section problem
Illustrate semaphores with its types
Understand various synchronization mechanisms

Classify four classical problems of synchronization


Understand a deadlock situation and describe how it can be

prevented, avoided, detected and recovered


ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

A thread is a program execution that uses the resources of a

process.
Since threads have some of the properties of a process, it is
sometimes called as lightweight process
Thread comprises:
Thread ID

A program counter
A register set
A Stack

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

Process
Thread 2

Time

Thread 1

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

Preemptive multithreading
Allows the operating system to determine time for context switching.
Disadvantage Context switching happens in an inappropriate time
and may leaf to negative effects. This may be avoided by cooperative
multithreading.

Cooperative multithreading
Depends on the threads themselves to release control once they are at
a stopping point.
Disadvantage Problems may be created if any thread waits for a
resource availability
ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

Heavyweight process

Information global to
all threads in a
process

Information local to
each thread

Address space
Other global process data
.
.
.

Registers

Registers

Registers

Stack

Stack

Stack

Mask

Mask

Mask

TSD

TSD

TSD

Threads

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

Data

Code

Open Files

Registers

Signal

Stack

Thread

ICT Academy of Tamil Nadu

Data

Code

Open Files

Signal

Registers

Registers

Registers

Stack

Stack

Stack

Thread

Thread

Thread

An Initiative of Government of India,


Government of Tamil Nadu and CII

A process with multiple threads can be viewed as a large


server. E.g: Print Server
As threads share common data, inter process communication
is not needed
Threads use very little resources and they do not need new

address space, global data, program code or operating system


resources
Context switching is faster when working with threads.
ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

Threads share CPU and only one active thread at a time


Threads within a process execute sequentially
Thread can create children
If one thread is blocked another thread can run

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

10

Threads are not dependent on one another


All threads can access every address in the task
Threads assist one another

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

11

User
Level
Threads

ICT Academy of Tamil Nadu

Kernel
Level
Threads

An Initiative of Government of India,


Government of Tamil Nadu and CII

12

User level threads do not require the operating system to


support threads
User level threads are more portable as they do not rely on a

particular operating systems thread


Program counter, registers, stack and small control block are
represented in a simple way
Without kernel intervention, threads are created, switched and
synchronized.
Threading library controls the scheduling.
ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

13

User level threads are also


called as many-to-one
thread mapping.
Threads
Process Scheduling
Thread Scheduling

CPU

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

14

Improved performance by which the kernel can dispatch


processs threads to several processors at one time.
Kernel manages each thread individually by which each
processs ready thread is dispatched even if the thread is
blocked.

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

15

Kernel Level
Threads

Kernel level threads are


also called as one-to-one
thread mapping.

Threads
Process Scheduling
Thread Scheduling

CPU

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

16

User level threads

Kernel level threads

User level threads are faster to create and Kernel level threads are slower to create and
manage.
manage.

Implemented by a thread library at the user Operating system support directly to Kernel
level.
threads.
User level thread can run on any operating Kernel level threads are specific to the operating
system.
system.
Support provided at the user level called user Support may be provided by Kernel is called
level thread.
Kernel level threads.
Multithread
application
cannot
advantage of multiprocessing.

take Kernel
routines
multithreaded.

ICT Academy of Tamil Nadu

themselves

can

An Initiative of Government of India,


Government of Tamil Nadu and CII

be

17

Software Design

Performance

Cooperation

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

18

Whenever a thread is created, it is put into the ready state.


When the task is completed or terminated, a thread in the
running state enters the dead state
It enters blocked state when it must wait for the completion
of any I/O request
If a thread is waiting for any event, it enters the waiting state.
A thread can enter the sleeping state for a period of time

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

19

Create

Exit

Suspend

Resume

Sleep

Wake

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

20

Cancel
Join

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

21

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

22

One-to-One
Model

Many-toOne Model

ICT Academy of Tamil Nadu

Many-toMany
Model

An Initiative of Government of India,


Government of Tamil Nadu and CII

23

User Thread

ICT Academy of Tamil Nadu

Kernel Thread

An Initiative of Government of India,


Government of Tamil Nadu and CII

24

User Thread

ICT Academy of Tamil Nadu

Kernel Thread

An Initiative of Government of India,


Government of Tamil Nadu and CII

25

User Thread

The Critical Section Problem

ICT Academy of Tamil Nadu

Kernel Thread

An Initiative of Government of India,


Government of Tamil Nadu and CII

26

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

27

When one process is using shared changeable data in its


critical section, no other process is permitted to execute in its
critical section.
No two processes are executing in their critical sections at the
same time.

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

28

do
{
entry section
critical section
exit section
remainder section

} while(1);

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

29

Wait

Enter

Critical Section
Exit
Signal

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

30

Two or more processes are varying with each other; in order


to reach completion
The process moves ahead of the other process.
When multiple processes access the same data, the execution
takes place depending on the order of the access.

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

31

Three goals for solving the critical section problem:


Mutual exclusion
Progress
Bounded waiting

Two general approaches to handle critical section:


Preemptive kernels
Non-preemptive kernels

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

32

For any two process, the data variables that are shared are:

int a;
boolean flag [2];

Three algorithms are used to solve the problem

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

33

All processes share the common integer variable a that has


been initialized to 0 0r 1
do
{
while (a!=i);
//critical section
a=j;
//remainder section
} while(1);

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

34

The variable a is replaced with the array as Boolean a[2]


do
{
a[i]=true;
while(a[j])
//critical section
a[i]=false;
//remainder Section
} while(1);

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

35

Each process shares two variables as:


Boolean a[2]
int b
do
{
a[i]=true;
b=j;
while(a[j]&&b==j)
//critical Section
a[i]=false
//remainder Section
} while(1)

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

36

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

37

Any solution to the critical-section problem requires a simple


tool called lock to protect the critical region
Modern computer system either test or modify the content or
swap the contents automatically like one uninterruptible unit
The TestAndSet() instruction is defined as below:
boolean TestAndSet (boolean *b)
{
boolean a=*b;
*b=true;
return a;
}
An Initiative of Government of India,
ICT Academy of Tamil Nadu
Government of Tamil Nadu and CII

38

Structure of process Pi is shown below:


do
{
while (TestAndSet(&lock));
//critical section
lock=false;
//remainder section
} while (1);

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

39

Swap() instruction is defined as below:

void Swap(boolean *x, boolean *y)


{
boolean z = *x;
*x = *y;
*y = z;
}

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

40

Mutual exclusion implementation with the Swap() instruction


is given below:
do
{
key = TRUE;
while (key == TRUE)
Swap (&lock, &key);
//critical section
lock = FALSE;
//remainder section
}while (1);

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

41

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

42

A semaphore is essentially an integer protected variable S


which is treated in a special way
The following are the two calls associated with a semaphore,
Lock
Unlock

A semaphore S is an integer variable that is accessed only


through two standard atomic operations: wait() and signal()

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

43

Definition of wait() is given below:


wait(S)
{
while S <= 0
S=S-1;
}

Definition of signal() is given below:


signal(S)
{
S=S+1;
}

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

44

All the system resources are used by the operating systems.


Semaphore is used to assign a particular resource to a
program.
Once the resources is used or no longer needed by the
program, the semaphore is returned back to the operating
system so that it can be allocated to another program

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

45

They are machine independent.


They are simple to implement.
They can be easily debugged.
There

are

different

critical

sections

with

different

semaphores.
They acquire many resources simultaneously.

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

46

Binary
semaphores

ICT Academy of Tamil Nadu

Counting
semaphores

An Initiative of Government of India,


Government of Tamil Nadu and CII

47

An integer value that can range only between 0 and 1


It can be used with multiple processes that solve the critical
section problems

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

48

The implementation mechanism of the binary semaphores is


very simple.

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

49

When a process that is executing in its critical section exists,


the waiting processes decrements to 0 and enter its critical
section.
Process that waits to enter its critical section will perform
busy waiting thus wasting CPU cycles.

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

50

A non-binary semaphore that can take any non-negative


integer value

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

51

All the waiting processes enter their critical section in FCFS


order. Thus bounded waiting is fully met.
A process that waits does not perform busy waiting thus
saving some CPU cycles.

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

52

The implementation mechanism of the counting semaphores


is more complex as it involves FCFS queue.
The context switches will involve certain overheads.

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

53

Full
Used when the counter for number of slots indicates full.
Empty
Used when the counter doe number of slots indicates empty.
Mutex
Producer and consumer will not access modifiable shared
section of the buffer.

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

54

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

55

A solution to a synchronization problem can be reached by


inter process communication using semaphores
It must also hold three important properties:
Correctness
Maximum concurrency

No busy waits

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

56

Consider a pool that consists of 11 buffers, each capable of


holding one item
Semaphore empty is initialized to value n and semaphore
full is initialized to value 0

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

57

do
{
.
.
//produces an item
.
.
wait (empty);
wait (mutex);
.
.
//Add an item
.
.

signal (mutex);
signal (full);
}while(1);
ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

58

do
{
wait (full);
wait (mutex);
.
.
//Remove an item
.
.
signal (mutex);
signal (empty);
.
.
//consume the item
.
.
}while(1);
ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

59

Consider a data area that is shared among a number of


processes
Number of readers can read from the data area but only one
writer can write data into the data area
If a writer is writing to the data area, then no reader is

allowed to read it
Also if a reader is reading from the data area, no other reader

reads the data from the data area


ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

60

An object is shared among many threads, each belonging to


one of the following two classes:
Readers: read data, never modify it
Writers: read data and modify it

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

61

The reader process shares the following declaration:


semaphore mutex, w;
int i;

Structure of the writer process:

do
{
wait (w);
.
.

ICT Academy of Tamil Nadu

// writing is performed
.
.
signal (w) ;
}while(1);
An Initiative of Government of India,
Government of Tamil Nadu and CII

62

Structure of the reader process:


mutex and w are initalized to 1
i is initlaized to 0

w is common to both reader and


writer
mutex ensure mutual exclusion

when i is modified
i is the count variable used to keep
track of the number of processes

currently reading the object

ICT Academy of Tamil Nadu

do
{
wait (mutex);
i++;
if (i==1)
wait (w);
signal (mutex);
.
.
//reading is performed
.
.
wait (mutex);
i--;
if (i==0)
signal (w);
signal (mutex);
}while(1);
An Initiative of Government of India,
Government of Tamil Nadu and CII

63

There are five philosophers seated around a circular table


with a plate and two forks
The philosophers share a circular table bounded by five
chairs, each belonging to one philosopher
In the center of the table is a bowl of rice, and the table is
placed with five single chopsticks

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

64

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

65

There are at most four philosophers who will be sitting


simultaneously at the circular table.
The philosopher is allowed to pick up the chopsticks only if
both chopsticks are available (normally it is picked up in a
critical section)

Odd philosopher picks up left chopstick first and then the


right chopstick,
Even philosopher picks up the right chopstick first and then
her left chopstick.
ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

66

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

67

Deadlock is a situation in which two process namely Process


A and Process B shares the resources specifically Resource A
and Resource B which prevents each other from accessing the
resource, resulting in both programs terminating the function.
A deadlock is a situation in which some processes wait for
each others actions indefinitely.

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

68

Process
A

Process
B

Resource A

Resource B

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

69

Mutual exclusion
Only one process accessing one resource.

Resource holding
One process holding one resource which do not release that process and
waits for other process for accessing purpose.

No preemption
A resource that cannot be released from the process

Circular wait
All processes holds one or more resources that demands the next process in
the chain

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

70

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

71

1. Deadlocks on file requests


2. Deadlocks in databases
3. Deadlocks in dedicated device allocation
4. Deadlocks in multiple device allocation
5. Deadlocks in spooling
6. Deadlocks in disk sharing
7. Deadlocks in a network

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

72

Deadlock happens when a process holds files for complete


duration of the execution
Once this completes, the process can release the files F1 and
F2
A process is withdrawn or removed from the resource and
this makes the deadlock to wait for a longer time

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

73

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

74

Two processes can access the resource and the records in the
database is locked
3 different levels of locking :
Entire database is locked
A subsection of the database is locked

Individual record is locked

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

75

If the records are not locked then races condition takes place
P1 access R1 and locks it.
P2 access R2 and locks it.
P1 request R2 but R2 is locked by P2.
P2 request R1 but R1 is locked by P1.

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

76

If the devices available are less in number, then deadlock


occurs.
E.g., printers, plotters or CD drives.
P1 requests printer 1 and gets it.
P2 requests printer 2 and gets it.

P1 requests printer 2 but printer 2 is blocked by P2.


P2 requests printer 1 but printer 1 is blocked by P1.

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

77

When many processes request for a resource it hold on to the


dedicated devices while other processes also proceeds in a
same manner.

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

78

Many systems have distorted devices such as a printer, plotter


into a device that can be shared by high-speed device, a disk
and the CPU.

Disk acts as a temporary storage area to accept output from


users for all output till printer is prepared to accept it
(spooling).
If a printer requires process result before it will starts printing,
but spooling system fills available disk space with only
partially completed output.
ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

79

Disks are designed so that it is common for 2 processes to


share different regions of same disk.
All processes that share the data send different commands for
using the resource.

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

80

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

81

A network if it is in traffic does not have protocols through


which the flow of messages can be controlled

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

82

Deadlock
Prevention

Prevent one of the


four conditions
from occurring.

Deadlock
Avoidance

Avoid the deadlock


if it becomes
probable.

Deadlock
detection

Detect the
deadlock when it
occurs and recover
from it gracefully.

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

83

Mutual
Exclusion

Resource
Holding

No
Preemption

Circular
Wait

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

84

Here some resources namely Memory unit, CPU and other


devices may be allocated to only one user at a time.
Spooling can be used for some resources

It holds for resources that cannot be shared


Example: Printer cannot be shared

Resources that can be shared do not lead to deadlock


Example: Read-only file

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

85

Every process request all resources so that the resources are


forced to run to completion
The degree of multiprogramming is decreased
Some devices can be inactive as the job allocated would be used all the
time

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

86

The resources are deallocated from the process


Good if process state is easily saved and restored.
Bad for I/O devices and also if the files are modified

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

87

Can be prevented if the circle is not formed.


Havenders solution by putting numbers for the resources
Each process requests resources in ascending order

Process requesting number one is allocated first

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

88

Safe State
All process gets all resources requested by them.
Process may be allocated in the following order
P1, P2, P3,,Pn

For a system in safe state, there is no deadlock

Unsafe State
When Pi enters to terminate state, then the next process namely Pi+1, in
its order, receives that resource

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

89

deadlock

unsafe
state

safe
state

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

90

It handles only one instance for resource types

Each process with a directed edge of graph is allocated to each


resource
Claim edge: Dashed line by which a process is allocated to a resource
Assignment edge: Directed line by which a resource is released by a
process

The resources that is allocated will reverse the edge of the direction
The resources that is released by the process is returned to the edge
of the direction
No cycle of graph is created
ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

91

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

92

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

93

It handles multiple instances for resource types


All process must be used at a maximum rate
A process requesting a resource needs to wait
When it gets the resource, it must place them in a finite
amount of time

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

94

Let n be the number of process and m be the number of


resources
Available : vector [1..m]
Max: matrix [1..n, 1..m]
Allocation: matrix [1..n, 1..m]

Need: matrix [1..n, 1..m]


Need[i,j] = Max[i,j] Allocation [i,j]

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

95

Work and Finish are two vector with length m and n namely
Work=vector[1..m] and Finish=vector[1..n]
Work = Available
Finish [i] = False where i= 0, 1,,n-1

Find i where,
Finish[i]=False

Need[i]<=Work

Work = Work+Allocation
Finish[i]=True, then go to Step 1

If Finish[i]=True, then the system is in safe state


ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

96

If Request[i]<=Need[i], go to step 2
If Request[i]<=Available, go to step 3
Available= Available- Request[i]
Allocation[i]=Allocation[i]+Request[i]
Need[i]=Need[i]- Request[i]

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

97

An algorithm that periodically defines the state of the system


to decide whether a deadlock has occurred in the system or
not
An algorithm to recover from the deadlock

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

98

One instance for one resource


A resource which has only one instance makes use of variant of the
resource allocation graph. This is called wait-for graph

Pi waits for Pj to release the resource that Pi requires.


An algorithm is used to identify the cycle in the graph which needs an
order of n2 operations. Here n is the number of variants defined in the
graph.

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

99

Resource Allocation Graph

P5

R1

R3

R4

P1

P2

P3

R2

P4

R5

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

100

Wait for Graph


P5

P1

P2

P3

P4

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

101

Multiple instance for one resource


Same as Bankers Algorithm with the following differences
Available : vector [1..m]

Allocation: matrix [1..n, 1..m]


Need: matrix [1..n, 1..m]
Need is replaced by request by which request [i] is the vector of the request
resource i

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

102

Work and Finish are two vector with length m and n namely
Work=vector[1..m] and Finish=vector[1..n]
Work = Available
If Allocation 0, then Finish[i] = False else Finish[i] = True where i= 0,
1,,n-1

Find i where
Finish[i]=False
Request [i]<=Work

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

103

Work = Work+Allocation
Finish[i]=True, then go to Step 2

If Finish[i]=False, then the system is said to be deadlocked


state

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

104

Process
termination

ICT Academy of Tamil Nadu

Resource
preemption

An Initiative of Government of India,


Government of Tamil Nadu and CII

105

Order to kill the process can be selected from:


Priority
Computation time
Used resources
Completed resource
No of processes to be terminated
Whether process is interactive or batch

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

106

Choose the process which is in the deadlock state


Rollback the process
The process is returned to the safe state and the system is restarted
form that safe state

Starvation
The same process can always be selected as the victim by considering
the cost factor

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

107

A thread is a program execution that uses the resources of a


process and consists of thread ID, a program counter, a
register set, a stack.
The implementation models of thread are user level thread
and kernel level thread.
Threads at the user level are implemented in user-level
libraries rather than using the system calls.

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

108

Kernel level threads map each thread to its own execution

context.
The thread states are ready, running, blocked, waiting, and

sleeping
The thread operations are create, exit, suspend, resume,

sleep, wake.
Multithreading refers to the ability of an operating system to
support multiple, concurrent paths of execution within a
single process.
ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

109

The three types of multithreading models are many-to-one


model, one-to-one model, many-to-many model.
When one process is using shared changeable data in its
critical section, no other process is permitted to execute in its
critical section.
Race condition is defined by which two or more processes are
varying with each other; in order to reach completion, the

process moves ahead of the other process


ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

110

Mutual exclusion is a situation where one process is executing


in its critical section and no other process is allowed to
execute its critical section.
Simple hardware instructions are used effectively to solve the
critical-section problem. Any solution to the critical-section
problem requires a simple tool called lock to protect the
critical region.

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

111

Semaphore is a shared integer variable with on-negative


values that can be subjected only to the following operation:
Initialization
The indivisible operations wait and signal

A solution to a synchronization problem can be reached by

inter process communication using semaphores.

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

112

A deadlock is a situation in which some processes wait for


each others actions indefinitely.
The four conditions of deadlock are mutual exclusion,
resource holding, no preemption, circular wait.
Deadlock may occur on files, databases, device allocation,
spooling ,disk sharing and network.

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

113

Deadlock can be prevented by one of the four conditions


namely mutual exclusion, resource holding, no preemption,
circular wait.
Deadlock avoidance is the deadlock handling approach by
which the deadlocks are dealt with before they occur.

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

114

Deadlock can be detected by an algorithm that periodically


defines the state of the system to decide whether a deadlock
has occurred in the system or not.
Deadlock can be recovered by either terminating the process
or preempting the resource.

ICT Academy of Tamil Nadu

An Initiative of Government of India,


Government of Tamil Nadu and CII

115

S-ar putea să vă placă și