20-04-2011, 03:47 PM
Presented by:
Shabaaz N Syed
Vedprakash Anad
Copy of osreport.doc (Size: 107.5 KB / Downloads: 47)
SAFETY ALGORITHM
Theory:
The safety algorithm is run by the operating system whenever a process requests resources. The algorithm avoids deadlock by denying or postponing the request if it determines that accepting the request could put the system in an unsafe state (one where deadlock could occur). When a new process enters a system, it must declare the maximum number of instances of each resource type that may not exceed the total number of resources in the system. Also, when a process gets all its requested resources it must return them in a finite amount of time.
Resources
For the safety algorithm to work, it needs to know three things:
• How much of each resource each process could possibly request.
• How much of each resource each process is currently holding.
Resources may be allocated to a process only if it satisfies the following conditions:
1. request ≤ max, else set error condition as process has crossed maximum claim made by it.
2. request ≤ available, else process waits until resources are available.
Some of the resources that are tracked in real systems are memory, semaphores and interface access
PROGRAM
#include<stdio.h>
#include<conio.h>
int i,j,reso,proc,resoinst[20],alloc[20][20],max[20][20],avail[20],need[20][20];
int safeflg=0,safeseq[20]; //GLOBAL VARIABLES
void printreso(int proc) // FUNCTION USED TO PRINT FORMAT TABLE
{
for(i=1;i<=proc;i++)
{
for(j=65;j<=64+reso;j++)
printf("%c ",j);
for(j=1;j<=reso*proc/2;j++)
printf(" ");
}
}
void availability(void) //FUNCTION USED TO CHECK AVAILABILTY OF RESOURCES
{ //FUNCTION CAN BE FURTHUR USED TO FIND SAFE SEQUENCE
int temp=0;
for(i=1;i<=reso;i++)
{
for(j=1;j<=proc;j++)
temp=temp+alloc[j][i];
avail[i]=resoinst[i]-temp;
temp=0;
}
}
void main(void) //START OF MAIN PROGRAM
{
clrscr();
printf("ENTER TOTAL NOS OF PROCESSES & RESOURES: ");
scanf("%d %d",&proc,&reso); //READING PROCESSES & RESOURCES
printf("\nENTER RESOURCE INSTANCES\n");
for(i=65;i<65+reso;i++)
{
printf("RESOURCE %c",i);
scanf("%d",&resoinst[i-64]);
}
clrscr();
printf("PROCESS");
for(i=1;i<=reso;i++)
printf(" ");
printf("ALLOCTION");
for(i=1;i<=reso;i++)
printf(" "); // PRINTING TABLE IN FORMAT
printf("MAX");
for(i=1;i<=reso;i++)
printf(" ");
printf("\n ");
printreso(2);
for(i=1;i<=proc;i++)
{
printf("\nP%d ",i-1);
for(j=1;j<=reso;j++)
scanf("%d",&alloc[i][j]); // READING RESOURCE DATA
for(j=1;j<=reso;j++)
scanf("%d",&max[i][j]);
}
printf("\nAVAILABLE\n");
printreso(1); printf("\n");
availability();
for(i=1;i<=reso;i++)
printf("%d ",avail[i]);
printf("\n\nNEED\n ");
printreso(1);
for(i=1;i<=proc;i++)
for(j=1;j<=reso;j++)
need[i][j]=max[i][j]-alloc[i][j]; // CALCULATING THE NEED
for(i=1;i<=proc;i++)
{
printf("\nP%d ",i);
for(j=1;j<=reso;j++)
printf("%d ",need[i][j]);
}
for(i=1;i<=proc;i++)
{
for(j=1;j<=reso;j++)
{
if(need[i][j]<=avail[j]);
else
safeflg=1; // SAFE FLAG SETS IF SYSTEM IS UNSAFE
}
}
getch();
clrscr();
if(safeflg==1)
printf("\n\nSYSTEM NOT IN SAFE STATE");
else
printf("\n\nSYSTEM IN SAFE STATE");
getch();
}