You are on page 1of 27

OS Experiment - 3

OS Experiment - 2
OS Experiment - 4
OS Experiment - 5
OS Experiment - 7

#include <stdio.h>

int main() {

// Initialize variables

int n, m, i, j;

printf("Enter number of processes: ");

scanf("%d", &n);

printf("Enter number of resources: ");

scanf("%d", &m);

int alloc[n][m], max[n][m], avail[m], need[n][m], finish[n], safeSeq[n], work[m];

// Input the allocation matrix

printf("Enter the allocation matrix:\n");

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

for (j = 0; j < m; j++) {

scanf("%d", &alloc[i][j]);

// Input the max matrix

printf("Enter the max matrix:\n");

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

for (j = 0; j < m; j++) {

scanf("%d", &max[i][j]);

}
// Input the available matrix

printf("Enter the available matrix:\n");

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

scanf("%d", &avail[i]);

// Calculate the need matrix

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

for (j = 0; j < m; j++) {

need[i][j] = max[i][j] - alloc[i][j];

// Initialize the finish and safeSeq arrays

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

finish[i] = 0;

safeSeq[i] = -1;

// Initialize the work array to the available resources

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

work[i] = avail[i];

// Find a safe sequence

int count = 0;

while (count < n) {

// Find an index i such that finish[i] = 0 and need[i] <= work

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

if (finish[i] == 0) {

int flag = 1;

for (j = 0; j < m; j++) {

if (need[i][j] > work[j]) {

flag = 0;

break;

if (flag) {

found = 1;

break;

// If no such i exists, the system is not in a safe state

if (!found) {

printf("System is not in safe state.\n");

return 0;

// Mark process i as finished and add it to the safe sequence

finish[i] = 1;

safeSeq[count] = i;

count++;

// Release the resources allocated to process i

for (j = 0; j < m; j++) {


work[j] += alloc[i][j];

// If we reach here, the system is in a safe state and we can output

printf("Safe sequence is: ");

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

printf("%d ", safeSeq[i]);

printf("\n");

return 0;

}
OS Experiment - 8

FOR FIRST FIT


#include <stdio.h>
int main() {
int bsize[10], psize[10], bno, pno, flags[10], allocation[10], i, j;
printf("Enter no. of blocks:");
scanf("%d", &bno);
printf("\nEnter size of each block:\n");
for (i = 0; i < bno; i++) {
scanf("%d", &bsize[i]);
flags[i] = 0;
}
printf("\nEnter no. of processes:");
scanf("%d", &pno);
printf("\nEnter size of each process:\n");
for (i = 0; i < pno; i++) {
scanf("%d", &psize[i]);
}
for (i = 0; i < pno; i++) {
for (j = 0; j < bno; j++) {
if (flags[j] == 0 && bsize[j] >= psize[i]) {
allocation[i] = j;
flags[j] = 1;
break;
}
}
}
printf("\nBlock no.\tsize\t\tprocess no.\t\tsize\n");
for (i = 0; i < bno; i++) {
printf("%d\t\t%d\t\t", i+1, bsize[i]);
if (flags[i] == 1) {
printf("%d\t\t\t%d\n", allocation[i]+1, psize[allocation[i]]);
} else {
printf("Not allocated\n");
}
}
return 0;
}
For BEST FIT
#include <stdio.h>

int main() {
int part_size[20], process[20], flag[20];
int n, p, i, j, temp;

printf("Enter number of partitions: ");


scanf("%d", &n);

printf("Enter size of each partition:\n");


for (i = 0; i < n; i++) {
scanf("%d", &part_size[i]);
}
printf("Enter number of processes: ");
scanf("%d", &p);

printf("Enter size of each process:\n");


for (i = 0; i < p; i++) {
scanf("%d", &process[i]);
}

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


flag[i] = 0;
}

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


for (j = i + 1; j < n; j++) {
if (part_size[i] > part_size[j]) {
temp = part_size[i];
part_size[i] = part_size[j];
part_size[j] = temp;
}
}
}

printf("\nProcess\tPartition\tPartition Size\n");
for (i = 0; i < p; i++) {
for (j = 0; j < n; j++) {
if (part_size[j] >= process[i] &z& flag[j] == 0) {
flag[j] = 1;
printf("P%d\t%d\t\t%d\n", i, j, part_size[j]);
break;
FOR WORST FIT
#include <stdio.h>

int main() {
int part[20], process[20], flag[20];
int n, p, i, j, temp;
printf("Enter number of partitions: ");
scanf("%d", &n);

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


printf("Enter size of partition %d: ", i);
scanf("%d", &part[i]);
}
printf("Enter number of processes: ");
scanf("%d", &p);

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


printf("Enter size of process %d: ", i);
scanf("%d", &process[i]);
}

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


flag[i] = 0;
}
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (part[i] < part[j]) {
temp = part[i];
part[i] = part[j];
part[j] = temp;
}
}
}
printf("\nProcess\tPartition\tPartition Size\n");
for (i = 0; i < p; i++) {
for (j = 0; j < n; j++) {
if (part[j] >= process[i] && flag[j] == 0) {
flag[j] = 1;
printf("P%d\t%d\t\t%d\n", i, j, part[j]);
break;
}
}
if (j == n) {
printf("P%d\t--\t\t--\n", i);
}
} return 0;
OS Experiment - 9

FIFO:

LIFO:
LRU:

OPTIMAL:
OS Experiment - 10

FCFS:

SSTF:
SCAN:

C-SCAN:
LOOK:

C-LOOK:
OS Experiment - 6

You might also like