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

Operating System Programs

Uploaded by

Khalid Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Operating System Programs

Uploaded by

Khalid Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

PROGRAM

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

#define MAX_READERS 2
#define MAX_WRITERS 3
#define ITERATIONS 3

int shared_data = 0;
sem_t mutex, wrt;
int readers_count = 0;

void init()
{
sem_init(&mutex, 0, 1);
sem_init(&wrt, 0, 1);
}

void* reader(void* arg) {


int id = *((int*)arg);
int i;
for (i = 0; i < ITERATIONS;i++)
{
sem_wait(&mutex);
readers_count++;
if (readers_count == 1)
{
sem_wait(&wrt);
}
sem_post(&mutex);
printf("Reader %d read: %d\n", id, shared_data);
sleep(1);

sem_wait(&mutex);
readers_count--;
if (readers_count == 0) {
sem_post(&wrt);
}
sem_post(&mutex);

sleep(1);
}
pthread_exit(NULL);
}
void* writer(void* arg) {

int id = *((int*)arg);
int i;
for (i = 0; i < ITERATIONS; i++) {
sem_wait(&wrt);
shared_data++;
printf("Writer %d wrote: %d\n", id, shared_data);
sleep(1);

sem_post(&wrt);

sleep(1);
}
pthread_exit(NULL);
}

int main() {
pthread_t readers[MAX_READERS], writers[MAX_WRITERS];
int reader_ids[MAX_READERS] = {1, 2};
int writer_ids[MAX_WRITERS] = {1, 2, 3};

init();

int i;
for (i = 0; i < MAX_READERS; i++) {
if (pthread_create(&readers[i], NULL, reader, (void*)&reader_ids[i]) != 0) {
perror("Failed to create reader thread");
return 1;
}
}
for (i = 0; i < MAX_WRITERS; i++) {
if (pthread_create(&writers[i], NULL, writer, (void*)&writer_ids[i]) != 0) {
perror("Failed to create writer thread");
return 1;
}
}

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


pthread_join(readers[i], NULL);
}
for (i = 0; i < MAX_WRITERS; i++) {
pthread_join(writers[i], NULL);
}

sem_destroy(&mutex);
sem_destroy(&wrt);

return 0;
}
PROGRAM
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>

#define NUM_PHILOSOPHERS 5

// Define the semaphores


sem_t chopsticks[NUM_PHILOSOPHERS];

// Function to simulate the philosopher's behavior


void *philosopher(void *arg)
{
int id = *((int *)arg);
int left_chopstick = id;
int right_chopstick = (id + 1) % NUM_PHILOSOPHERS;

// Thinking
printf("Philosopher %d is thinking...\n", id);

// Start eating
printf("Philosopher %d is hungry...\n", id);
sem_wait(&chopsticks[left_chopstick]);
sem_wait(&chopsticks[right_chopstick]);
printf("Philosopher %d is eating...\n", id);
sleep(1); // Simulate eating time

// Release chopsticks
sem_post(&chopsticks[left_chopstick]);
sem_post(&chopsticks[right_chopstick]);
printf("Philosopher %d finished eating and released chopsticks.\n", id);

// Resting
sleep(1); // Simulate resting time

int main()
{
pthread_t philosophers[NUM_PHILOSOPHERS];
int philosopher_ids[NUM_PHILOSOPHERS];
int i;

// Initialize semaphores
for (i = 0; i < NUM_PHILOSOPHERS; i++)
{
sem_init(&chopsticks[i], 0, 1);
}

// Create philosopher threads


for (i = 0; i < NUM_PHILOSOPHERS; i++)
{
philosopher_ids[i] = i;
pthread_create(&philosophers[i], NULL, philosopher, (void *)&philosopher_ids[i]);
}

// Join philosopher threads


for (i = 0; i < NUM_PHILOSOPHERS; i++)
{
pthread_join(philosophers[i], NULL);
}

// Destroy semaphores
for (i = 0; i < NUM_PHILOSOPHERS; i++)
{
sem_destroy(&chopsticks[i]);
}

return 0;
}
PROGRAM

#include <stdio.h>

void FIFO(int pages[], int n, int capacity) {


int frame[capacity];
int used[capacity];
for (int i = 0; i < capacity; i++) {
frame[i] = -1;
used[i] = 0;
}

int pageFaults = 0, index = 0;

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


int found = 0;
for (int j = 0; j < capacity; j++) {
if (frame[j] == pages[i]) {
found = 1;
break;
}
}

if (!found) {
frame[index] = pages[i];
index = (index + 1) % capacity;
pageFaults++;
}

printf("Page %d: ", pages[i]);


for (int j = 0; j < capacity; j++) {
if (frame[j] != -1)
printf("%d ", frame[j]);
else
printf("X ");
}
printf("\n");
}
printf("Total Page Faults: %d\n", pageFaults);
}

int main() {
int n, capacity;

printf("Enter the number of pages: ");


scanf("%d", &n);
int pages[n];
printf("Enter the page reference string: ");
for (int i = 0; i < n; i++) {
scanf("%d", &pages[i]);
}

printf("Enter the number of frames: ");


scanf("%d", &capacity);

FIFO(pages, n, capacity);
return 0;
}
PROGRAM

#include <stdio.h>

void LRU(int pages[], int n, int capacity) {


int frame[capacity];
int age[capacity];
for (int i = 0; i < capacity; i++) {
frame[i] = -1;
age[i] = 0;
}

int pageFaults = 0;

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


int found = 0;
for (int j = 0; j < capacity; j++) {
if (frame[j] == pages[i]) {
found = 1;
age[j] = i;
break;
}
}

if (!found) {
int minAge = age[0], minIndex = 0;
for (int j = 1; j < capacity; j++) {
if (age[j] < minAge) {
minAge = age[j];
minIndex = j;
}
}
frame[minIndex] = pages[i];
age[minIndex] = i;
pageFaults++;
}
printf("Page %d: ", pages[i]);
for (int j = 0; j < capacity; j++) {
if (frame[j] != -1)
printf("%d ", frame[j]);
else
printf("X ");
}
printf("\n");
}
printf("Total Page Faults: %d\n", pageFaults);
}
int main() {
int n, capacity;

printf("Enter the number of pages: ");


scanf("%d", &n);
int pages[n];

printf("Enter the page reference string: ");


for (int i = 0; i < n; i++) {
scanf("%d", &pages[i]);
}

printf("Enter the number of frames: ");


scanf("%d", &capacity);

LRU(pages, n, capacity);
return 0;
}
PROGRAM

#include <stdio.h>

int predict(int pages[], int frame[], int n, int index, int capacity) {
int res = -1, farthest = index;
for (int i = 0; i < capacity; i++) {
int j;
for (j = index; j < n; j++) {
if (frame[i] == pages[j]) {
if (j > farthest) {
farthest = j;
res = i;
}
break;
}
}
if (j == n)
return i;
}
return (res == -1) ? 0 : res;
}

void Optimal(int pages[], int n, int capacity) {


int frame[capacity];
for (int i = 0; i < capacity; i++)
frame[i] = -1;

int pageFaults = 0;

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


int found = 0;
for (int j = 0; j < capacity; j++) {
if (frame[j] == pages[i]) {
found = 1;
break;
}
}

if (!found) {
if (i < capacity)
frame[i] = pages[i];
else {
int j = predict(pages, frame, n, i + 1, capacity);
frame[j] = pages[i];
}
pageFaults++;
}
printf("Page %d: ", pages[i]);
for (int j = 0; j < capacity; j++) {
if (frame[j] != -1)
printf("%d ", frame[j]);
else
printf("X ");
}
printf("\n");
}
printf("Total Page Faults: %d\n", pageFaults);
}

int main() {
int n, capacity;

printf("Enter the number of pages: ");


scanf("%d", &n);
int pages[n];

printf("Enter the page reference string: ");


for (int i = 0; i < n; i++) {
scanf("%d", &pages[i]);
}

printf("Enter the number of frames: ");


scanf("%d", &capacity);

Optimal(pages, n, capacity);
return 0;
}
PROGRAM

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

void FCFS(int *request, int n, int initial_position);

int main() {
int n;
printf("Enter the number of requests: ");
scanf("%d", &n);

int *request = (int *)malloc(n * sizeof(int));


if (request == NULL) {
printf("Memory allocation failed!\n");
return 1;
}

printf("Enter the requests: ");


for (int i = 0; i < n; ++i) {
scanf("%d", &request[i]);
}

int initial_position;
printf("Enter the initial position of the disk head: ");
scanf("%d", &initial_position);

// Call FCFS function


FCFS(request, n, initial_position);

free(request);
return 0;
}

void FCFS(int *request, int n, int initial_position) {


int total_seek_time = 0;
printf("Seek Sequence: ");
printf("%d ", initial_position);
for (int i = 0; i < n; ++i) {
total_seek_time += abs(request[i] - initial_position);
initial_position = request[i];
printf("%d ", initial_position);
}
printf("\nTotal Seek Time: %d\n", total_seek_time);
}
PROGRAM

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

void SSTF(int *request, int n, int initial_position);

int main() {
int n;
printf("Enter the number of requests: ");
scanf("%d", &n);

int *request = (int *)malloc(n * sizeof(int));


if (request == NULL) {
printf("Memory allocation failed!\n");
return 1;
}

printf("Enter the requests: ");


for (int i = 0; i < n; ++i) {
scanf("%d", &request[i]);
}

int initial_position;
printf("Enter the initial position of the disk head: ");
scanf("%d", &initial_position);

// Call SSTF function


SSTF(request, n, initial_position);

free(request);
return 0;
}

void SSTF(int *request, int n, int initial_position) {


int total_seek_time = 0;
int visited[n];
for (int i = 0; i < n; ++i) {
visited[i] = 0;
}

printf("Seek Sequence: ");


printf("%d ", initial_position);
for (int count = 0; count < n; ++count) {
int min_distance = INT_MAX;
int next_index = -1;
for (int i = 0; i < n; ++i) {
if (!visited[i]) {
int distance = abs(request[i] - initial_position);
if (distance < min_distance) {
min_distance = distance;
next_index = i;
}
}
}
visited[next_index] = 1;
total_seek_time += min_distance;
initial_position = request[next_index];
printf("%d ", initial_position);
}
printf("\nTotal Seek Time: %d\n", total_seek_time);
}
PROGRAM

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

void SCAN(int *request, int n, int initial_position, int max_position);

int main() {
int n;
printf("Enter the number of requests: ");
scanf("%d", &n);

int *request = (int *)malloc(n * sizeof(int));


if (request == NULL) {
printf("Memory allocation failed!\n");
return 1;
}

printf("Enter the requests: ");


for (int i = 0; i < n; ++i) {
scanf("%d", &request[i]);
}

int initial_position;
printf("Enter the initial position of the disk head: ");
scanf("%d", &initial_position);

int max_position;
printf("Enter the maximum position of the disk: ");
scanf("%d", &max_position);

// Call SCAN function


SCAN(request, n, initial_position, max_position);

free(request);
return 0;
}

void SCAN(int *request, int n, int initial_position, int max_position) {


int total_seek_time = 0;
printf("Seek Sequence: ");
printf("%d ", initial_position);
int direction = 1; // 1 for right, -1 for left
int start_position = initial_position;
while (1) {
if (direction == 1) {
for (int i = start_position; i <= max_position; ++i) {
for (int j = 0; j < n; ++j) {
if (request[j] == i) {
total_seek_time += abs(i - initial_position);
initial_position = i;
printf("%d ", initial_position);
request[j] = -1; // mark as visited
}
}
}
direction = -1; // change direction
} else {
for (int i = max_position; i >= 0; --i) {
for (int j = 0; j < n; ++j) {
if (request[j] == i) {
total_seek_time += abs(i - initial_position);
initial_position = i;
printf("%d ", initial_position);
request[j] = -1; // mark as visited
}
}
}
direction = 1; // change direction
}

// Check if all requests have been serviced


int done = 1;
for (int i = 0; i < n; ++i) {
if (request[i] != -1) {
done = 0;
break;
}
}
if (done) break;
}
printf("\nTotal Seek Time: %d\n", total_seek_time);
}
PROGRAM

#include<stdio.h>
#include<sys/stat.h>
#include<time.h>
int main()
{
struct stat filestat;
char *filename="46dir.c";
printf("File number:%ld\n",filestat.st_ino);
printf("User id: %d\n",filestat.st_uid);
printf("File size : %ld\n",filestat.st_size);
printf("Last access time:%ld\n",filestat.st_atime);
printf("Last modify time: %ld\n",filestat.st_mtime);
return 0;
}
PROGRAM

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t pid1, pid2;
pid1 = fork();
if (pid1 == 0) {
printf("Child 1 process (PID: %d) is sorting a file.\n", getpid());
execlp("sort", "sort", "m1", NULL);
}
pid2=fork();
if(pid2 == 0) {
wait(NULL);
printf("Child 2 process (PID: %d) is copying a file.\n", getpid());
execlp("cp", "cp", "m1", "m3", NULL);
}
else {
wait(NULL);
printf("Parent is executing");
printf("Parent process (PID: %d) \n", getpid());
}
return 0;
}
OUTPUT
OUTPUT
OUTPUT
OUTPUT
OUTPUT
OUTPUT
OUTPUT
OUTPUT
OUTPUT
OUTPUT

You might also like