OS_LAB_PROGRAM_neww
OS_LAB_PROGRAM_neww
#include <stdio.h>
#define MAX_PAGES 50
#define MAX_FRAMES 10
int main() {
int pages[MAX_PAGES], frames[MAX_FRAMES];
int num_pages, num_frames;
int i, j, k;
int page_faults = 0;
int is_page_in_memory;
// Input number of pages
printf("Enter the number of pages: ");
scanf("%d", &num_pages);
// Input page numbers
printf("Enter the page numbers:\n");
for (i = 0; i < num_pages; i++) {
scanf("%d", &pages[i]); }
// Input number of frames
printf("Enter the number of frames: ");
scanf("%d", &num_frames);
// Initialize frame array to -1 (indicating empty)
for (i = 0; i < num_frames; i++) {
frames[i] = -1;}
// Print the header for the output
printf("Ref String\tPage Frames\n");
// Process each page
for (i = 0; i < num_pages; i++) {
// printf("%d\t\t", pages[i]);
is_page_in_memory = 0;
// Check if the page is already in one of the frames
for (k = 0; k < num_frames; k++) {
if (frames[k] == pages[i]) {
is_page_in_memory = 1;
break; } }
// If page is not in memory, replace the oldest page
if (!is_page_in_memory) {
frames[j] = pages[i];
j = (j + 1) % num_frames;
page_faults++;}
// Print the current state of the frames
for (k = 0; k < num_frames; k++) {
printf("%d\t", frames[k]);}
printf("\n");}
// Output the total number of page faults
printf("Total page faults: %d\n", page_faults);
return 0;
}
2)BEST FIT
#include <stdio.h>
struct Process {
int pid, size;
};
struct Partition {
int pid, size, isFree;
};
void bestFit(struct Process p[], struct Partition f[], int n, int m) {
for (int i = 0; i < n; i++) {
int best = -1;
for (int j = 0; j < m; j++) {
if (f[j].isFree && f[j].size >= p[i].size &&
(best == -1 || f[j].size < f[best].size))
best = j;
}
if (best != -1) {
f[best].isFree = 0;
f[best].pid = p[i].pid;
printf("P%d -> F%d\n", p[i].pid, best + 1);
} else {
printf("P%d not allocated\n", p[i].pid);
}}}
int main() {
int n, m;
bestFit(p, f, n, m);
return 0;
}
4)))))PRIORITY
#include <stdio.h>
int main() {
scanf("%d", &n);
printf("Enter burst time and priority for process %d: ", i + 1);
// Swap priority
temp = pr[i];
pr[i] = pr[j];
pr[j] = temp;
// Swap burst time
temp = bt[i];
bt[i] = bt[j];
bt[j] = temp;
if (i > 0) {
avwt += wt[i];
avtat += tat[i];
avwt /= n;
avtat /= n;
return 0;
*PRODUCER CONSUMER**
#include <stdio.h>
#include <stdlib.h>
int mutex = 1;
int full = 0;
int empty = 3;
int x = 0;
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 item %d\n", x);
mutex = signal(mutex);}
void consumer() {
mutex = wait(mutex);
full = wait(full);
empty = signal(empty);
printf("\nConsumer consumes item %d\n", x);
x--;
mutex = signal(mutex);}
int main() {
int n;
printf("\n1. PRODUCER\n2. CONSUMER\n3. EXIT\n");
while (1) {
printf("\nENTER YOUR CHOICE\n");
scanf("%d", &n);
switch (n) {
case 1:
if ((mutex == 1) && (empty != 0)) {
producer();
} else {
printf("BUFFER IS FULL\n");
}
break;
case 2:
if ((mutex == 1) && (full != 0)) {
consumer();
} else {
printf("BUFFER IS EMPTY\n");
}
break;
case 3:
printf("Exiting the program.\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;}}
SJF
#include <stdio.h>
void main() {
int i, j, temp, bt[10], n, wt[10], tt[10], w_total = 0, t_total = 0;
float aw, at;
// Initialize waiting time and turnaround time for the first process
wt[0] = 0; // First process has no waiting time
tt[0] = bt[0]; // Turnaround time is equal to its burst time
w_total = wt[0];
t_total = tt[0];
// Printing results
printf("\nProcess\tBT\tWT\tTT\n");
for (i = 0; i < n; i++) {
printf("P%d\t%d\t%d\t%d\n", i + 1, bt[i], wt[i], tt[i]);
}
for(i=0;i<n;i++){
wt[0]=0;
tt[0]=bt[0];
wt[i+1]=bt[i]+wt[i];
tt[i+1]=tt[i]+bt[i+1];
w1=w1+wt[i];
t1=t1+tt[i];
}
aw=w1/n;
at=t1/n;
printf("\nbt\twt\ttt\n");
for(i=0;i<n;i++)
printf("%d\t%d\t%d\n",bt[i],wt[i],tt[i]);
printf("aw=%f\n,at=%f\n",aw,at);
}
FORK
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main() {
pid_t pid;
pid = fork();
if (pid < 0) {
fprintf(stderr, "Fork Failed\n");
return 1;
}else if (pid == 0) {
printf("This is the child process. My PID is %d\n", getpid());
}else {
printf("This is the parent process. My child's PID is %d and my PID is %d\n", pid,
getpid());
}
return 0;
}
WAIT
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
int main(){
pid_t pid;
pid=fork();
if(pid<0){
perror("fork failed");
}
else if(pid==0){
printf("the pi id is %d",getpid());
sleep(2);
}
else{
printf("parent process is waitig for child ");
wait(NULL);
}
First git new
#include <stdio.h>
int main() {
int numProcesses, numPartitions;
// Allocation logic
for (int i = 0; i < numProcesses; i++) {
for (int j = 0; j < numPartitions; j++) {
if (f[j] >= p[i]) {
a[i] = j;
f[j] -= p[i];
break;
} }}
// Print the allocation results
printf("\nProcess No.\tProcess Size\tPartition No.\n");
for (int i = 0; i < numProcesses; i++) {
printf("%d\t\t\t%d\t\t\t", i + 1, p[i]);
if (a[i] != -1)
printf("%d\n", a[i] + 1);
else
printf("Not Allocated\n"); }
return 0;
}