Operating systems (OS) are crucial for managing computer hardware and software resources. They provide a stable and consistent way for applications to interact with the hardware without needing to know all the details of the hardware. This section will cover the main functions of an operating system, which include process management, memory management, storage management, and device management.
Key Functions of an Operating System
- Process Management
Process management involves handling the processes in a system. A process is a program in execution, and the OS is responsible for:
- Process Scheduling: Deciding which process gets the CPU and for how long.
- Process Creation and Termination: Creating new processes and terminating them when they are no longer needed.
- Process Synchronization: Ensuring that processes do not interfere with each other.
- Inter-process Communication (IPC): Allowing processes to communicate and synchronize their actions.
Example: Process Scheduling
// Simplified example of a Round Robin scheduling algorithm #include <stdio.h> #define TIME_QUANTUM 4 void roundRobin(int processes[], int n, int burst_time[]) { int remaining_time[n]; for (int i = 0; i < n; i++) { remaining_time[i] = burst_time[i]; } 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 is a pending process if (remaining_time[i] > TIME_QUANTUM) { t += TIME_QUANTUM; remaining_time[i] -= TIME_QUANTUM; } else { t += remaining_time[i]; remaining_time[i] = 0; } printf("Process %d executed at time %d\n", processes[i], t); } } if (done == 1) break; } } int main() { int processes[] = {1, 2, 3}; int n = sizeof processes / sizeof processes[0]; int burst_time[] = {10, 5, 8}; roundRobin(processes, n, burst_time); return 0; }
Explanation: This code snippet demonstrates a simple Round Robin scheduling algorithm where each process gets a fixed time quantum to execute.
- Memory Management
Memory management is the function of an OS that handles or manages primary memory. It keeps track of each byte in a computer's memory and manages the allocation and deallocation of memory spaces as needed by various programs.
- Memory Allocation: Allocating memory to processes when they need it.
- Memory Deallocation: Releasing memory when it is no longer needed.
- Paging and Segmentation: Techniques to manage memory more efficiently.
Example: Memory Allocation
#include <stdio.h> #include <stdlib.h> int main() { int *ptr; int n, i; n = 5; printf("Enter number of elements: %d\n", n); ptr = (int*)malloc(n * sizeof(int)); if (ptr == NULL) { printf("Memory not allocated.\n"); exit(0); } else { printf("Memory successfully allocated using malloc.\n"); for (i = 0; i < n; ++i) { ptr[i] = i + 1; } printf("The elements of the array are: "); for (i = 0; i < n; ++i) { printf("%d ", ptr[i]); } } free(ptr); return 0; }
Explanation: This code demonstrates dynamic memory allocation using malloc
and free
in C.
- Storage Management
Storage management involves managing data storage, which includes the file system and disk management.
- File System Management: Organizing and managing files on storage devices.
- Disk Scheduling: Deciding the order in which disk I/O requests are processed.
- Space Management: Managing the space on storage devices.
Example: File System Management
import os # Create a new directory os.mkdir('new_directory') # List directories and files print("List of directories and files:", os.listdir()) # Create a new file with open('new_directory/new_file.txt', 'w') as file: file.write("Hello, World!") # Read the file with open('new_directory/new_file.txt', 'r') as file: print(file.read())
Explanation: This Python script demonstrates basic file system operations such as creating a directory, listing files, and reading/writing a file.
- Device Management
Device management is responsible for managing all hardware devices of the computer system. The OS acts as an intermediary between the hardware and the application programs.
- Device Drivers: Software that allows the OS to communicate with hardware devices.
- I/O Management: Managing input and output operations.
- Interrupt Handling: Handling interrupts generated by hardware devices.
Example: Device Management
import psutil # Get the list of all disk partitions partitions = psutil.disk_partitions() # Print details of each partition for partition in partitions: print(f"Device: {partition.device}") print(f"Mountpoint: {partition.mountpoint}") print(f"File system type: {partition.fstype}") print("") # Get disk usage statistics usage = psutil.disk_usage('/') print(f"Total: {usage.total} bytes") print(f"Used: {usage.used} bytes") print(f"Free: {usage.free} bytes") print(f"Percentage: {usage.percent}%")
Explanation: This Python script uses the psutil
library to manage and retrieve information about disk partitions and usage.
Summary
In this section, we covered the main functions of an operating system, including process management, memory management, storage management, and device management. Each function is crucial for the efficient operation of a computer system, ensuring that resources are allocated and managed effectively. Understanding these functions provides a solid foundation for delving deeper into the complexities of operating systems.
Next, we will explore resource management in more detail, starting with process management.
Fundamentals of Operating Systems
Module 1: Introduction to Operating Systems
- Basic Concepts of Operating Systems
- History and Evolution of Operating Systems
- Types of Operating Systems
- Main Functions of an Operating System
Module 2: Resource Management
Module 3: Concurrency
- Concepts of Concurrency
- Threads and Processes
- Synchronization and Mutual Exclusion
- Classic Concurrency Problems