Os Experiments
Os Experiments
return 0;
}
Priority
#include <stdio.h>
#define max 20
int main() {
int bt[max], at[max], ft[max], wt[max], tat[max], p[max];
int i, j, n, temp1, temp2, temp3;
float avg_wt = 0, avg_tat = 0;
printf("Enter number of processes to be executed: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter the burst time of process-%d: ", i + 1);
scanf("%d", &bt[i]);
printf("Enter the arrival time for process-%d: ", i + 1);
scanf("%d", &at[i]);
p[i] = i + 1;
}
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (at[i] > at[j] || (at[i] == at[j] && p[i] > p[j])) {
temp1 = at[i];
at[i] = at[j];
at[j] = temp1;
temp2 = bt[i];
bt[i] = bt[j];
bt[j] = temp2;
temp3 = p[i];
p[i] = p[j];
p[j] = temp3;
}
}
}
int total_time = 0;
for (i = 0; i < n; i++) {
if (total_time < at[i]) {
total_time = at[i];
}
total_time += bt[i];
ft[i] = total_time;
wt[i] = ft[i] - at[i] - bt[i];
tat[i] = bt[i] + wt[i];
avg_wt += wt[i];
avg_tat += tat[i];
}
printf("\n%-10s %-12s %-10s %-15s %-12s %-14s\n", "Process", "Arrival Time",
"Burst Time", "Completion Time", "Waiting Time", "Turnaround Time");
for (i = 0; i < n; i++) {
printf("%-10d %-12d %-10d %-15d %-12d %-14d\n", p[i], at[i], bt[i], ft[i], wt[i],
tat[i]);
}
printf("\nAverage Waiting Time: %.2f", avg_wt / n);
printf("\nAverage Turnaround Time: %.2f\n", avg_tat / n);
return 0;
}
7. Write a program to solve producer-consumer problem using
Semaphores.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include <signal.h>
#define MAX 5
#define TOTAL 10
int buffer[MAX];
int in = 0, out = 0;
sem_t empty, full;
pthread_mutex_t mutex;
int running = 1;
void handle_signal(int sig) {
printf("\nReceived signal %d. Cleaning up and exiting...\n", sig);
running = 0;
}
void* producer(void* param) {
int item;
for (int i = 0; i < TOTAL && running; i++) {
item = rand() % 100;
sem_wait(&empty);
pthread_mutex_lock(&mutex);
buffer[in] = item;
printf("Produced: %d at index %d\n", item, in);
in = (in + 1) % MAX;
pthread_mutex_unlock(&mutex);
sem_post(&full);
sleep(1);
}
return NULL;
}
void* consumer(void* param) {
int item;
for (int i = 0; i < TOTAL && running; i++) {
sem_wait(&full);
pthread_mutex_lock(&mutex);
item = buffer[out];
printf("Consumed: %d from index %d\n", item, out);
out = (out + 1) % MAX;
pthread_mutex_unlock(&mutex);
sem_post(&empty);
sleep(1);
}
return NULL;
}
int main() {
srand(time(NULL));
signal(SIGINT, handle_signal);
pthread_t prod, cons;
sem_init(&empty, 0, MAX);
sem_init(&full, 0, 0);
pthread_mutex_init(&mutex, NULL);
pthread_create(&prod, NULL, producer, NULL);
pthread_create(&cons, NULL, consumer, NULL);
pthread_join(prod, NULL);
pthread_join(cons, NULL);
sem_destroy(&empty);
sem_destroy(&full);
pthread_mutex_destroy(&mutex);
printf("Program finished successfully!\n");
return 0;
}
8. Implement the following memory allocation methods for fixed
partition
a) First fit b) Worst fit c) Best fit
a) First fit
#include <stdio.h>
#include <stdlib.h>
void firstFit(int blockSize[], int m, int processSize[], int n) {
int allocation[n];
for (int i = 0; i < n; i++)
allocation[i] = -1;
for (int i = 0; i < n; i++) {
int firstIdx = -1;
for (int j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
firstIdx = j;
break;
}
}
if (firstIdx != -1) {
allocation[i] = firstIdx;
blockSize[firstIdx] -= processSize[i];
}
}
printf("Process No.\tProcess Size\tBlock No.\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t", i, processSize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i]);
else
printf("Not Allocated\n");
}
}
int main() {
int i, bs, p, blockSize[10], processSize[10];
printf("Enter no. of blocks: ");
scanf("%d", &bs);
for (i = 0; i < bs; i++) {
printf("Enter %d block size: ", i);
scanf("%d", &blockSize[i]);
}
printf("Enter no. of processes: ");
scanf("%d", &p);
for (i = 0; i < p; i++) {
printf("Enter %d process size: ", i);
scanf("%d", &processSize[i]);
}
firstFit(blockSize, bs, processSize, p);
return 0;
}
c) Best fit
#include <stdio.h>
#include <stdlib.h>
void BestFit(int blockSize[], int m, int processSize[], int n) {
int allocation[n];
for (int i = 0; i < n; i++)
allocation[i] = -1;
for (int i = 0; i < n; i++) {
int bestIdx = -1;
for (int j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
if (bestIdx == -1 || blockSize[j] < blockSize[bestIdx])
bestIdx = j;
}
}
if (bestIdx != -1) {
allocation[i] = bestIdx;
blockSize[bestIdx] -= processSize[i];
}
}
printf("Process No.\tProcess Size\tBlock No.\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t", i, processSize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i]);
else
printf("Not Allocated\n");
}
}
int main() {
int i, bs, p, blockSize[10], processSize[10];
printf("Enter no. of blocks: ");
scanf("%d", &bs);
for (i = 0; i < bs; i++) {
printf("Enter %d block size: ", i);
scanf("%d", &blockSize[i]);
}
printf("Enter no. of processes: ");
scanf("%d", &p);
for (i = 0; i < p; i++) {
printf("Enter %d process size: ", i);
scanf("%d", &processSize[i]);
}
BestFit(blockSize, bs, processSize, p);
return 0;
}
b) Worst fit
#include <stdio.h>
#include <stdlib.h>
void worstFit(int blockSize[], int m, int processSize[], int n) {
int allocation[n];
for (int i = 0; i < n; i++)
allocation[i] = -1;
for (int i = 0; i < n; i++) {
int worstIdx = -1;
for (int j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
if (worstIdx == -1 || blockSize[j] > blockSize[worstIdx])
worstIdx = j;
}
}
if (worstIdx != -1) {
allocation[i] = worstIdx;
blockSize[worstIdx] -= processSize[i];
}
}
printf("Process No.\tProcess Size\tBlock No.\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t", i, processSize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i]);
else
printf("Not Allocated\n");
}
}
int main() {
int i, bs, p, blockSize[10], processSize[10];