0% found this document useful (0 votes)
8 views4 pages

Os Exam

Uploaded by

Abhishek Negi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Os Exam

Uploaded by

Abhishek Negi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <stdio.

h> Producer Consumer


#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#define BUFFER_SIZE 5

int buffer[BUFFER_SIZE];
int in = 0, out = 0;
sem_t empty, full, mutex;

void *producer(void *arg) {


for (int i = 0; i < 10; i++) {
int item = rand() % 100;
sem_wait(&empty);
sem_wait(&mutex);
buffer[in] = item;
printf("Producer produced %d\n", item);
in = (in + 1) % BUFFER_SIZE;
sem_post(&mutex);
sem_post(&full);
}
return NULL; }

void *consumer(void *arg) {


for (int i = 0; i < 10; i++) {
sem_wait(&full);
sem_wait(&mutex);
int item = buffer[out];
printf("Consumer consumed %d\n", item);
out = (out + 1) % BUFFER_SIZE;
sem_post(&mutex);
sem_post(&empty);
}
return NULL;
}
int main() {
pthread_t prod, cons;

sem_init(&empty, 0, BUFFER_SIZE);
sem_init(&full, 0, 0);
sem_init(&mutex, 0, 1);

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);
sem_destroy(&mutex);
return 0; }
#include <stdio.h> #include <stdlib.h> #include <pthread.h>

#include <semaphore.h> #include <unistd.h>

Dining Philospher
#define NUM_PHILOSOPHERS 5

sem_t forks[NUM_PHILOSOPHERS];

void *philosopher(void *num) {

int id = *((int *)num);

sem_wait(&forks[id]);

sem_wait(&forks[(id + 1) % NUM_PHILOSOPHERS]);

printf("Philosopher %d is eating\n", id);

sleep(1);

printf("Philosopher %d has finished eating\n", id);

sem_post(&forks[id]);

sem_post(&forks[(id + 1) % NUM_PHILOSOPHERS]);

return NULL;

int main() {

pthread_t threads[NUM_PHILOSOPHERS];

int ids[NUM_PHILOSOPHERS];

for (int i = 0; i < NUM_PHILOSOPHERS; i++) {

sem_init(&forks[i], 0, 1);

ids[i] = i; }

for (int i = 0; i < NUM_PHILOSOPHERS; i++) {

pthread_create(&threads[i], NULL, philosopher, &ids[i]);

for (int i = 0; i < NUM_PHILOSOPHERS; i++) {

pthread_join(threads[i], NULL); }

for (int i = 0; i < NUM_PHILOSOPHERS; i++) {

sem_destroy(&forks[i]);

return 0; }
#include <stdio.h> LRU
void lru(int pages[], int n, int capacity) {
int frames[capacity], timestamps[capacity], time = 0, faults = 0;
for (int i = 0; i < capacity; i++) {
frames[i] = -1; // Initialize frames as empty
timestamps[i] = -1; // Initialize timestamps
}
printf("Page\tFrames\n");
for (int i = 0; i < n; i++) {
int found = 0, least_used_index = 0;
// Check if the page is already in a frame
for (int j = 0; j < capacity; j++) {
if (frames[j] == pages[i]) {
found = 1;
timestamps[j] = time++; // Update usage time
break;
}
}
// Page fault: replace the least recently used page
if (!found) {
for (int j = 1; j < capacity; j++) {
if (timestamps[j] < timestamps[least_used_index])
least_used_index = j;
}
frames[least_used_index] = pages[i];
timestamps[least_used_index] = time++;
faults++;
}
// Print the current state of frames
printf("%d\t", pages[i]);
for (int j = 0; j < capacity; j++)
printf("%d ", frames[j] == -1 ? -1 : frames[j]);
printf("\n");
}
printf("Total Page Faults: %d\n", faults);
}
int main() {
int pages[] = {3,2,1,3,4,1,6,2,4,3,4,2,1,4,5,2,1,3,4};
int n = sizeof(pages) / sizeof(pages[0]);
int capacity = 3;
lru(pages, n, capacity);
return 0;
}
#include <stdio.h> cscan
#include <stdlib.h>
int cScan(int requests[], int n, int head, int disk_size) {
int total_seek = 0;
int i, j;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (requests[i] > requests[j]) {
int temp = requests[i];
requests[i] = requests[j];
requests[j] = temp;
} }
}
int pos = 0;
while (pos < n && requests[pos] < head) {
pos++;
}
for (i = pos; i < n; i++) {
total_seek += abs(requests[i] - head);
head = requests[i];
}
total_seek += abs(disk_size - 1 - head);
total_seek += disk_size - 1;
head = 0;
for (i = 0; i < pos; i++) {
total_seek += abs(requests[i] - head);
head = requests[i];
}
return total_seek;
}
int main() {
int n, i, head;
int disk_size = 200;
printf("Enter number of disk requests: ");
scanf("%d", &n);
int requests[n];
printf("Enter the disk requests: ");
for (i = 0; i < n; i++) { scanf("%d", &requests[i]); }
printf("Enter the initial position of the head: ");
scanf("%d", &head);
int total_seek = cScan(requests, n, head, disk_size);
printf("Total seek movement: %d\n", total_seek);
return 0; }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy