0% found this document useful (0 votes)
16 views14 pages

OSlab Codes

The document contains multiple C programs implementing various CPU scheduling and page replacement algorithms including Round Robin, FCFS, FIFO, SJF, LS, and LRU. Each program prompts the user for input regarding processes or pages, calculates relevant metrics such as waiting time and turnaround time, and displays the results. The algorithms demonstrate different approaches to managing process execution and memory management in operating systems.

Uploaded by

et23.rane.advait
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)
16 views14 pages

OSlab Codes

The document contains multiple C programs implementing various CPU scheduling and page replacement algorithms including Round Robin, FCFS, FIFO, SJF, LS, and LRU. Each program prompts the user for input regarding processes or pages, calculates relevant metrics such as waiting time and turnaround time, and displays the results. The algorithms demonstrate different approaches to managing process execution and memory management in operating systems.

Uploaded by

et23.rane.advait
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/ 14

ROUND ROBINS

#include <stdio.h>

#include <conio.h>

int main() {

int i, processes, sum = 0, cnt = 0, y, q;

int wt = 0, tat = 0;

int at[10], bt[10], temp[10];

float avg_waitt, avg_turnat;

clrscr();

printf("Total number of processes in the system: ");

scanf("%d", &processes);

y = processes;

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

printf("\nEnter the Arrival and Burst time of Process[%d]\n", i + 1);

printf("Arrival time: ");

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

printf("Burst time: ");

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

temp[i] = bt[i];

printf("Enter the Time Quantum: ");

scanf("%d", &q);
printf("\nProcess No\tBurst Time\tTAT\t\tWaiting Time\n");

i = 0; // initialize i before loop

while (y != 0) {

if (temp[i] <= q && temp[i] > 0) {

sum = sum + temp[i];

temp[i] = 0;

cnt = 1;

} else if (temp[i] > 0) {

temp[i] = temp[i] - q;

sum = sum + q;

if (temp[i] == 0 && cnt == 1) {

y--;

printf("\nProcess[%d]\t%d\t\t%d\t\t%d", i + 1, bt[i], sum - at[i], sum - at[i] - bt[i]);

wt = wt + sum - at[i] - bt[i];

tat = tat + sum - at[i];

cnt = 0;

if (i == processes - 1) {

i = 0;

} else if (at[i + 1] <= sum) {

i++;

} else {

i = 0;
}

avg_waitt = (float) wt / processes;

avg_turnat = (float) tat / processes;

printf("\n\nAverage Turnaround Time: %.2f", avg_turnat);

printf("\nAverage Waiting Time: %.2f", avg_waitt);

getch();

return 0;

}
FCFS(First Come First Serve)
#include<stdio.h>

#include<conio.h>

void main()

int bt[20];

int wt[20];

int tat[20];

int i, n,b;

float wtavg, tatavg;

clrscr();

printf("\nEnter the number of processes--");

scanf("%d", &n);

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

printf("\nEnter Burst Time for Process %d--", i);

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

wt[0]=wtavg=0;

tat[0]=tatavg=bt[0];

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

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

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

wtavg=wtavg+wt[i];

tatavg=tatavg+tat[i];

printf("\t PROCESS BURST TIME WAITING TIME TURNAROUND TIME\n");


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

printf("\n\t P%d \t\t %d \t\t %d \t\t %d", i, bt[i], wt[i], tat[i]);

printf("\nAverage Waiting Time %f", wtavg/n);

printf("\nAverage Turnaround Time -- %f", tatavg/n);

getch();

}
FIFO(First In First Out)
#include <stdio.h>

#include <conio.h>

void main() {

int incomingStream[] = {4, 1, 2, 4, 5};

int pageFaults = 0;

int frames = 3;

int temp[10]; // Use fixed size for Turbo C

int m, n, s, pages;

clrscr();

pages = sizeof(incomingStream) / sizeof(incomingStream[0]);

printf("Incoming \t Frame 1 \t Frame 2 \t Frame 3\n");

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

temp[m] = -1;

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

s = 0;

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

if (incomingStream[m] == temp[n]) {

s++;

pageFaults--; // Counteract increment later


break;

pageFaults++;

if ((pageFaults <= frames) && (s == 0)) {

temp[m] = incomingStream[m];

} else if (s == 0) {

temp[(pageFaults - 1) % frames] = incomingStream[m];

printf("%d\t\t", incomingStream[m]);

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

if (temp[n] != -1)

printf(" %d\t\t", temp[n]);

else

printf(" - \t\t");

printf("\n");

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

getch();

}
SJF(Shortest Job First)
#include <stdio.h>

#include <conio.h>

void main() {

int bt[20]; // Burst Time

int wt[20]; // Waiting Time

int tat[20]; // Turnaround Time

int pid[20]; // Process IDs

int i, j, n, temp_bt, temp_pid;

float wtavg = 0, tatavg = 0;

clrscr();

printf("\nEnter the number of processes: ");

scanf("%d", &n);

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

pid[i] = i;

printf("Enter Burst Time for Process %d: ", i);

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

// Sort by burst time (SJF Scheduling)

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

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

if (bt[i] > bt[j]) {

// Swap burst time


temp_bt = bt[i];

bt[i] = bt[j];

bt[j] = temp_bt;

// Swap process IDs

temp_pid = pid[i];

pid[i] = pid[j];

pid[j] = temp_pid;

wt[0] = 0;

tat[0] = bt[0];

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

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

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

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

wtavg += wt[i];

tatavg += tat[i];

printf("\n\tPROCESS\tBURST TIME\tWAITING TIME\tTURNAROUND TIME\n");

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

printf("\n\tP%d\t\t%d\t\t%d\t\t%d", pid[i], bt[i], wt[i], tat[i]);


}

printf("\n\nAverage Waiting Time: %.2f", wtavg / n);

printf("\nAverage Turnaround Time: %.2f", tatavg / n);

getch();

}
LS(Longest Seek Time First)
#include <stdio.h>

#include <dir.h>

#include <string.h>

int main() {

struct ffblk file;

int done;

int count;

count = 0;

printf("Simulated output of `ls` command:\n\n");

done = findfirst("*.*", &file, 0);

while (!done) {

printf("%s\n", file.ff_name);

count++;

done = findnext(&file);

printf("\nSimulated output of `ls | wc -l`:\n");

printf("%d\n", count);

return 0;

}
LRU(Least Recently Used)
#include <stdio.h>

#include <conio.h>

int findLRU(int time[], int n) {

int i, minimum = time[0], pos = 0;

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

if (time[i] < minimum) {

minimum = time[i];

pos = i;

return pos;

void main() {

int no_of_frames, no_of_pages;

int frames[10], pages[30], time[10];

int counter = 0, i, j, pos, faults = 0;

int page, page_found;

clrscr();

printf("Enter number of frames (max 10): ");

scanf("%d", &no_of_frames);
printf("Enter number of pages (max 30): ");

scanf("%d", &no_of_pages);

printf("Enter reference string:\n");

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

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

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

frames[i] = -1;

time[i] = 0;

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

page = pages[i];

page_found = 0;

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

if (frames[j] == page) {

time[j] = counter++;

page_found = 1;

break;

if (!page_found) {

pos = findLRU(time, no_of_frames);

frames[pos] = page;
time[pos] = counter++;

faults++;

printf("Current frames: ");

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

if (frames[j] != -1)

printf("%d\t", frames[j]);

else

printf("-\t");

printf("\n");

printf("\nTotal Page Faults = %d", faults);

getch();

You might also like