Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Assignment 3

Submitted By
Muneeb Shahid
Roll No
19011598-138
Section
SE-4C
Submitted To
Mr Shahzaib Abbas
QUESTION # 04
Deadlock is a line between safe and unsafe state.

A Deadlock is a situation where each of the computer process waits for a


resource which is being

assigned to some another process. In this situation, none of the process


gets executed since the resource

it needs, is held by some other process which is also waiting for some other
resource to be released

A state is safe if the system can allocate resources to each process (up to its
maximum) in some order and

still avoid a deadlock.

More formally, a system is in a safe state only if there exists a safe


sequence.

A sequence of processes is a safe sequence for the current allocation state


if, for

each the resource requests that can still make can be satisfied by the
currently available resources

plus the resources held by all, with.

In this situation, if the resources that needs are not immediately available,
then can wait until

all have finished.

When they have finished, can obtain all of its needed resources, complete
its designated task, return its

allocated resources, and terminate.


When terminates, can obtain its needed resources, and so on.If no such
sequence exists, then

the system state is said to be unsafe.

A safe state is not a deadlocked state. Conversely, a deadlocked state is an


unsafe state. Not all unsafe states

are deadlocks

QUESTION # 05
Concurrent programs are well known for containing errors that are difficult
to detect,

reproduce, and diagnose. Deadlock is a common concurrency error, which


occurs when a set of

threads are blocked, due to each attempting to acquire a lock held by


another.

Multi-Threaded application counter deadlock easily because threads run


simultaneously and

when more than one thread tries to access the same resource
simultaneously there is a

deadlock. Hence in Multi-threaded applications deadlocks are encountered


easily.

There are two popular ways to detect deadlocks.

One is to have threads set checkpoints. For example, if you have a thread
that has a work loop,

you set a timer at the beginning of doing work that's set for longer than you
think the work could
possibly take. If the timer _res, you assume the thread is deadlocked. When
the work is done, you

cancel the timer.

Another (sometimes used in combination) is to have things that a thread


might block on track

what other resources a thread might hold. This can directly detect an
attempt to acquire one lock

while holding another one when other threads have acquired those locks in
the opposite order. This

can even detect deadlock risk without the deadlock actually occurring. If
one thread acquires lock

A then B and another acquires lock B Then A, there is no deadlock unless


they overlap. But this

method can detect it.

Advanced deadlock detection is typically only used during debugging. Other


than coding the

application to check each blocking lock for a possible deadlock and knowing
what to do if it

happens, the only thing you can do after a deadlock is tear the application
down. You can't release

locks blindly because the resources they protect may be in an inconsistent


state.

Sometimes you deliberately write code that you know can deadlock and
specifically code it to

avoid the problem. For example, if you know lots of threads take lock A and
then try to acquire
lock B, and some other thread needs to do the reverse, you can code it do a
non-blocking attempt

to lock B and release lock A if it fails.

Typically, it's more useful to spend your effort making deadlocks impossible
rather than making

the code detects and work around deadlocks.

Benefits of Multithreading:

Improved throughput. Many concurrent computer operations and I/O


requests within a
single process.
Simultaneous and fully symmetric use of multiple processors for
computation and I/O
Superior application responsiveness. If a request can be launched on its
own thread,
applications do not freeze or show the "hourglass". An entire application
will not block,
or otherwise wait, pending the completion of another request.
Improved server responsiveness. Large or complex requests or slow
clients don't block
other requests for service. The overall throughput of the server is much
greater.
Minimized system resource usage. Threads impose minimal impact on
system resources.
Threads require less overhead to create, maintain, and manage than a
traditional process.

QUESTION # 06
How monitor helps to solve synchronization problems.

Classical Problem of Synchronization:


Dining-Philosophers Problem

Solution to Dining Philosophers:

monitor DiningPhilosophers

enum { THINKING; HUNGRY, EATING) state [5] ;

condition self [5];

void pickup (int i) {

state[i] = HUNGRY;

test(i);

if (state[i] != EATING) self[i].wait;}

void putdown (int i) {

state[i] = THINKING;

// test left and right neighbors

test((i + 4) % 5);

test((i + 1) % 5);}

void test (int i) {

if ((state[(i + 4) % 5] != EATING) &&

(state[i] == HUNGRY) &&

(state[(i + 1) % 5] != EATING) ) {

state[i] = EATING ;
self[i].signal () ;}}

initialization_code() {

for (int i = 0; i < 5; i++)

state[i] = THINKING;}}

Each philosopher i invokes the operations pickup() and putdown() in the


following sequence:

DiningPhilosophers.pickup(i);

EAT

DiningPhilosophers.putdown(i);

No deadlock
QUESTION # 07

Write important term keywords from Chapter of Process Synchronization


and Deadlock.
Keywords from Process Synchronization:
 Producer-Consumer Process
 Race Condition
 Critical Section Problem
 Mutual exclusion
 Progress.
 Bounded waiting.
 Preemptive kernels
 Non-Preemptive kernels
 Peterson’s Solution
 Synchronization Hardware
 Mutex Locks
 Acquire () and Release ()
 Semaphore
 Semaphore Usage
 Deadlock
 Starvation
 Priority Inversion
 Classical Problems of Synchronization
 Dining-Philosophers Problem
 Monitors
 Monitor with Condition Variables

Keywords from Deadlock:

 System Model
 Mutual exclusion
 Hold and wait
 No preemption
 Circular wait
 Deadlock with Mutex Locks
 Resource-Allocation Graph
 Deadlock Prevention
 Deadlock Avoidance
 Safe State
 Unsafe State
 Deadlock State
 Banker’s Algorithm
 Safety Algorithm
 Deadlock Detection
 Wait-for Graph
 Detection Algorithm
 Process Termination
 Resource Preemption

You might also like