Last active
December 4, 2016 09:19
-
-
Save skymansandy/2358bfd615e2b702717ef50f28ccec2b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
struct process | |
{ | |
int all[6],max[6],need[6],finished; | |
}p[10]; | |
int avail[6],sseq[10],ss=0,check1=0,check2=0,n,work[6]; | |
int nor; | |
int safeseq(void); | |
int main() | |
{ | |
int ch,i=0,j=0,k,pid,ch1; | |
do | |
{ | |
printf("\n\n\t 1. Input"); | |
printf("\n\n\t 2. Safe State or Not"); | |
printf("\n\n\t 3. print"); | |
printf("\n\n\t 4. Exit"); | |
printf("\n\n\t Enter ur choice :"); | |
scanf("%d",&ch); | |
switch(ch) | |
{ | |
case 1: | |
printf("\n\n\t Enter number of processes : "); | |
scanf("%d",&n); | |
printf("\n\n\t Enter the Number of Resources : "); | |
scanf("%d",&nor); | |
printf("\n\n\t Enter the Available Resouces : "); | |
for(j=0;j<n;j++) | |
{ | |
for(k=0;k<nor;k++) | |
{ | |
if(j==0) | |
{ | |
printf("\n\n\t For Resource type %d : ",k); | |
scanf("%d",&avail[k]); | |
} | |
p[j].max[k]=0; | |
p[j].all[k]=0; | |
p[j].need[k]=0; | |
p[j].finished=0; | |
} | |
} | |
for(i=0;i<n;i++) | |
{ | |
printf("\n\n\t Enter Max and Allocated resources for P %d : ",i); | |
for(j=0;j<nor;j++) | |
{ | |
printf("\n\n\t Enter the Max of resource %d : ",j); | |
scanf("%d",&p[i].max[j]); | |
printf("\n\n\t Allocation of resource %d :",j); | |
scanf("%d",&p[i].all[j]); | |
if(p[i].all[j]>p[i].max[j]) | |
{ | |
printf("\n\n\t Allocation should be less < or == max"); | |
j--; | |
} | |
else | |
p[i].need[j]=p[i].max[j]-p[i].all[j]; | |
avail[j]=avail[j]-p[i].all[j]; | |
} | |
} | |
break; | |
case 2: | |
if(safeseq()==1) | |
printf("\n\n\t The System is in safe state "); | |
else | |
printf("\n\n\t The System is Not in safe state "); | |
break; | |
case 3: | |
printf("\n\n\t Number of processes : %d",n); | |
printf("\n\n\t Number of Resources : %d",nor); | |
printf("\n\n\t Pid \t Max \t Allocated \t Need "); | |
for(i=0;i<n;i++) | |
{ | |
printf("\n\n\t P%d : ",i); | |
for(j=0;j<nor;j++) | |
printf(" %d",p[i].max[j]); | |
printf("\t"); | |
for(j=0;j<nor;j++) | |
printf("%d",p[i].all[j]); | |
printf("\t"); | |
for(j=0;j<nor;j++) | |
printf("%d",p[i].need[j]); | |
} | |
printf("\n\n\t Available :"); | |
for(i=0;i<nor;i++) | |
printf("%d",avail[i]); | |
break; | |
case 4: | |
break; | |
} | |
//getch(); | |
}while(ch!=5); | |
} | |
int safeseq() | |
{ | |
int tj,tk,i,j,k; | |
ss=0; | |
for(j=0;j<nor;j++) | |
work[j]=avail[j]; | |
for(j=0;j<n;j++) | |
p[j].finished=0; | |
for( tk=0;tk<nor;tk++) | |
{ | |
for(j=0;j<n;j++) | |
{ | |
if(p[j].finished==0) | |
{ | |
check1=0; | |
for(k=0;k<nor;k++) | |
if(p[j].need[k]<=work[k]) | |
check1++; | |
if(check1==nor) | |
{ | |
for(k=0;k<nor;k++) | |
{ | |
work[k]=work[k]+p[j].all[k]; | |
p[j].finished=1; | |
} | |
sseq[ss]=j; | |
ss++; | |
} | |
} | |
} | |
} | |
check2=0; | |
for(i=0;i<n;i++) | |
if(p[i].finished==1) | |
check2++; | |
printf("\n\n\t"); | |
if(check2>=n) | |
{ | |
printf("\n\n\t The system is in safe state"); | |
for( tj=0;tj<n;tj++) | |
printf("P%d,",sseq[tj]); | |
return 1; | |
} | |
else | |
printf("\n\n\t The system is Not in safe state"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment