Deadlock creation in C Linux (Operating System):-
P1 is with resource 10-20 p2 is with resource 40-60
now p1 is waiting for the resources of p2
and p2 is waiting for resources of p1
program1:-
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<stdio.h>
int main()
{
int fd,pt;
struct flock r1,r2;
fd=open("myfile22",O_CREAT|O_RDWR,0666);
r1.l_type=F_WRLCK;
r1.l_whence=SEEK_SET;
r1.l_start=10;
r1.l_len=20;
pt=fcntl(fd,F_SETLKW,&r1);
if(pt==-1)
{
printf("Error in locking region1 in p1\n");
}
else
{
printf("Resource allocated in p1 10-30\n");
}
sleep(5);
r2.l_type=F_WRLCK;
r2.l_whence=SEEK_SET;
r2.l_start=40;
r2.l_len=20;
pt=-1;
while(pt==-1)
{
pt=fcntl(fd,F_SETLK,&r2);
if(pt==-1)
{
printf(" waiting for resource(40 -60) in P1\n");
sleep(3);
}
else
{
printf("Region2 Locked in p1 40-60\n");
}
}
sleep(10);
}
program2:-
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<stdio.h>
int main()
{
int fd,pt;
struct flock r1,r2;
fd=open("myfile22",O_CREAT|O_RDWR,0666);
r1.l_type=F_WRLCK;
r1.l_whence=SEEK_SET;
r1.l_start=40;
r1.l_len=20;
pt=fcntl(fd,F_SETLK,&r1);
if(pt==-1)
{
printf("Error in locking region1 in p2\n");
}
else
{
printf("Rescource allocated1 in p2 40 - 60\n");
}sleep(5);
r2.l_type=F_WRLCK;
r2.l_whence=SEEK_SET;
r2.l_start=10;
r2.l_len=20;
pt=-1;
while(pt==-1)
{
pt=fcntl(fd,F_SETLK,&r2);
if(pt==-1)
{
printf(" waiting for resource(10-30) in P2\n"); sleep(3);
}
else
{
printf("Region2Locked in p2 10 - 30\n");
}
}
sleep(20);
}
A deadlock is a situation in which two or more
competing actions are each waiting for the other to finish, and thus neither
ever does.
Conditions for Deadlock:-
·
Mutual Exclusion: At
least one resource must be held in a non-sharable mode. Only one process
can use the resource at any given instant of time.
·
Hold and Wait or Resource
Holding: A process is currently holding at least one resource and
requesting additional resources which are being held by other processes.
·
No Preemption: The
operating system must not de-allocate resources once they have been allocated;
they must be released by the holding process voluntarily.
·
Circular Wait: A
process must be waiting for a resource which is being held by another process,
which in turn is waiting for the first process to release the resource. In
general, there is a set of
waiting processes, P = {P1, P2, ..., PN}, such
that P1 is waiting for a resource held by P2, P2 is
waiting for a resource held by P3 and so on until PN is
waiting for a resource held by P1.
P1 is with resource 10-20 p2 is with resource 40-60
now p1 is waiting for the resources of p2
and p2 is waiting for resources of p1
program1:-
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<stdio.h>
int main()
{
int fd,pt;
struct flock r1,r2;
fd=open("myfile22",O_CREAT|O_RDWR,0666);
r1.l_type=F_WRLCK;
r1.l_whence=SEEK_SET;
r1.l_start=10;
r1.l_len=20;
pt=fcntl(fd,F_SETLKW,&r1);
if(pt==-1)
{
printf("Error in locking region1 in p1\n");
}
else
{
printf("Resource allocated in p1 10-30\n");
}
sleep(5);
r2.l_type=F_WRLCK;
r2.l_whence=SEEK_SET;
r2.l_start=40;
r2.l_len=20;
pt=-1;
while(pt==-1)
{
pt=fcntl(fd,F_SETLK,&r2);
if(pt==-1)
{
printf(" waiting for resource(40 -60) in P1\n");
sleep(3);
}
else
{
printf("Region2 Locked in p1 40-60\n");
}
}
sleep(10);
}
program2:-
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<stdio.h>
int main()
{
int fd,pt;
struct flock r1,r2;
fd=open("myfile22",O_CREAT|O_RDWR,0666);
r1.l_type=F_WRLCK;
r1.l_whence=SEEK_SET;
r1.l_start=40;
r1.l_len=20;
pt=fcntl(fd,F_SETLK,&r1);
if(pt==-1)
{
printf("Error in locking region1 in p2\n");
}
else
{
printf("Rescource allocated1 in p2 40 - 60\n");
}sleep(5);
r2.l_type=F_WRLCK;
r2.l_whence=SEEK_SET;
r2.l_start=10;
r2.l_len=20;
pt=-1;
while(pt==-1)
{
pt=fcntl(fd,F_SETLK,&r2);
if(pt==-1)
{
printf(" waiting for resource(10-30) in P2\n"); sleep(3);
}
else
{
printf("Region2Locked in p2 10 - 30\n");
}
}
sleep(20);
}
output:-
Comments