Documente Academic
Documente Profesional
Documente Cultură
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;
}
.
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.