Copy Chette Os Lab
Copy Chette Os Lab
h>
#include<stdlib.h>
int wait(int s)
{ return (--s);}
int signal(int s)
{return (++s);}
void producer(){
mutex = wait(mutex);
full = signal(full);
empty = wait(empty);
x++;
mutex = signal(mutex);}
void consumer(){
mutex = wait(mutex);
full = wait(full);
empty = signal(empty);
i++;
full = wait(full);
empty = signal(empty);}
mutex = signal(mutex);}
int main(){
int n;
while (1){
producer();
else
printf("Buffer is Full!!");
break;
case 2:
consumer();
else
printf("Buffer is empty!!");
break;
case 3:
exit(0);
break;
}}return 0;}
Priority schedule
#include<stdio.h>
#include<stdlib.h>
int main() {
int i, j, n;
scanf("%d", &n);
scanf("%d", &bt[i]);
printf("Priority: ");
scanf("%d", &p[i]); }
temp1 = bt[i];
bt[i] = bt[j];
bt[j] = temp1;
temp2 = p[i];
p[i] = p[j];
p[j] = temp2;}}}// Calculate completion time, turnaround time, and waiting time
compt[0] = bt[0];
wt[0] = 0;
tat[i] = compt[i];
sumtat += tat[i];
sumwt += wt[i];}
return 0;}
#include <stdio.h>
FCFS CODE #include <stdlib.h>
int main() {
int i, n;
int bt[10];
scanf("%d", &n);
scanf("%d", &bt[i]);}
printf("\n FCFS\n");
sum = 0;
twt = 0;
ttat = bt[0];
wt = sum;
twt += wt;
ttat += tat;
printf("\n Process = %d \t Waiting Time = %d \t Turn Around Time = %d", i + 1, wt, tat);
printf("\n");}
sumwt = (float)twt / n;
sumtat = (float)ttat / n;
return 0;
}
BEST FIT
#include<stdio.h>
void main() {
int b[20], p[20];
int i, j, nb, np
printf("Enter number of blocks: ");
scanf("%d", &nb);
for(i = 0; i < nb; i++) {
printf("Enter the size of Block %d: ", i+1);
scanf("%d", &b[i]);
} printf("Enter number of processes: ");
scanf("%d", &np);
for(i = 0; i < np; i++) {
printf("Enter the size of Process %d: ", i+1);
scanf("%d", &p[i]);
} for(i = 0; i < np; i++) {
for(j = 0; j < nb; j++) {
if(p[i] <= b[j]) {
printf("Process %d of size %d is allocated to Block %d of size %d\n", i+1, p[i], j+1, b[j]);
b[j] -= p[i]; // update block size after allocation
break; } }
if(j == nb) { // if no block is found for the process
printf("Process %d of size %d cannot be allocated\n", i+1, p[i]);}}
printf("\nBlocks with remaining sizes:\n");
for(i = 0; i < nb; i++) {
printf("Block %d: %d\n", i+1, b[i]);}}
First fit
#include <stdio.h>
int main() {
int i, j;
int nb, b[10]; // number of blocks and array to hold block sizes
int np, p[10]; // number of processes and array to hold process sizes
printf("Enter the number of free blocks: ");
scanf("%d", &nb);
printf("Enter the sizes of free blocks:\n");
for(i = 0; i < nb; i++)
scanf("%d", &b[i]);
printf("Enter the number of processes: ");
scanf("%d", &np);
printf("Enter the sizes of processes:\n");
for(i = 0; i < np; i++)
scanf("%d", &p[i]);
printf("\nSizes of free blocks:\n");
for(i = 0; i < nb; i++)
printf("Block %d: %d\n", i+1, b[i]);
printf("\nSizes of processes:\n");
for(i = 0; i < np; i++)
printf("Process %d: %d\n", i+1, p[i]);
printf("\nFIRST FIT MEMORY ALLOCATION\n\n");
printf("Process no\tAllocated block\tAllocated size\tFragment in that block\n");
i = 0;
while(i < np) {
for(j = 0; j < nb; j++) {
if(p[i] <= b[j]) {
printf("%d\t\t%d\t\t%d\t\t%d\n", i+1, j+1, p[i], b[j]);
b[j] -= p[i];
break;}}
if(j == nb) { printf("%d\t\tNot allocated\t%d\t\t---\n", i+1, p[i]);} i++; return 0;}
BANKERS AALGORITHM
#include <stdio.h>
int main() {
int p, r; // Number of processes and resources
int count = 0;
int i, j;
int aloc[10][10], max[10][10], need[10][10];
int safe[10];
int avail[10];
int done[10];
int finish = 0;
printf("Enter the number of processes and resources: ");
scanf("%d%d", &p, &r);
printf("Enter the allocation of resources for all processes (%dx%d matrix):\n", p, r);
for (i = 0; i < p; i++) {
for (j = 0; j < r; j++) {
scanf("%d", &aloc[i][j]);}}
printf("Enter the maximum resources required for all processes (%dx%d matrix):\n", p, r);
for (i = 0; i < p; i++) {
for (j = 0; j < r; j++) {
scanf("%d", &max[i][j]);}}
printf("Enter the available resources:\n");
for (i = 0; i < r; i++) {
scanf("%d", &avail[i]);}
printf("Need matrix is:\n");
for (i = 0; i < p; i++) {
for (j = 0; j < r; j++) {
need[i][j] = max[i][j] - aloc[i][j];
printf("%d\t", need[i][j]);}
printf("\n");}
for (i = 0; i < p; i++) {
done[i] = 0;}
while (count < p) {
for (i = 0; i < p; i++) {
if (done[i] == 0) {
for (j = 0; j < r; j++) {
if (need[i][j] > avail[j]) {
break;}}
if (j == r) {
safe[count] = i;
done[i] = 1;
for (j = 0; j < r; j++) {
avail[j] += aloc[i][j];}
count++;
finish = 0;
} else {
finish++;}}}
if (finish == p) {
printf("Safe sequence does not exist.\n");
break;}}
if (finish < p) {
printf("\nAvailable resources after completion:\n");
for (i = 0; i < r; i++) {
printf("%d\t", avail[i]);}
printf("\nSafe Sequence is:\n");
for (i = 0; i < p; i++) {
printf("P%d\t", safe[i]);}} return o;}