Operating System Lab Assignment
1. FCFS Scheduling
#include <stdio.h>
int main() {
int n, bt[20], wt[20], tat[20];
float avg_wt = 0, avg_tat = 0;
printf("Enter number of processes: ");
scanf("%d", &n);
printf("Enter burst times:\n");
for(int i=0;i<n;i++) scanf("%d",&bt[i]);
wt[0] = 0;
for(int i=1;i<n;i++) wt[i] = wt[i-1] + bt[i-1];
for(int i=0;i<n;i++) {
tat[i] = wt[i] + bt[i];
avg_wt += wt[i];
avg_tat += tat[i];
}
printf("\nProcess\tBT\tWT\tTAT\n");
for(int i=0;i<n;i++)
printf("%d\t%d\t%d\t%d\n",i+1,bt[i],wt[i],tat[i]);
printf("Avg WT=%.2f\n",avg_wt/n);
printf("Avg TAT=%.2f\n",avg_tat/n);
}
2. SJF Scheduling
#include <stdio.h>
int main() {
int n, bt[20], wt[20], tat[20], pos, temp;
printf("Enter number of processes: ");
scanf("%d",&n);
printf("Enter burst times:\n");
for(int i=0;i<n;i++) scanf("%d",&bt[i]);
for(int i=0;i<n;i++) {
pos=i;
for(int j=i+1;j<n;j++)
if(bt[j]<bt[pos]) pos=j;
temp=bt[i]; bt[i]=bt[pos]; bt[pos]=temp;
}
wt[0]=0;
for(int i=1;i<n;i++)
wt[i]=wt[i-1]+bt[i-1];
printf("\nProcess\tBT\tWT\tTAT\n");
for(int i=0;i<n;i++) {
tat[i]=bt[i]+wt[i];
printf("%d\t%d\t%d\t%d\n",i+1,bt[i],wt[i],tat[i]);
}
}
3. Priority Scheduling
#include <stdio.h>
int main() {
int n, bt[20], pr[20], wt[20], tat[20], pos, temp;
printf("Enter number of processes: ");
scanf("%d",&n);
printf("Enter burst time and priority:\n");
for(int i=0;i<n;i++) scanf("%d%d",&bt[i],&pr[i]);
for(int i=0;i<n;i++) {
pos=i;
for(int j=i+1;j<n;j++)
if(pr[j]<pr[pos]) pos=j;
temp=bt[i]; bt[i]=bt[pos]; bt[pos]=temp;
temp=pr[i]; pr[i]=pr[pos]; pr[pos]=temp;
}
wt[0]=0;
for(int i=1;i<n;i++)
wt[i]=wt[i-1]+bt[i-1];
printf("\nP\tBT\tPR\tWT\tTAT\n");
for(int i=0;i<n;i++) {
tat[i]=wt[i]+bt[i];
printf("%d\t%d\t%d\t%d\t%d\n",i+1,bt[i],pr[i],wt[i],tat[i]);
}
}
4. Round Robin
#include <stdio.h>
int main() {
int n, bt[20], rem[20], wt[20]={0}, tat[20], tq;
printf("Enter number of processes: ");
scanf("%d",&n);
printf("Enter burst times:\n");
for(int i=0;i<n;i++) {
scanf("%d",&bt[i]);
rem[i]=bt[i];
}
printf("Enter time quantum: ");
scanf("%d",&tq);
int time=0, done;
do {
done=1;
for(int i=0;i<n;i++) {
if(rem[i]>0) {
done=0;
if(rem[i]>tq) {
time+=tq;
rem[i]-=tq;
} else {
time+=rem[i];
wt[i]=time-bt[i];
rem[i]=0;
}
}
}
} while(!done);
printf("\nP\tBT\tWT\tTAT\n");
for(int i=0;i<n;i++) {
tat[i]=bt[i]+wt[i];
printf("%d\t%d\t%d\t%d\n",i+1,bt[i],wt[i],tat[i]);
}
}
5. Banker's Algorithm
#include <stdio.h>
int main() {
int n=3, m=3;
int alloc[3][3]={{0,1,0},{2,0,0},{3,0,2}};
int max[3][3]={{7,5,3},{3,2,2},{9,0,2}};
int avail[3]={3,3,2};
int need[3][3], finish[3]={0}, safe[3];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
need[i][j]=max[i][j]-alloc[i][j];
int count=0;
while(count<n) {
for(int i=0;i<n;i++) {
if(!finish[i]) {
int flag=1;
for(int j=0;j<m;j++)
if(need[i][j]>avail[j]) flag=0;
if(flag) {
for(int j=0;j<m;j++)
avail[j]+=alloc[i][j];
safe[count++]=i;
finish[i]=1;
}
}
}
}
printf("Safe sequence: ");
for(int i=0;i<n;i++)
printf("P%d ",safe[i]);
}
6. Device Driver
#include <stdio.h>
void readDevice() { printf("Reading from device...\n"); }
void writeDevice() { printf("Writing to device...\n"); }
int main() {
readDevice();
writeDevice();
}
7. Disk Scheduling
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, req[20], head, seek=0;
printf("Enter number of requests: ");
scanf("%d",&n);
printf("Enter requests:\n");
for(int i=0;i<n;i++) scanf("%d",&req[i]);
printf("Enter head position: ");
scanf("%d",&head);
for(int i=0;i<n;i++) {
seek += abs(req[i]-head);
head = req[i];
}
printf("Total seek time: %d\n",seek);
}
8. Dining Philosopher
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
sem_t chopstick[5];
void* philos(void* num) {
int i = *(int*)num;
printf("Philosopher %d is thinking\n",i);
sem_wait(&chopstick[i]);
sem_wait(&chopstick[(i+1)%5]);
printf("Philosopher %d is eating\n",i);
sem_post(&chopstick[i]);
sem_post(&chopstick[(i+1)%5]);
}
int main() {
pthread_t t[5];
int a[5];
for(int i=0;i<5;i++) {
sem_init(&chopstick[i],0,1);
a[i]=i;
pthread_create(&t[i],NULL,philos,&a[i]);
}
for(int i=0;i<5;i++)
pthread_join(t[i],NULL);
}
9. Producer Consumer
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
sem_t empty, full;
pthread_mutex_t mutex;
int buffer=0;
void* producer(void* p) {
sem_wait(&empty);
pthread_mutex_lock(&mutex);
buffer++;
printf("Produced: %d\n",buffer);
pthread_mutex_unlock(&mutex);
sem_post(&full);
}
void* consumer(void* c) {
sem_wait(&full);
pthread_mutex_lock(&mutex);
printf("Consumed: %d\n",buffer);
buffer--;
pthread_mutex_unlock(&mutex);
sem_post(&empty);
}
int main() {
pthread_t p,c;
sem_init(&empty,0,1);
sem_init(&full,0,0);
pthread_mutex_init(&mutex,NULL);
pthread_create(&p,NULL,producer,NULL);
pthread_create(&c,NULL,consumer,NULL);
pthread_join(p,NULL);
pthread_join(c,NULL);
}
10. LRU Page Replacement
#include <stdio.h>
int main() {
int frames[10], n, pages[50], count=0, faults=0;
printf("Enter number of pages: ");
scanf("%d",&n);
printf("Enter pages:\n");
for(int i=0;i<n;i++) scanf("%d",&pages[i]);
int f;
printf("Enter number of frames: ");
scanf("%d",&f);
for(int i=0;i<f;i++) frames[i]=-1;
for(int i=0;i<n;i++) {
int found=0;
for(int j=0;j<f;j++)
if(frames[j]==pages[i]) found=1;
if(!found) {
frames[count%f]=pages[i];
count++;
faults++;
}
}
printf("Page faults: %d\n",faults);
}