0% found this document useful (0 votes)
27 views

Oslab 34 45

Uploaded by

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

Oslab 34 45

Uploaded by

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

Ex no: 7

Date:
BANKERS ALGORITHM FOR DEADLOCK
AVOIDANCE.

AIM:
To write a C program to implement banker's algorithm for deadlock avoidance
ALGORITHM:
Step-1: Start the program.
Step-2: Declare the memory for the process.
Step-3: Read the number of process, resources, allocation matrix and available matrix.
Step-4: Compare each and every process using the banker's algorithm.
Step-5: If the process is in safe state then it is a not a deadlock process otherwise it is a
deadlock process.
Step-6: produce the result of state of process.
Step-7: Stop the program.
PROGRAM:
#include <stdio.h>
#include <conio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n, r;
void input();
void show();
void cal();
int main() {
printf("********** Banker's Algorithm **********\n");
input();
show();
cal();
getch();
return 0;
}
void input() {
int i, j;
printf("Enter the number of Processes: ");
scanf("%d", &n);
printf("Enter the number of resource instances: ");
scanf("%d", &r);

printf("Enter the Max Matrix\n");


for (i = 0; i< n; i++) {
for (j = 0; j < r; j++) {
scanf("%d", &max[i][j]);
}
}
printf("Enter the Allocation Matrix\n");
for (i = 0; i< n; i++) {
for (j = 0; j < r; j++) {
scanf("%d", &alloc[i][j]);
}
}
printf("Enter the Available Resources\n");
for (j = 0; j < r; j++) {
scanf("%d", &avail[j]);
}
}
void show() {
int i, j;
printf("Process\tAllocation\tMax\tAvailable\n");
for (i = 0; i< n; i++) {
printf("P%d\t", i + 1);
for (j = 0; j < r; j++) {
printf("%d ", alloc[i][j]);
}
printf("\t");
for (j = 0; j < r; j++) {
printf("%d ", max[i][j]);
}
printf("\t");
if (i == 0) {
for (j = 0; j < r; j++) {
printf("%d ", avail[j]);
}
}
printf("\n");
}
}
void cal() {
int finish[100], temp, flag = 1, k, c1 = 0;
int safe[100];
int i, j;
for (i = 0; i< n; i++) {
finish[i] = 0;
}
// Find need matrix
for (i = 0; i< n; i++) {
for (j = 0; j < r; j++) {
need[i][j] = max[i][j] - alloc[i][j];
}
}
printf("\n");
while (flag) {
flag = 0;
for (i = 0; i< n; i++) {
int c = 0;
for (j = 0; j < r; j++) {
if ((finish[i] == 0) && (need[i][j] <= avail[j])) {
c++;
}
}
if (c == r) {
for (k = 0; k < r; k++) {
avail[k] += alloc[i][j];
}
finish[i] = 1;
flag = 1;
printf("P%d->", i);
}
}
}
for (i = 0; i< n; i++) {
if (finish[i] == 1) {
c1++;
} else {
printf("P%d->", i);
}
}
if (c1 == n) {
printf("\nThe system is in a safe state\n");
} else {
printf("\nProcesses are in a deadlock\n");
printf("System is in an unsafe state\n");
}
}

RESULT:

Thus the C Program for implementation of banker's algorithm for deadlock avoidance
has been written and Executed Successfully.
Ex no: 8

Date:
ALGORITHMFORDEADLOCKDETECTION

AIM:
To write a C program to implement algorithm for deadlock detection
ALGORITHM:
Step-1: Start the program.
Step-2: Declare the memory for the process.
Step-3: Read the number of process, resources, allocation matrix and available matrix.
Step-4: Compare each and every process using the banker's algorithm.
Step-5: If the process is in safe state then it is a not a deadlock process otherwise it is a
deadlock process.
Step-6: produce the result of state of process.
Step-7: Stop the program.

PROGRAM:
#include <stdio.h>
#include <conio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n, r;
void input();
void show();
void cal();
int main() {
printf("****** Deadlock Detection Algorithm ******\n");
input();
show();
cal();
getch();
return 0;
}
void input() {
int i, j;
printf("Enter the number of Processes: ");
scanf("%d", &n);
printf("Enter the number of resource instances: ");
scanf("%d", &r);

printf("Enter the Max Matrix:\n");


for (i = 0; i< n; i++) {
for (j = 0; j < r; j++) {
scanf("%d", &max[i][j]);
}
}
printf("Enter the Allocation Matrix:\n");
for (i = 0; i< n; i++) {
for (j = 0; j < r; j++) {
scanf("%d", &alloc[i][j]);
}
}
printf("Enter the Available Resources:\n");
for (j = 0; j < r; j++) {
scanf("%d", &avail[j]);
}
}
void show() {
int i, j;
printf("Process\tAllocation\tMax\tAvailable\n");
for (i = 0; i< n; i++) {
printf("\nP%d\t", i + 1);
for (j = 0; j < r; j++) {
printf("%d ", alloc[i][j]);
}
printf("\t");
for (j = 0; j < r; j++) {
printf("%d ", max[i][j]);
}
printf("\t");
if (i == 0) {
for (j = 0; j < r; j++) {
printf("%d ", avail[j]);
}
}
}
}
void cal() {
int finish[100], flag = 1, k, c1 = 0;
int dead[100];
int i, j;
for (i = 0; i< n; i++) {
finish[i] = 0;
// Find need matrix
for (j = 0; j < r; j++) {
need[i][j] = max[i][j] - alloc[i][j];
}
}
while (flag) {
flag = 0;
for (i = 0; i< n; i++) {
int c = 0;
for (j = 0; j < r; j++) {
if ((finish[i] == 0) && (need[i][j] <= avail[j])) {
c++;
}
}
if (c == r) {
for (k = 0; k < r; k++) {
avail[k] += alloc[i][k];
}
finish[i] = 1;
flag = 1;
}
}
}
int flag_deadlock = 0;

for (i = 0; i< n; i++) {


if (finish[i] == 0) {
dead[flag_deadlock] = i;
flag_deadlock++;
flag = 1;
}
}
if (flag == 1) {
printf("\n\nSystem is in Deadlock and the Deadlocked processes are:\n");

for (i = 0; i<flag_deadlock; i++) {


printf("P%d ", dead[i] + 1);
}
printf("\n");
} else {
printf("\nNo Deadlock Detected.\n");
}
}

RESULT:

Thus the C Program for implementation of algorithm for deadlock detection has been
written and Executed Successfully.
Ex no: 9
THREADING&SYNCHONIZATIONAPPLICATIONS
Date:

AIM:
To write a c program to implement Threading and Synchronization Applications
ALGORITHM:
Step 1: Start the process.
Step 2: Declare process thread, thread-id.
Step 3: Read the process thread and thread state.
Step 4: Check the process thread equals to thread-id by using if condition.
Step 5: Check the error state of the thread.
Step 6: Display the completed thread process.
Step 7: Stop the program.
PROGRAM:
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
pthread_ttid[2];
void* doSomeThing(void *arg)
{
unsigned long i = 0;
pthread_t id = pthread_self();
if(pthread_equal(id,tid[0]))
{
printf("\n First thread processing\n");
}
else
{
printf("\n Second thread processing\n");
}
for(i=0; i<(0xFFFFFFFF);i++);
return NULL;
}
int main(void)
{
int i = 0;
int err;
while(i< 2)
{
err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);
if (err != 0)
printf("\ncan't create thread :[%s]", strerror(err));
else
printf("\n Thread created successfully\n");
i++;
}
sleep(5);
return 0;
}
RESULT:

Thus the C Program for implementation of Threading and Synchronization Applications


has been written and Executed Successfully.

You might also like