Operating system
Operating system
COLLEGE
Lagankhel, Lalitpur
Tribhuvan University
Report on
Operating System
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.
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>
void producer() {
wait(&empty); wait(&mutex);
in = (in + 1) % 5;
signal(&mutex); signal(&full);
void consumer() {
wait(&full); wait(&mutex);
out = (out + 1) % 5;
signal(&mutex); signal(&empty);
int main() {
int choice;
while (1) {
scanf("%d", &choice);
if (choice == 1) producer();
else break; }
return 0;
}
2. Simulate First Come First Serve (FCFS) Scheduling in C
#include <stdio.h>
total_wait_time += wait_time[i];
total_turnaround_time += turn_around_time[i];
int main() {
int n = 4;
fcfs(processes, n, burst_time);
return 0;
}
3. Simulate Shortest Job First (SJF) Scheduling in C
#include <stdio.h>
#include <stdlib.h>
printf("Process\tBurst\tWait\tTurnaround\n");
int main() {
sjf(processes, 3, burst);
return 0;
}
4. Simulate Priority Scheduling in C
#include <stdio.h>
#include <stdlib.h>
printf("Process\tBurst\tPriority\tWait\tTurnaround\n");
int main() {
int processes[] = {1, 2, 3}, burst[] = {6, 8, 7}, priority[] = {2, 1, 3};
return 0;
}
5. Simulate Round Robin Scheduling in C
#include <stdio.h>
printf("Process\tBurst\tTurnaround\tWait\n");
while (1) {
int done = 1;
if (remaining[i] > 0) {
done = 0;
} else {
time += remaining[i];
remaining[i] = 0;
turnaround += time;
}}}
if (done) break;
int main() {
return 0;
}
6. Simulate the First Fit Algorithm in C
#include <stdio.h>
int allocated = 0;
blocks[j] -= processes[i];
allocated = 1;
int main() {
return 0;
}
7. Simulate the Best Fit Algorithm in C
#include <stdio.h>
bestIdx = j;
if (bestIdx != -1) {
blocks[bestIdx] -= processes[i];
} else {
int main() {
return 0;
}
8. Simulate the Worst Fit Algorithm in C
#include <stdio.h>
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>
int found = 0;
if (!found) {
frames[index] = pages[i];
index = (index + 1) % m;
pageFaults++;
for (int j = 0; j < m; j++) printf(frames[j] != -1 ? "%d " : "- ", frames[j]);
printf("\n");
int main() {
return 0;
}
10. LRU Page Replacement
#include <stdio.h>
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;
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>
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>
int idx = 0;
while (requests[idx] < start) idx++;
printf("Track Movement: %d", start);
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>
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>
int main() {
char files[][20] = {"file1.txt", "file2.txt", "file3.txt", "file4.txt"};
singleLevelDirectory(files, 4);
return 0;
}