0% found this document useful (0 votes)
28 views4 pages

A1 Menudriven

The document contains a C program implementing the Banker's Algorithm for deadlock avoidance in operating systems. It includes functions for accepting process and resource data, checking system safety, handling resource requests, and displaying matrices. The program features a menu-driven interface for user interaction with options to accept data, check safety, request resources, and display current matrices.

Uploaded by

thirdeye353
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views4 pages

A1 Menudriven

The document contains a C program implementing the Banker's Algorithm for deadlock avoidance in operating systems. It includes functions for accepting process and resource data, checking system safety, handling resource requests, and displaying matrices. The program features a menu-driven interface for user interaction with options to accept data, check safety, request resources, and display current matrices.

Uploaded by

thirdeye353
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

// Bankers Algorithm

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int n, m, i, j;
int alloc[10][10], max[10][10], need[10][10];
int avail[10], work[10], req[10];
struct process
{
char name[10];
int flag;
}p[10];

void Accept()
{
printf("Enter the total number of processes:- \n");
scanf_s("%d", &n);
for (i = 0; i < n; i++)
{
printf("Enter Process name:");
gets(p[i].name);
}
printf("\n Enter the number of resources:-\n");
scanf_s("%d", &m);
printf("\ n Enter the allocation matrix:- \n");
for (i = 0; i < n; ++i)
{
for (j = 0; j < m; ++j)
scanf_s("%d", &alloc[i][j]);
}
printf(" \n Enter Max matrix:- \n");
for (i = 0; i < n; ++i)
{
for (j = 0; j < m; ++j)
scanf_s("%d", &max[i][j]);
}
printf("\n Need matrix \n");
for (i = 0; i < n; ++i)
{
for (j = 0; j < m; ++j)
{
need[i][j] = max[i][j] - alloc[i][j];
printf(" %d ", need[i][j]);
printf("\n");
}
}
printf("\n");
printf("\n Enter avaliable resources:- \n");
for (i = 0; i < m; i++)
scanf_s("%d", &avail[i]);
}
void safeseq()
{
int sseq[10], ss = 0, chk = 0, chki = 0, count = 0;
for (j = 0; j < m; ++j)
work[j] = avail[j];
for (i = 0; i < n; ++i)
p[i].flag = 0;
while (count != 5)
{
for (i = 0; i < n; ++i)
{
chk = 0;
for (j = 0; j < m; ++j)
{
if (p[i].flag == 0)
{
if (need[i][j] <= work[j]);
chk++;
}
if (chk == m)
{
for (j = 0; j < m; j++)
{
work[j] = work[j] + alloc[i][j];
printf("%d", work[j]);
p[i].flag = 1;
}
sseq[ss] = 1;
ss++;
count++;
}
}
}
}
for (i = 0; i < n; ++i)
{
if (p[i].flag == 1)
chki++;
}
if (chki >= n)
{
printf("\n System is in Safe State \n");
for (i = 0; i < n; ++i)
printf(" %d ", sseq[i]);
}
else
printf("\n Unsafe State \n");
}
void request()
{
int pro;
printf(" enter the process number of request \n");
scanf_s("%d", &pro);
printf(" enetr the request array \n");
for (i = 0; i < m; ++i);
scanf_s("%d", &req[i]);
for (j = 0; j < m; j++)
{
if (req[j] <= need[pro][j])
{
if (req[j] <= avail[j])
{
avail[j] = avail[j] - req[j];
alloc[pro][j] = alloc[pro][j] + req[j];
need[pro][j] = need[pro][j] - req[j];
printf(" avail: %d", avail[j]);
}
printf("\t need : %d", need[pro][j]);
}
else
{
printf("process has some kirik \n");
exit(0);
}
}
}
void print()
{
printf("\n Max matrix \n");
for (i = 0; i < n; ++i)
{
for (j = 0; j < m; ++j)
printf(" %d ", max[i][j]);
printf("\n");
}
printf("\n Allocation matrix \n");
for (i = 0; i < n; ++i)
{
for (j = 0; j < m; ++j)
printf(" %d ", alloc[i][j]);
printf("\n");
}
printf(" Need matrix \n");
for (i = 0; i < n; ++i)
{
for (j = 0; j < m; ++j)
printf(" %d ", need[i][j]);
printf("\n");
}
printf(" \n Availiable ");
for (i = 0; i < m; ++i)
printf(" %d ", avail[i]);
}
int main()
{
int ch;
do {
printf("\n Menu \n");
printf("\n 1.Accept");
printf("\n 2.Safey Algorithm ");
printf("\n3.Resource request Algorithm ");
printf("\n 4.Display ");
printf("\n 5.Exit");
printf("\n Enter your choice:- ");
scanf_s("%d", &ch);
switch (ch)
{
case 1: Accept();
break;
case 2: safeseq();
break;
case 3: request();
break;
case 4: print();
break;
case 5:break;
}
} while (ch != 5);
return 0;
}

You might also like