Sunteți pe pagina 1din 11

Program :Write a c program to implement deadlock detection alg

#include<stdio.h>
int main()
{
int r,ch,p,i,j,k,count=0,temp,flag;
int rv[10],rr[10],av[10],am[20][20],cm[20][20],a[20][20],fla[10];
printf("enter the maximum number of resources \n");
scanf("%d",&r);
printf("enter the resource vector");
for(i=0;i<r;i++)
scanf("%d",&rv[i]);
printf("enter the available vector:\n");
for(i=0;i<r;i++)
scanf("%d",&av[i]);
printf("enter the number of processes to be executed \n");
scanf("%d",&p);
for(i=0;i<p;i++)
fla[i]=0;
printf("enter the request matrix \n");
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
scanf("%d",&cm[i][j]);
}
printf("enter the allocate matrix \n");
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
scanf("%d",&am[i][j]);
}
for(i=0;i<p;i++)
{

flag=0;
for(j=0;j<r;j++)
{
if(cm[i][j]<=av[j])
flag++;
}

if(flag==r&&fla[i]==0)
{
fla[i]=1;

count++;
printf("process %d is exected \n",i+1);
temp=i;
for(k=0;k<r;k++)
{
av[k]=am[temp][k]+av[k];
cm[temp][k]=0;
}
printf("after executing process %d available
vector:",i+1);
for(k=0;k<r;k++)
printf("%5d",av[k]);
printf("\n");
}
if(count==p)
{
printf("deadlock is avoided \n");
exit(0);
}
}
printf("request is not granted \n");
printf("deadlock is detected \n");
return 0;
}
/*
enter the maximum number of resources
3
enter the resource vector7 2 6
enter the available vector:
0 0 0
enter the number of processes to be executed
5
enter the request matrix
0 0 0
2 0 2
0 0 1
1 0 0
0 0 2
enter the allocate matrix
0 1 0
2 0 0
3 0 3
2 1 1
0 0 2
process 1 is exected
after executing process 1 available vector: 0 1 0
request is not granted
deadlock is detected
*/
/*PROGRAM TO IMPLEMENT DEAD LOCK AVOIDANCE */

#include<stdio.h>
int main()
{
int r,ch,p,i,j,k,count,temp,flag;
int rv[10],rr[10],av[10],am[20][20],cm[20][20],a[20][20];
printf("enter the maximum no of resources\n");
scanf("%d",&r);
printf("enter the resource vector\n");
for(i=0;i<r;i++)
scanf("%d",&rv[i]);
printf("enter the available vector\n");
for(i=0;i<r;i++)
scanf("%d",&av[i]);
printf("enter the number of processes to be executed\n");
scanf("%d",&p);
printf("enter the claim matrix\n");
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
scanf("%d",&cm[i][j]);
}
printf("enter the allocate matrix\n");
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
scanf("%d",&am[i][j]);
}
count=0;
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
a[i][j]=cm[i][j]-am[i][j];
}
ch=0;
while(ch<=r)
{
for(i=0;i<p;i++)
{
flag=0;
for(j=0;j<r;j++)
{
if(a[i][j]<=av[j])
flag++;
}
if(flag==r)
{
count++;
printf("process %d is executed\n",i+1);
temp=i;
for(k=0;k<r;k++)
{
av[k]=am[temp][k]+av[k];
cm[temp][k]=0;
}
printf("after executing the process %d the available matrix
is\n",i+1);
for(k=0;k<r;k++)

printf("%5d",av[k]);
printf("\n");
}
if(count==p)
{

printf("deadlock is avoided\n");
printf("without using the resource
required\n");
printf("now state of system is safe
system\n");
exit(0);
}

}
}
for(i=0;i<r;i++)
{
if(av[i]==rv[i])
ch=ch+1;
}
printf("request is not granted\n");
printf("since after granting the req also atate of the system
is");
printf("unsafe state\n");
printf("deadlock cant be avoided\n");
return(0);
}

/***********************************************************
OUTPUT

mum no of resources
3
enter the resource vector
10 5 7
enter the available vector
3 3 2
enter the number of processes to be executed
5
enter the claim matrix
7 5 3
3 2 2
9 0 2
2 2 2
4 3 3
enter the allocate matrix
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
process 2 is executed
after executing the process 2 the available matrix is
5 3 2
process 4 is executed
after executing the process 4 the available matrix is
7 4 3
process 5 is executed
after executing the process 5 the available matrix is
7 4 5
process 1 is executed
after executing the process 1 the available matrix is
7 5 5
process 2 is executed
after executing the process 2 the available matrix is
9 5 5
deadlock is avoided
without using the resource required
now state of system is safe system

************************************************************************
*/
*/ C program to implement Least Recently Used algorithm */

#include<stdio.h>

char ref[20];
struct non_frame
{
char pno;
int count;
}frames[10];
int n;

int lru() //function to send page number with les count value
{
int a,min=frames[0].count,p=0;
for(a=0;a<n;a++)
{
if(frames[a].count<min)//comparing count wit min
{
min=frames[a].count;
p=a;
}
}
return(p);//returning the count
}

int present(int i)//check whetehr the page is present in table


{
int j=0;
while(j<=n)
{
if(ref[i]==frames[j].pno)//if present
{
frames[j].count=i;
return(1);
}
j++;
}
return(0);
}

void print()//to display


{
int j;
for(j=0;j<n;j++)
{
printf("%3c",frames[j].pno);
}
printf("\n");
}

int main()
{
int i,j,k,pf=0;
printf("Enter the reference string without spaces:");
scanf("%s",ref);//reference srting
printf("enter the no of frames");
scanf("%d",&n);
printf("pages in mainmemory after each page is refered \n");
j=0;
for(k=0;k<n;)
{
if(present(j)!=1)
{
pf++;
frames[k].pno=ref[j];
frames[k].count=j;
k++;
print();
}
j++;
if(ref[j]=='\0')
break;
}
k=0;//for indexin into frames
for(i=j;ref[i]!='\0';i++)
{
if(present(i)==0)
{
k=lru();
pf++;// if not present inc.page fault
frames[k].pno=ref[i];
frames[k].count=i;
}//end of if
print();
}//end of for
printf("no of page faults=%d\n",pf);
return(0);
}//end of main
/************************output********************
[y7cse26@cvr oslab]$ ./a.out
Enter the reference string without spaces:1111111111
enter the no of frames3
pages in mainmemory after each page is refered
1
no of page faults=1
[y7cse26@cvr oslab]$ ./a.out
Enter the reference string without spaces:1245872
enter the no of frames4
pages in mainmemory after each page is refered
1
1 2
1 2 4
1 2 4 5
8 2 4 5
8 7 4 5
8 7 2 5
no of page faults=7
***************************************************/
/*Prog for MFT(multiple fragmentations of fixed frames.)*/
#include<stdio.h>
struct memory
{
int id,ifrag,status;
}m[20];
int n,s;//no of partitions,s-size of the fragment.
void allocate()
{
int j,l,pname,psize;
//Check whether the memory is available.
for(j=1;j<=n;j++)
if(m[j].status==0)
break;
if(j>n)
{
printf("No space available");
return;
}

printf("Enter process id\n");


scanf("%d",&pname);
printf("Enter process size\n");
scanf("%d",&psize);
if(psize<s)//if size of the process<partition size
{
m[j].id=pname;
m[j].ifrag=s-psize;
m[j].status=1;
return;
}
else //when partition size<process size.
{
int i,count,partreq;
if(psize%s!=0)
partreq=(psize/s)+1;
else if(psize%s==0)
partreq=psize/s;
/*Check whether available space is sufficient or not.*/
for(l=0;l<=n;)
{
count=0;
while(count<partreq)
{
if(m[l].status==0)
{
//check for contigious memory.
l++;
count++;
}
else
break;
}
if(count==partreq)
break;
else
l++;

}
if(count==partreq)
{
//if enough space is available then allocate
int space=psize,num,temp;
for(num=1;num<=partreq;num++)
{
m[j].id=pname;
m[j].status=1;
temp=space;
space=space-s;
if(space>0)//occupies 1 complete partition
m[j].ifrag=0;
else//occupies part of last partition.
{
m[j].ifrag=s-temp;
break;
}
j++;
}
return;
}//if colse
printf("Enough space is not available.\n");
return;

}//else close.

}//allocate() close.
void deallocate()
{
int pid,j;
printf("Enter the process id.\n");
scanf("%d",&pid);
for(j=1;j<=n;j++)
{
if(m[j].id==pid)
{
m[j].id=0;
m[j].status=0;
m[j].ifrag=0;
if(m[j+1].id!=pid)
{
printf("Deallocated.\n");
return;
}
}
}
if(j>n)
printf("Not found.\n");
return;
}
void printpic()
{
int j;
printf("partition\tallocateto\tIF\n");
for(j=1;j<=n;j++)
printf("%5d\t\t\t%d\t%d\n",j,m[j].id,m[j].ifrag);
return;
}
int main()
{
int ch,j;
printf("Enter no. of partitions.\n");
scanf("%d",&n);
printf("Enter partition size");
scanf("%d",&s);
for(j=1;j<=n;j++)
{
m[j].id=0;
m[j].status=0;
m[j].ifrag=0;
}
do
{
printf("1.Allocate\n2.Deallocate\n3.page memory
picture\n4.exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:allocate();
printpic();
break;
case 2:deallocate();
printpic();
break;
case 3:printpic();
break;
default:break;
}
}while(ch<4);
return 0;
}//End of main.
/*output:
Enter no. of partitions.
10
Enter partition size20
1.Allocate
2.Deallocate
3.page memory picture
4.exit
1
Enter process id
1
Enter process size
7
partition allocateto IF
1 1 13
2 0 0
3 0 0
4 0 0
5 0 0
6 0 0
7 0 0
8 0 0
9 0 0
10 0 0
1.Allocate
2.Deallocate
3.page memory picture
4.exit*/

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