Sunteți pe pagina 1din 3

Deadlock

A process requests resources, if those are not available at that time; a process
enters into the wait state. It may happen that waiting processes will never change
the state again, because resources requested by the process is occupied by some
other process. This is known as deadlock.
Example:
In multiprogramming system, suppose two processes are there and each
want to print a very large file. Process A requests permission to use the printer and is
granted. Process B then requests permission to use the tape drive and is also granted.
Now A asks for the tape drive, B asks for the printer. At this point both processes are
blocked and will remain so forever. This situation is called a deadlock.

Conditions of Deadlock:
Mutual Exclusion: At least one resource is held in a non sharable mode; that is
only one process at a time can use the resource. If another process requests that
resource, the requesting process must be delayed until the resource has been
released. Each resource is either currently assigned to exactly one process or is
available.
Pseudocode Mutual Exclusion:-
Boolean in1=false, in2=false;
Process P1::
while(1){
while(in2){
in1=true;
}
}
Process P2::
while(1){
in2=true;
while(in1)
{
in2=true;
}
in1=false;
}

 Hold and Wait: There must exist a process that is holding at least one resource
and is waiting to acquire additional resources that are currently being held by
another process. Process currently holding resources granted earlier can request
new resources.
int p[2], retval;
retval = pipe(p);
if (retval < 0) error("pipe");
retval=fork();
if (retval < 0) error("forking");
if (retval==0) { /* child */
dup2(p[1],1); /* redirect stdout to pipe */
close(p[0]); /* don't permit this
process to read from pipe */
execl("/bin/ls","ls","-l",NULL);
error("Exec of ls");
}
/* if we get here, we are the parent */
dup2(p[0],0); /* redirect stdin to pipe */
close(p[1]); /* don't permit this
process to write to pipe */
execl("/bin/more","more",NULL);
error("Exec of more");
return 0;
}

 No Pre-emption: Resources cannot be pre-empted; i.e. resource can only be


released voluntarily by the process holding it, after the process has completed its
task. Resources previously granted cannot be forcibly taken away from the
process. They must be explicitly released by the process holding them.
A request from process P for resources R1, R2, ..., Rn
f process P currently has any resources, then
refuse the request
else if any resource R1, ... Rn, does not exist, then
refuse the request
else
{
if any resource R1, ... Rn is not free, then
wait until all resources R1, ... Rn are free
end if
grant process P exclusive access to resources R1, ... Rn
}
end if
If a process P wants to request resources while holding resources, they follow these
steps:

 P frees all resources being held


 P requests all resources previously held plus the new resources it wants to
acquire

.
 Circular Wait: There exist a set (P0, P1... Pn) of waiting processes such that P0
is waiting for a resource which is held by P1, P1 is waiting for resource which is
held by P2. Pn – 1 is waiting for resources which are held by Pn and Pn is
waiting for a resource which is held by P0. Thus there must be a circular chain of
two or more processes, each of which is waiting for a resource held by the next
member of the chain.

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