Download Deadlock Conditions

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Concurrency control wikipedia , lookup

Java ConcurrentMap wikipedia , lookup

Asynchronous I/O wikipedia , lookup

Monitor (synchronization) wikipedia , lookup

Transcript
JRE SCHOOL OF Engineering
CLASS TEST-II EXAMINATIONS APRIL 15(SOLUTION)
Subject Name Operating System
Roll No. of Student
Date
17th April 2015
For CSE & IT Branch Only(IV Sem)
Subject Code
Max Marks
Max
Duration
Time
NCS-401
30
1 Hour
9:20 AM to 10:20 AM
SECTION – A
(3 × 5 = 15)
Attempt all the questions:
Q1. Explain wait( ) & signal( ) operations?
Semaphore S is an integer variable that is accessed through standard atomic operations i.e. wait() and
signal().
It also provided basic definition of wait()
wait(Semaphore S)
{
while S<=0
; //no operation
S--;
}
Definition of signal()
signal(S)
{
S++;
}
Q2. Differentiate between thread and process.
Process
Each process provides the resources needed to execute a program. A process has a virtual address space, executable
code, open handles to system objects, a security context, a unique process identifier, environment variables, a priority
class, minimum and maximum working set sizes, and at least one thread of execution. Each process is started with a
single thread, often called the primary thread, but can create additional threads from any of its threads.
Thread
A thread is the entity within a process that can be scheduled for execution. All threads of a process share its virtual
address space and system resources. In addition, each thread maintains exception handlers, a scheduling priority,
thread local storage, a unique thread identifier, and a set of structures the system will use to save the thread context
until it is scheduled. The thread context includes the thread's set of machine registers, the kernel stack, a thread
environment block, and a user stack in the address space of the thread's process. Threads can also have their own
security context, which can be used for impersonating clients.
Q3. Explain Test and Set machine instructions.
The test-and-set instruction is an instruction used to write to a memory location and return its old value as a
single atomic (i.e., non-interruptible) operation. Typically, the value 1 is written to the memory location. If
multiple processes may access the same memory location, and if a process is currently performing a testand-set, no other process may begin another test-and-set until the first process is done.
function Lock(boolean *lock)
{
while (test_and_set(lock) == 1);
}
Q5. Explain the Readers-Writers problem.
Two kinds of processes -- readers and writers -- share a database. Readers execute transactions that examine database
records; writer transactions both examine and update the database. The database is assumed initially to be in a
consistent state (i.e., one in which relations between data are meaningful). Each transaction, if executed in isolation,
transforms the database from one consistent state to another. To preclude interference between transactions, a writer
process must have exclusive access to the database. Assuming no writer is accessing the database, any number of
readers may concurrently execute transactions.
SECTION – B
(5 × 1 = 5)
Attempt any one question:
Q1. What is deadlock? List the necessary conditions for occurrence of deadlock.
A condition that occurs when two processes are each waiting for the other to complete before proceeding. The
result is that both processes hang. Deadlocks occur most commonly
in multitaskingand client/server environments. Ideally, the programs that are deadlocked, or
the operating system, should resolve the deadlock, but this doesn't always happen.
Deadlock Conditions
1. mutual exclusion
The resources involved must be unshareable; otherwise, the processes would not be prevented from
using the resource when necessary.
2. hold and wait or partial allocation
The processes must hold the resources they have already been allocated while waiting for other
(requested) resources. If the process had to release its resources when a new resource or resources
were requested, deadlock could not occur because the process would not prevent others from using
resources that it controlled.
3. no pre-emption
The processes must not have resources taken away while that resource is being used. Otherwise,
deadlock could not occur since the operating system could simply take enough resources from
running processes to enable any process to finish.
4. resource waiting or circular wait
A circular chain of processes, with each process holding resources which are currently being
requested by the next process in the chain, cannot exist. If it does, the cycle theorem (which states
that "a cycle in the resource graph is necessary for deadlock to occur") indicated that deadlock could
.
occur
Q2. Explain multithreading. Also explain the various multithreading models.
The word multithreading can be translated as multiple threads of control or multiple flows of control. While a traditional UNIX process always
has contained and still does contain a single thread of control, multithreading (MT) separates a process into many execution threads, each of
which runs independently.
Multithreading your code can




Improve application responsiveness
Use multiprocessors more efficiently
Improve program structure
Use fewer system resources
Threads are implemented in following two ways





User Level Threads -- User managed threads
Kernel Level Threads -- Operating System managed threads acting on kernel, an operating system core.
Many to many relationship.
Many to one relationship.
One to one relationship.
SECTION – C
(10 × 1 = 10)
Attempt any one question:
Q1. Consider a system with five processes and three resource types and at time T0 the
following snapshot of the system has been taken:
Allocated
Maximum
Available
Process Id R1 R2
R3
R1 R2 R3
R1 R2 R3
P1
1
1
2
4
3
3
3
1
0
P2
2
1
2
3
2
2
P3
4
0
1
9
0
2
P4
0
2
0
7
5
3
P5
1
1
2
11
2
3
i) Determine the total amount of resources of each type.
ii) Compute the need matrix.
iii) Determine if the state is safe or not using Banker’s algorithm.
iv) If the request from P2 arrives for( 3 , 3, 1),can the request be granted immediately?
Now suppose, P1 requests an additional instance of A and 2 more instances of type C.
request[1] = (1,0,2)
1. check if request[1] <= need[i] (yes)
2. check if request[1] <= available[i] (yes)
3. do pretend updates to the state
Process Allocation
Max
Need
Available
A B C A B C A B C A B C
P0
0
1
0
7
5
3
7
4
3
P1
3
0
2
3
2
2
0
2
0
P2
3
0
2
9
0
2
6
0
0
P3
2
1
1
2
2
2
0
1
1
3
3
2
P4
0 0 2 4 3 3 4 3 1
Is the system in a safe state? If so, which sequence satisfies the safety criteria?
<P1, P3, P4, P0, P2>
Hence, we immediately grant the request.
Q2. What are the requirements of a solution to critical section? State and explain
Producer-Consumer problem with is suitable solution.
1.
mutual exclusion: if process
is executing in its critical section, no other process is executing
in its critical section
2. progress: if no process is executing in its critical section and there exists some processes that wish
to enter their critical sections, then only those processes that are not executing in their remainder
section can participate in the decision of which will enter its critical section next, and this decision
cannot be postponed indefinitely
o if no process is in critical section, can decide quickly who enters
o only one process can enter the critical section so in practice, others are put on the queue
3. bounded waiting: there must exist a bound on the number of times that other processes are
allowed to enter their critical sections after a process has made a request to enter its critical
section and before that request is granted
o The wait is the time from when a process makes a request to enter its critical section
until that request is granted
o in practice, once a process enters its critical section, it does not get another turn until a
waiting process gets a turn (managed as a queue)
4. CONCEPT: Producers produce items to be stored in the buffer. Consumers remove and consume
items which have been stored. Mutual exclusion must be enforced on the buffer itself. Moreover,
producers can store only when there is an empty slot, and consumers can remove only when there
is a full slot.
Three semaphores are used. The binary semaphore mutex controlls access to the buffer itself. The
counting semaphore empty keeps track of empty slots, and the counting semaphore full keeps track
of full slots.
In this example, the buffer is implemented as an array of size MAX treated as a circular (ring) buffer.
Variables in and out give the index of the next position for putting in and taking out (if any).
Variable countgives the number of items in the buffer.
PRODUCER :
anytype item;
repeat {
/* produce something */
item = produce();
/* wait for an empty space */
P(empty);
/* store the item */
P(mutex);
buffer[in] = item;
in = in + 1 mod MAX;
count = count + 1;
V(mutex);
/* report the new full slot */
V(full);
} until done;
CONSUMER:
anytype item;
repeat {
/* wait for a stored item */
P(full);
/* remove the item */
P(mutex);
item = buffer[out];
out = out + 1 mod MAX;
count = count - 1;
V(mutex);
/* report the new empty slot */
V(empty);
/* consume it */
consume(item);
} until done
*********