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

Operating system

The document is a report on Operating Systems submitted by Aayush Maharjan to Dharmendra Prasad at Swoyambhu International College. It includes acknowledgments, a table of contents, and detailed explanations of various operating system concepts and simulations in C programming, such as scheduling algorithms and memory allocation strategies. The report aims to enhance understanding of operating systems through practical examples and simulations.

Uploaded by

Aayush Mhrz'n
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)
14 views

Operating system

The document is a report on Operating Systems submitted by Aayush Maharjan to Dharmendra Prasad at Swoyambhu International College. It includes acknowledgments, a table of contents, and detailed explanations of various operating system concepts and simulations in C programming, such as scheduling algorithms and memory allocation strategies. The report aims to enhance understanding of operating systems through practical examples and simulations.

Uploaded by

Aayush Mhrz'n
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/ 20

SWOYAMBHU INTL.

COLLEGE
Lagankhel, Lalitpur
Tribhuvan University

Report on
Operating System

Submitted by: Submitted to:


Aayush Maharjan Dharmendra Prasad
Lecturer: Operating
Level: BCA
System
Year/Semester: 4th (Swoyambhu

International College)
ACKNOWLEDGEMENT

We extend our sincere gratitude to the Department of Computer Science and Information
Technology, Swoyambhu International College, for providing us with the opportunity to explore
and enhance our understanding of computer software through the case study on "OPERATING
SYSTEM".

Our heartfelt appreciation goes to our esteemed guide, Raj Kumar Sah, Supervisor at Swoyambhu
International College, for his invaluable guidance, encouragement, and continuous support
throughout this work. His insightful suggestions and collaborative approach have been
instrumental in the completion of this project.

We would also like to thank Mr. Dharmendra Prasad, Lecturer of Computer Science at,
Swoyambhu International College, Lagankhel, for his unwavering support and leadership.

Finally, we are deeply grateful to our friends for their continuous encouragement and support
throughout this project. Our gratitude also extends to all individuals, both seen and unseen, who
contributed to the preparation of this work.

We eagerly welcome feedback and suggestions for improvement, which will be highly valued and
appreciated.
TABLE OF CONTENTS
S.N Topic Signature
1 Introduction to Operating System
2 Simulate Producer-Consumer Problem Using Semaphore
3 Simulate Producer-Consumer Problem Using Semaphore
4 Simulate Shortest Job First (SJF) Scheduling in C
5 Simulate Priority Scheduling in C
6 Simulate Round Robin Scheduling in C
7 Simulate the First Fit Algorithm in C
8 Simulate the Best Fit Algorithm in C
9 Simulate the Worst Fit Algorithm in C
10 Simulate the FIFO Page Replacement in C
11 Simulate the LRU Page Replacement in C
12 Simulate the OPR Page Replacement in C
13 Simulate the FCFS Disk Scheduling in C
14 Simulate the SCAN Disk Scheduling in C
15 C Program for Implementing Sequential File Allocation Method
16 C Program to Simulate Single-Level Directory Organization
Introduction to Operating Systems

An operating system (OS) is a collection of system programs designed to supervise and control
the operations of a computer system. In simpler terms, it serves as the infrastructure software
responsible for managing and coordinating activities and enabling resource sharing among the
computer's components. The OS acts as a bridge between hardware, users, and applications.

The core of an operating system, known as the "kernel," operates in a privileged state. It handles
tasks such as reacting to interrupts from external devices, servicing requests from applications,
and ensuring smooth communication between hardware and software components.

Figure 1: Architecture of an Operating System

Roles of an Operating System

The operating system plays a critical role in managing system resources and ensuring efficient
operation. Key roles include:

 Providing the user interface: Facilitating interaction between users and the computer.
 Resource sharing: Enabling multiple users to access hardware efficiently.
 Data sharing: Allowing users to exchange and share data securely.
 User isolation: Preventing interference among users to ensure stability and security.
 Resource scheduling: Managing and allocating resources to different processes
effectively.
Functions of an Operating System

OS as an Extended Machine

 The operating system provides a high level of abstraction for programmers, simplifying
interactions with hardware.
o Example: Floppy Disk I/O Operation
 The disk contains a list of named files.
 Each file can be opened for reading or writing.
 Once the read/write operation is complete, the file is closed.
 The programmer does not need to manage low-level hardware details.
 This abstraction conceals the complexity of hardware operations, presenting a simplified view
where files can be easily accessed.
 The operating system acts as a virtual machine or an extended machine, making programming
more straightforward compared to working directly with hardware.

OS as a Resource Manager

 The operating system ensures efficient management of system resources, handling potential
conflicts and maximizing utilization:
o Example Scenarios:
 When three programs attempt to print to the same printer simultaneously.
 When two network users try to update the same shared document.
 Key responsibilities of the operating system include:
o Keeping track of resource usage and user requests.
o Mediating conflicting requests to ensure smooth operation.
o Virtualizing resources so multiple users or programs can share them seamlessly.
o Protecting applications and users from interfering with each other.
1. Simulate Producer-Consumer Problem Using Semaphore in C
#include <stdio.h>

#include <stdlib.h>

int mutex = 1, empty = 5, full = 0, buffer[5], in = 0, out = 0;

void wait(int *s) { (*s)--; }

void signal(int *s) { (*s)++; }

void producer() {

wait(&empty); wait(&mutex);

buffer[in] = rand() % 100;

printf("Produced: %d\n", buffer[in]);

in = (in + 1) % 5;

signal(&mutex); signal(&full);

void consumer() {

wait(&full); wait(&mutex);

printf("Consumed: %d\n", buffer[out]);

out = (out + 1) % 5;

signal(&mutex); signal(&empty);

int main() {

int choice;

while (1) {

printf("1. Produce 2. Consume 3. Exit\nChoice: ");

scanf("%d", &choice);

if (choice == 1) producer();

else if (choice == 2) consumer();

else break; }

return 0;

}
2. Simulate First Come First Serve (FCFS) Scheduling in C
#include <stdio.h>

void fcfs(int processes[], int n, int burst_time[]) {

int wait_time[n], turn_around_time[n];

int total_wait_time = 0, total_turnaround_time = 0;

wait_time[0] = 0; // First process has no waiting time.

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

wait_time[i] = burst_time[i - 1] + wait_time[i - 1];

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

turn_around_time[i] = burst_time[i] + wait_time[i];

total_wait_time += wait_time[i];

total_turnaround_time += turn_around_time[i];

printf("Process\tBurst Time\tWaiting Time\tTurnaround Time\n");

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

printf("%d\t\t%d\t\t%d\t\t%d\n", processes[i], burst_time[i], wait_time[i], turn_around_time[i]);

printf("Average waiting time: %.2f\n", (float)total_wait_time / n);

printf("Average turnaround time: %.2f\n", (float)total_turnaround_time / n);

int main() {

int n = 4;

int processes[] = {1, 2, 3, 4};

int burst_time[] = {5, 8, 12, 6};

fcfs(processes, n, burst_time);

return 0;

}
3. Simulate Shortest Job First (SJF) Scheduling in C
#include <stdio.h>

#include <stdlib.h>

void sjf(int processes[], int n, int burst[]) {

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

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

if (burst[j] > burst[j + 1]) {

int t = burst[j]; burst[j] = burst[j + 1]; burst[j + 1] = t;

t = processes[j]; processes[j] = processes[j + 1]; processes[j + 1] = t;

int wait = 0, turnaround = 0;

printf("Process\tBurst\tWait\tTurnaround\n");

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

printf("%d\t%d\t%d\t%d\n", processes[i], burst[i], wait, wait + burst[i]);

turnaround += (wait += burst[i]);

printf("Average Wait: %.2f, Turnaround: %.2f\n", (float)turnaround / n - (float)burst[n - 1] / n,


(float)turnaround / n);

int main() {

int processes[] = {1, 2, 3}, burst[] = {6, 8, 7};

sjf(processes, 3, burst);

return 0;

}
4. Simulate Priority Scheduling in C
#include <stdio.h>

#include <stdlib.h>

void priorityScheduling(int processes[], int n, int burst[], int priority[]) {

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

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

if (priority[j] > priority[j + 1]) {

int t = priority[j]; priority[j] = priority[j + 1]; priority[j + 1] = t;

t = burst[j]; burst[j] = burst[j + 1]; burst[j + 1] = t;

t = processes[j]; processes[j] = processes[j + 1]; processes[j + 1] = t;

int wait = 0, turnaround = 0;

printf("Process\tBurst\tPriority\tWait\tTurnaround\n");

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

printf("%d\t%d\t%d\t\t%d\t%d\n", processes[i], burst[i], priority[i], wait, wait + burst[i]);

turnaround += (wait += burst[i]);

printf("Average Wait: %.2f, Turnaround: %.2f\n", (float)turnaround / n - (float)burst[n - 1] / n,


(float)turnaround / n);

int main() {

int processes[] = {1, 2, 3}, burst[] = {6, 8, 7}, priority[] = {2, 1, 3};

priorityScheduling(processes, 3, burst, priority);

return 0;

}
5. Simulate Round Robin Scheduling in C
#include <stdio.h>

void roundRobin(int processes[], int n, int burst[], int quantum) {

int remaining[n], wait = 0, turnaround = 0, time = 0;

for (int i = 0; i < n; i++) remaining[i] = burst[i];

printf("Process\tBurst\tTurnaround\tWait\n");

while (1) {

int done = 1;

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

if (remaining[i] > 0) {

done = 0;

if (remaining[i] > quantum) {

time += quantum; remaining[i] -= quantum;

} else {

time += remaining[i];

printf("%d\t%d\t%d\t\t%d\n", processes[i], burst[i], time, time - burst[i]);

remaining[i] = 0;

wait += time - burst[i];

turnaround += time;

}}}

if (done) break;

printf("Average Wait: %.2f, Turnaround: %.2f\n", (float)wait / n, (float)turnaround / n);

int main() {

int processes[] = {1, 2, 3}, burst[] = {10, 5, 8};

roundRobin(processes, 3, burst, 2);

return 0;

}
6. Simulate the First Fit Algorithm in C
#include <stdio.h>

void firstFit(int blocks[], int m, int processes[], int n) {

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

int allocated = 0;

for (int j = 0; j < m && !allocated; j++) {

if (blocks[j] >= processes[i]) {

printf("Process %d (Size %d) -> Block %d\n", i + 1, processes[i], j + 1);

blocks[j] -= processes[i];

allocated = 1;

if (!allocated) printf("Process %d (Size %d) -> Not Allocated\n", i + 1, processes[i]);

int main() {

int blocks[] = {100, 500, 200, 300, 600};

int processes[] = {212, 417, 112, 426};

firstFit(blocks, 5, processes, 4);

return 0;

}
7. Simulate the Best Fit Algorithm in C
#include <stdio.h>

void bestFit(int blocks[], int m, int processes[], int n) {

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

int bestIdx = -1;

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

if (blocks[j] >= processes[i] && (bestIdx == -1 || blocks[j] < blocks[bestIdx])) {

bestIdx = j;

if (bestIdx != -1) {

printf("Process %d (Size %d) -> Block %d\n", i + 1, processes[i], bestIdx + 1);

blocks[bestIdx] -= processes[i];

} else {

printf("Process %d (Size %d) -> Not Allocated\n", i + 1, processes[i]);

int main() {

int blocks[] = {100, 500, 200, 300, 600};

int processes[] = {212, 417, 112, 426};

bestFit(blocks, 5, processes, 4);

return 0;

}
8. Simulate the Worst Fit Algorithm in C

#include <stdio.h>

void worstFit(int blocks[], int m, int processes[], int n) {


for (int i = 0; i < n; i++) {
int worstIdx = -1;
for (int j = 0; j < m; j++) {
if (blocks[j] >= processes[i] && (worstIdx == -1 || blocks[j] > blocks[worstIdx])) {
worstIdx = j;
}
}
if (worstIdx != -1) {
printf("Process %d (Size %d) -> Block %d\n", i + 1, processes[i], worstIdx + 1);
blocks[worstIdx] -= processes[i];
} else {
printf("Process %d (Size %d) -> Not Allocated\n", i + 1, processes[i]);
}
}
}

int main() {
int blocks[] = {100, 500, 200, 300, 600};
int processes[] = {212, 417, 112, 426};
worstFit(blocks, 5, processes, 4);
return 0;
}
9. FIFO Page Replacement
#include <stdio.h>

void fifoPageReplacement(int pages[], int n, int frames[], int m) {

int index = 0, pageFaults = 0;

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

int found = 0;

for (int j = 0; j < m; j++) if (frames[j] == pages[i]) found = 1;

if (!found) {

frames[index] = pages[i];

index = (index + 1) % m;

pageFaults++;

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

for (int j = 0; j < m; j++) printf(frames[j] != -1 ? "%d " : "- ", frames[j]);

printf("\n");

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

int main() {

int pages[] = {1, 3, 0, 3, 5, 6, 3};

int frames[] = {-1, -1, -1};

fifoPageReplacement(pages, 7, frames, 3);

return 0;

}
10. LRU Page Replacement

#include <stdio.h>

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


int time[m], pageFaults = 0, counter = 0;

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


int found = 0, lruIdx = 0;
for (int j = 0; j < m; j++) if (frames[j] == pages[i]) { found = 1; time[j] = counter++; }
if (!found) {
for (int j = 1; j < m; j++) if (time[j] < time[lruIdx]) lruIdx = j;
frames[lruIdx] = pages[i];
time[lruIdx] = counter++;
pageFaults++;
}
printf("Page %d -> ", pages[i]);
for (int j = 0; j < m; j++) printf(frames[j] != -1 ? "%d " : "- ", frames[j]);
printf("\n");
}
printf("Total Page Faults: %d\n", pageFaults);
}

int main() {
int pages[] = {1, 3, 0, 3, 5, 6, 3};
int frames[] = {-1, -1, -1};
lruPageReplacement(pages, 7, frames, 3);
return 0;
}
11. Simulate Optimal Page Replacement (OPR)

#include <stdio.h>
int findOptimal(int pages[], int n, int frames[], int m, int index) {
int farthest = -1, pos = -1;
for (int i = 0; i < m; i++) {
int found = 0;
for (int j = index; j < n; j++) {
if (frames[i] == pages[j]) {
if (j > farthest) {
farthest = j;
pos = i;
}
found = 1;
break;
}
}
if (!found) return i;
}
return pos == -1 ? 0 : pos;
} void optimalPageReplacement(int pages[], int n, int frames[], int m) {
int pageFaults = 0;

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

int found = 0;
for (int j = 0; j < m; j++) if (frames[j] == pages[i]) found = 1;

if (!found) {
int replaceIdx = (i < m) ? i : findOptimal(pages, n, frames, m, i + 1);
frames[replaceIdx] = pages[i];
pageFaults++;
}
printf("Page %d -> ", pages[i]);
for (int j = 0; j < m; j++) printf(frames[j] != -1 ? "%d " : "- ", frames[j]);
printf("\n");
}
printf("Total Page Faults: %d\n", pageFaults);
}
int main() {
int pages[] = {1, 3, 0, 3, 5, 6, 3};
int frames[] = {-1, -1, -1};
optimalPageReplacement(pages, 7, frames, 3);
return 0;
}
12. Simulate FCFS Disk Scheduling in C

#include <stdio.h>

void fcfsDiskScheduling(int requests[], int n, int start) {


int seekTime = 0;
printf("Track Movement: %d", start);

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


seekTime += abs(requests[i] - start);
start = requests[i];
printf(" -> %d", start);
}

printf("\nTotal Seek Time: %d\n", seekTime);


}

int main() {
int requests[] = {98, 183, 37, 122, 14, 124, 65, 67};
int start = 53;
fcfsDiskScheduling(requests, 8, start);
return 0;
}
13. Simulate SCAN Disk Scheduling

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

void scanDiskScheduling(int requests[], int n, int start, int max) {


int seekTime = 0, dir = 1;
requests[n] = start;
requests[n + 1] = (dir == 1) ? max : 0;
n += 2;

qsort(requests, n, sizeof(int), (int (*)(const void *, const void *))cmp);

int idx = 0;
while (requests[idx] < start) idx++;
printf("Track Movement: %d", start);

for (int i = idx; i < n; i++) { // Move in current direction


seekTime += abs(requests[i] - start);
start = requests[i];
printf(" -> %d", start);
}

for (int i = idx - 1; i >= 0; i--) {


seekTime += abs(requests[i] - start);
start = requests[i];
printf(" -> %d", start);
}

printf("\nTotal Seek Time: %d\n", seekTime);


}

int main() {
int requests[] = {98, 183, 37, 122, 14, 124, 65, 67};
scanDiskScheduling(requests, 8, 53, 200);
return 0;
}
14. Simulate Sequential File Allocation

#include <stdio.h>

void sequentialFileAllocation(int files[], int n) {


printf("File\tStart\tEnd\n");
int start = 0;

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


printf("%d\t%d\t%d\n", files[i], start, start + files[i] - 1);
start += files[i];
}
}

int main() {
int files[] = {10, 20, 15, 30};
sequentialFileAllocation(files, 4);
return 0;
}
15. Simulate Single-Level Directory Organization

#include <stdio.h>
#include <string.h>

void singleLevelDirectory(char files[][20], int n) {


printf("File List:\n");
for (int i = 0; i < n; i++) printf("%d. %s\n", i + 1, files[i]);
}

int main() {
char files[][20] = {"file1.txt", "file2.txt", "file3.txt", "file4.txt"};
singleLevelDirectory(files, 4);
return 0;
}

You might also like