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

Copy Chette Os Lab

Uploaded by

Karthi Mk
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)
7 views

Copy Chette Os Lab

Uploaded by

Karthi Mk
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/ 8

CONSUMER AND PRODUCER #include<stdio.

h>

#include<stdlib.h>

int mutex = 1, full = 0, empty = 3, x = 0, i = 1;

int wait(int s)

{ return (--s);}

int signal(int s)

{return (++s);}

void producer(){

mutex = wait(mutex);

full = signal(full);

empty = wait(empty);

x++;

printf("\nProducer produces the item = %d", x);

mutex = signal(mutex);}

void consumer(){

mutex = wait(mutex);

full = wait(full);

empty = signal(empty);

printf("\nConsumer consumes item = %d", x);

while (i <= x){

printf("\nConsumer consumes item %d", i);

i++;

full = wait(full);

empty = signal(empty);}

mutex = signal(mutex);}

int main(){

int n;

printf("\n1. Producer\n2. Consumer\n3. Exit\n");

while (1){

printf("Enter your Choice:\n");

scanf("%d", &n);switch (n){


case 1:

if (mutex == 1 && empty != 0)

producer();

else

printf("Buffer is Full!!");

break;

case 2:

if (mutex == 1 && full != 0)

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;

int bt[10], p[10], compt[10], wt[10], tat[10];

int temp1, temp2;

float sumwt = 0.0, sumtat = 0.0;

printf("Enter number of processes: ");

scanf("%d", &n);

printf("Enter the burst time and priority of each process:\n");

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

printf("Process %d:\n", i+1);

printf("Burst Time: ");

scanf("%d", &bt[i]);
printf("Priority: ");

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

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

for(j = i + 1; j < n; j++) {

if(p[i] > p[j]) {

// Swap burst time

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;

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

compt[i] = bt[i] + compt[i - 1];

wt[i] = compt[i] - bt[i];}

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

tat[i] = compt[i];

sumtat += tat[i];

sumwt += wt[i];}

printf("\nProcess\tBurst Time\tPriority\tCompletion Time\tTurnaround Time\tWaiting Time\n");

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

printf("P%d\t%d\t\t%d\t\t%d\t\t%d\t\t\t%d\n", i + 1, bt[i], p[i], compt[i], tat[i], wt[i]);}

printf("\nTOTAL WAITING TIME = %.2f\n", sumwt);

printf("AVERAGE WAITING TIME = %.2f\n", sumwt / n);

printf("TOTAL TURNAROUND TIME = %.2f\n", sumtat);

printf("AVERAGE TURNAROUND TIME = %.2f\n", sumtat / n);

return 0;}

#include <stdio.h>
FCFS CODE #include <stdlib.h>

int main() {

int i, n;

int sum, wt, tat, twt, ttat;

int bt[10];

float sumwt, sumtat;

printf("Enter number of processes = ");

scanf("%d", &n);

printf("Enter the burst time of %d processes\n", n);

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

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

printf("\n FCFS\n");

printf(" Process \t Waiting Time \t Turn Around Time\n");

printf("1 \t\t 0 \t\t\t %d\n", bt[0]);

sum = 0;

twt = 0;

ttat = bt[0];

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

sum += bt[i - 1];

wt = sum;

tat = sum + bt[i];

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;

printf("\n Average Waiting Time = %4.2f", sumwt);

printf("\n Average Turn Around Time = %4.2f", sumtat);

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;}

You might also like