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

OSPrac

The document discusses several algorithms related to process scheduling and synchronization including semaphore-based mutual exclusion, process creation using fork(), wait(), the banker's algorithm for deadlock avoidance, FCFS scheduling, SJF scheduling, priority scheduling, and round robin scheduling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

OSPrac

The document discusses several algorithms related to process scheduling and synchronization including semaphore-based mutual exclusion, process creation using fork(), wait(), the banker's algorithm for deadlock avoidance, FCFS scheduling, SJF scheduling, priority scheduling, and round robin scheduling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

1.

SEMAPHORE MUTUAL EXCLUSION WITHOUT USING MONITOR

#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>

#include <semaphore.h>

#include <unistd.h>

#define NUM_THREADS 2

// Shared resource

int shared_variable = 0;

// Semaphore for mutual exclusion

sem_t mutex;

void* thread_function(void* thread_id) {

int id = ((int)thread_id);

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

// Entry section (wait)

sem_wait(&mutex);

// Critical section

printf("Thread %d in critical section. Shared variable = %d\n", id, shared_variable);

shared_variable++;

// Exit section (signal)

sem_post(&mutex);

// Non-critical section

printf("Thread %d in non-critical section.\n", id);


// Sleep to make the output more readable

usleep(100000);

pthread_exit(NULL);

int main() {

pthread_t threads[NUM_THREADS];

int thread_ids[NUM_THREADS];

// Initialize semaphore

sem_init(&mutex, 0, 1);

// Create threads

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

thread_ids[i] = i;

pthread_create(&threads[i], NULL, thread_function, (void*)&thread_ids[i]);

// Join threads

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

pthread_join(threads[i], NULL);

// Destroy semaphore

sem_destroy(&mutex);

return 0;

}
2. PROCESS CREATION USING FORK(), GETPID(),GETPPID(), WAIT()

#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/wait.h>

#include <unistd.h>

int main() {

pid_t child_pid;

// Create a new process

child_pid = fork();

if (child_pid == -1) {

perror("fork");

exit(EXIT_FAILURE);

if (child_pid == 0) {

// Code for the child process

printf("Child process: PID = %d, PPID = %d\n", getpid(), getppid());

exit(EXIT_SUCCESS); // Child process exits

} else {

// Code for the parent process

printf("Parent process: PID = %d, Child PID = %d\n", getpid(), child_pid);

wait(NULL); // Parent process waits for the child to finish

printf("Child process has terminated.\n");

return 0;

}
3. BANKERS ALGORITHM FOR DEADLOCKS

#include <stdio.h>

int m, n, i, j, al[10][10], max[10][10], av[10], need[10][10], temp, z, y, p, k;

void main() {

printf("\n Enter no of processes : ");

scanf("%d", &m); // enter numbers of processes

printf("\n Enter no of resources : ");

scanf("%d", &n); // enter numbers of resources

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

for (j = 0; j < n; j++) {

printf("\n Enter instances for al[%d][%d] = ", i,j); // al[][] matrix is for allocated instances

scanf("%d", &al[i][j]);

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

for (j = 0; j < n; j++) {

printf("\n Enter instances for max[%d][%d] = ", i,j); // max[][] matrix is for max instances

scanf("%d", &max[i][j]);

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

printf("\n Available Resource for av[%d] = ",i); // av[] matrix is for available instances

scanf("%d", &av[i]);

// Print allocation values

printf("Alocation Values :\n");

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

for (j = 0; j < n; j++) {

printf(" \t %d", al[i][j]); // printing allocation matrix

}
printf("\n");

printf("\n\n");

// Print max values

printf("Max Values :\n");

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

for (j = 0; j < n; j++) {

printf(" \t %d", max[i][j]); // printing max matrix

printf("\n");

printf("\n\n");

// Print need values

printf("Need Values :\n");

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

for (j = 0; j < n; j++) {

need[i][j] = max[i][j] - al[i][j]; // calculating need matrix

printf("\t %d", need[i][j]); // printing need matrix

printf("\n");

p = 1; // used for terminating while loop

y = 0;

while (p != 0) {

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

z = 0;

for (j = 0; j < n; j++) {

if (need[i][j] <= av[j] &&

(need[i][0] != -1)) { // comparing need with available instance and

// checking if the process is done

// or not
z++; // counter if condition TRUE

if (z == n) { // if need<=available TRUE for all resources then condition

// is TRUE

for (k = 0; k < n; k++) {

av[k] += al[i][k]; // new work = work + allocated

printf("\n SS process %d", i); // Print the Process

need[i][0] = -1; // assign -1 if Process done

y++; // cont if process done

} // end for loop

if (y == m) { // if all done then

p = 0; // exit while loop

}} // end while

printf("\n");

4. FCFS

#include <stdio.h>

void findWaitingTime(int n, int bt[], int wt[]) {

wt[0] = 0;

for (int i = 1; i < n; i++)

wt[i] = bt[i - 1] + wt[i - 1];

void findTurnAroundTime(int n, int bt[], int wt[], int tat[]) {

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

tat[i] = bt[i] + wt[i];


}

void findAverageTime(int n, int bt[]) {

int wt[n], tat[n];

findWaitingTime(n, bt, wt);

findTurnAroundTime(n, bt, wt, tat);

float total_wt = 0, total_tat = 0;

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

total_wt += wt[i];

total_tat += tat[i];

printf("Average waiting time = %.2f\n", total_wt / n);

printf("Average turnaround time = %.2f\n", total_tat / n);

int main() {

int n;

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

scanf("%d", &n);

int burst_time[n];

printf("Enter burst times for each process:\n");

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

printf("Process %d: ", i + 1);

scanf("%d", &burst_time[i]);

findAverageTime(n, burst_time);

return 0;

}
5. SJF

#include <stdio.h>

void findWaitingTime(int n, int bt[], int wt[]) {

int temp[n];

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

temp[i] = bt[i];

int complete = 0, t = 0, minm = 9999, shortest = 0, finish_time;

while (complete != n) {

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

if ((temp[j] <= t) && (temp[j] < minm) && (temp[j] > 0)) {

minm = temp[j];

shortest = j;

temp[shortest] = 0;

wt[shortest] = t;

t += bt[shortest];

complete++;

minm = 9999;

void findTurnAroundTime(int n, int bt[], int wt[], int tat[]) {

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

tat[i] = bt[i] + wt[i];

void findAverageTime(int n, int bt[]) {

int wt[n], tat[n];


findWaitingTime(n, bt, wt);

findTurnAroundTime(n, bt, wt, tat);

float total_wt = 0, total_tat = 0;

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

total_wt += wt[i];

total_tat += tat[i];

printf("Average waiting time = %.2f\n", total_wt / n);

printf("Average turnaround time = %.2f\n", total_tat / n);

int main() {

int n;

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

scanf("%d", &n);

int burst_time[n];

printf("Enter burst times for each process:\n");

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

printf("Process %d: ", i + 1);

scanf("%d", &burst_time[i]);

findAverageTime(n, burst_time);

return 0;

}
6. Priority Scheduling

#include <stdio.h>

void findWaitingTime(int n, int bt[], int wt[], int priority[]) {

int pos, temp;

wt[0] = 0;

// Sort processes based on priority

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

pos = i;

for (int j = i + 1; j < n; j++) {

if (priority[j] < priority[pos]) {

pos = j;

// Swap priority values

temp = priority[i];

priority[i] = priority[pos];

priority[pos] = temp;

// Swap burst time values

temp = bt[i];

bt[i] = bt[pos];

bt[pos] = temp;

// Calculate waiting time

for (int i = 1; i < n; i++)

wt[i] = bt[i - 1] + wt[i - 1];

void findTurnAroundTime(int n, int bt[], int wt[], int tat[]) {


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

tat[i] = bt[i] + wt[i];

void findAverageTime(int n, int bt[], int priority[]) {

int wt[n], tat[n];

findWaitingTime(n, bt, wt, priority);

findTurnAroundTime(n, bt, wt, tat);

float total_wt = 0, total_tat = 0;

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

total_wt += wt[i];

total_tat += tat[i];

printf("Average waiting time = %.2f\n", total_wt / n);

printf("Average turnaround time = %.2f\n", total_tat / n);

int main() {

int n;

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

scanf("%d", &n);

int burst_time[n], priority[n];

printf("Enter burst times and priorities for each process:\n");

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

printf("Process %d:\n", i + 1);


printf("Burst Time: ");

scanf("%d", &burst_time[i]);

printf("Priority: ");

scanf("%d", &priority[i]);

findAverageTime(n, burst_time, priority);

return 0;

7. ROUND ROBIN

#include <stdio.h>

void findWaitingTime(int processes[], int n, int bt[], int wt[], int quantum) {

int remaining_time[n];

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

remaining_time[i] = bt[i];

wt[i] = 0;

int t = 0; // Current time

while (1) {

int done = 1;

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

if (remaining_time[i] > 0) {

done = 0; // There are still processes to execute

if (remaining_time[i] > quantum) {

t += quantum;
remaining_time[i] -= quantum;

} else {

t += remaining_time[i];

wt[i] = t - bt[i];

remaining_time[i] = 0;

if (done == 1)

break;

void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[]) {

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

tat[i] = bt[i] + wt[i];

void findAverageTime(int processes[], int n, int bt[], int quantum) {

int wt[n], tat[n];

findWaitingTime(processes, n, bt, wt, quantum);

findTurnAroundTime(processes, n, bt, wt, tat);

float total_wt = 0, total_tat = 0;

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

total_wt += wt[i];

total_tat += tat[i];

}
printf("Average waiting time = %.2f\n", total_wt / n);

printf("Average turnaround time = %.2f\n", total_tat / n);

int main() {

int n, quantum;

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

scanf("%d", &n);

int burst_time[n], processes[n];

printf("Enter burst times for each process:\n");

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

processes[i] = i + 1;

printf("Process %d: ", i + 1);

scanf("%d", &burst_time[i]);

printf("Enter the time quantum: ");

scanf("%d", &quantum);

findAverageTime(processes, n, burst_time, quantum);

return 0;

8. LRU

#include <stdio.h>

#define MAX_FRAMES 3 // Number of frames in memory


void printFrames(int frames[], int n) {

printf("Frames: ");

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

if (frames[i] == -1) {

printf("[ ] ");

} else {

printf("[%d] ", frames[i]);

printf("\n");

int isPageInFrames(int frames[], int n, int page) {

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

if (frames[i] == page) {

return 1; // Page is in frames

return 0; // Page is not in frames

int getLRUIndex(int counter[], int n) {

int minIndex = 0;

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

if (counter[i] < counter[minIndex]) {

minIndex = i;

return minIndex;

}
void lruPageReplacement(int pages[], int n, int frames[], int frameCount) {

int pageFaults = 0;

int counter[MAX_FRAMES] = {-1, -1, -1}; // Counter to keep track of usage

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

printf("\nReference to Page %d:\n", pages[i]);

printFrames(frames, frameCount);

if (!isPageInFrames(frames, frameCount, pages[i])) {

int lruIndex = getLRUIndex(counter, frameCount);

frames[lruIndex] = pages[i];

counter[lruIndex] = i; // Update the counter for the newly added page

pageFaults++;

} else {

for (int j = 0; j < frameCount; j++) {

if (frames[j] == pages[i]) {

counter[j] = i; // Update the counter for the referenced page

break;

printf("\nTotal Page Faults: %d\n", pageFaults);

int main() {

int n, frames[MAX_FRAMES], pages[MAX_FRAMES];

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


scanf("%d", &n);

printf("Enter the page reference sequence:\n");

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

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

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

frames[i] = -1; // Initialize frames as empty

lruPageReplacement(pages, n, frames, MAX_FRAMES);

return 0;

9. LFU

#include <stdio.h>

#include <limits.h>

#define MAX_FRAMES 3 // Number of frames in memory

void printFrames(int frames[], int n) {

printf("Frames: ");

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

if (frames[i] == -1) {

printf("[ ] ");

} else {

printf("[%d] ", frames[i]);

printf("\n");

}
int isPageInFrames(int frames[], int n, int page) {

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

if (frames[i] == page) {

return 1; // Page is in frames

return 0; // Page is not in frames

int getLFUIndex(int frequency[], int n) {

int minIndex = 0;

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

if (frequency[i] < frequency[minIndex]) {

minIndex = i;

return minIndex;

void lfuPageReplacement(int pages[], int n, int frames[], int frameCount) {

int pageFaults = 0;

int frequency[MAX_FRAMES] = {0};

int counter[MAX_FRAMES] = {0};

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

printf("\nReference to Page %d:\n", pages[i]);

printFrames(frames, frameCount);

if (!isPageInFrames(frames, frameCount, pages[i])) {

int lfuIndex = getLFUIndex(frequency, frameCount);

frames[lfuIndex] = pages[i];
frequency[lfuIndex] = 1; // Set frequency to 1 for the newly added page

pageFaults++;

} else {

for (int j = 0; j < frameCount; j++) {

if (frames[j] == pages[i]) {

frequency[j]++; // Increment the frequency for the referenced page

break;

printf("\nTotal Page Faults: %d\n", pageFaults);

int main() {

int n, frames[MAX_FRAMES], pages[MAX_FRAMES];

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

scanf("%d", &n);

printf("Enter the page reference sequence:\n");

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

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

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

frames[i] = -1; // Initialize frames as empty

lfuPageReplacement(pages, n, frames, MAX_FRAMES);

return 0;

You might also like