Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- typedef struct{
- int max[100];
- int allocation[100];
- int need[100];
- int finish;
- }Process;
- int n,m,ans[100],front=-1,rear=-1;
- void enqueue(int item){
- if(front==-1){
- front++;
- }
- ans[++rear] = item;
- }
- int dequeue(){
- int item = ans[front];
- if(front==rear) {
- rear = -1;
- front = -1;
- }
- else
- front++;
- return item;
- }
- void getInstanceDetails(int t[]){
- for (int i = 0; i < m; ++i) {
- printf("\nEnter total instance of resource %d:",i+1);
- scanf("%d",&t[i]);
- }
- }
- void getProcessDetails(Process p[]){
- for (int i = 0; i < n; ++i) {
- printf("\nEnter Details of Process P%d:\n",i);
- for (int j = 0; j < m; ++j) {
- printf("Max of Instance %d:",j+1);
- scanf("%d",&p[i].max[j]);
- }
- printf("\n");
- for (int j = 0; j < m; ++j) {
- printf("Allocation of Instance %d",j+1);
- scanf("%d",&p[i].allocation[j]);
- p[i].need[j] = p[i].max[j] - p[i].allocation[j];
- }
- p[i].finish = 0;
- }
- }
- void displayTable(Process p[]){
- for (int i = 0; i < 4; ++i) {
- printf("+++++++++++++++");
- }
- printf("\n| Process | Max | Allocation | Need |\n");
- for (int i = 0; i < n; ++i) {
- printf("|P%-10d|",i);
- for (int j = 0; j < m; ++j) {
- printf("%-5d",p[i].max[j]);
- }
- printf("|");
- for (int j = 0; j < m; ++j) {
- printf("%-5d",p[i].allocation[j]);
- }
- printf("|");
- for (int j = 0; j < m; ++j) {
- printf("%-4d",p[i].need[j]);
- }
- printf("|\n");
- if (i<m-1) {
- for (int j = 0; j < 4; ++j) {
- printf("---------------");
- }
- }
- printf("\n");
- }
- for (int i = 0; i < 4; ++i) {
- printf("+++++++++++++++");
- }
- }
- void findAvail(int a[], int t[], Process p[]){
- printf("\nAvailable: { ");
- for (int i = 0; i < m; ++i) {
- int temp=0;
- for (int j = 0; j < n; ++j) {
- temp += p[j].allocation[i];
- }
- a[i] = t[i] - temp;
- printf("%d ",a[i]);
- }
- printf("}\n");
- }
- void bankersAlgorithm(Process process[], int totalInstance[]){
- int availableInstance[m];
- findAvail(availableInstance, totalInstance, process);
- int prev_rear;
- while (1){
- for (int i = 0; i < n; ++i) {
- prev_rear = rear;
- if(!process[i].finish){
- for (int j = 0; j < m; ++j) {
- if(process[i].need[j] > availableInstance[j]){
- process[i].finish=0;
- break;
- }
- process[i].finish = 1;
- }
- if(process[i].finish){
- enqueue(i);
- for (int j = 0; j < m; ++j) {
- availableInstance[j] += process[i].allocation[j];
- }
- }
- }
- }
- if(rear==n-1){
- printf("\nThe Safe Sequence is: { ");
- for (int j = 0; j < n-1; ++j) {
- printf("P%d, ",dequeue());
- }
- printf("P%d }\n",dequeue());
- break;
- }
- else if(prev_rear==rear) {
- printf("\nThe system is not Safe!\n");
- break;
- }
- }
- }
- int main(){
- printf("Enter no. of resources:");
- scanf("%d",&m);
- int totalInstance[m];
- printf("Enter no. of processes:");
- scanf("%d",&n);
- Process process[n];
- getInstanceDetails(totalInstance);
- getProcessDetails(process);
- displayTable(process);
- bankersAlgorithm(process, totalInstance);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement