Operating System Fundamentals and Linux Essentials
Operating System Concepts: Key Differences
This section outlines the differences between core operating system concepts:
- Preemptive and Non-Preemptive Scheduling
- Program and Process
- Hard Real-Time Systems and Soft Real-Time Systems
- Time Sharing and Multiprogramming
Preemptive vs. Non-Preemptive Scheduling
| Point | Preemptive Scheduling | Non-Preemptive Scheduling |
|---|---|---|
| 1 | CPU can be taken away from a running process before completion. | CPU cannot be taken away; the process runs until it finishes or waits. |
| 2 | Better response time but higher overhead (due to context switching). | Less overhead but poor response for short processes. |
| 3 | Examples: Round Robin, Shortest Remaining Time First (SRTF). | Examples: First Come First Serve (FCFS), Shortest Job First (SJF). |
Program vs. Process
| Point | Program | Process |
|---|---|---|
| 1 | A passive set of instructions stored on disk. | An active instance of a program in execution. |
| 2 | Has no state (passive). | Has states like Ready, Running, Waiting. |
| 3 | Example: MS Word file on disk. | Example: Running MS Word on CPU. |
Hard Real-Time Systems vs. Soft Real-Time Systems
| Point | Hard Real-Time Systems | Soft Real-Time Systems |
|---|---|---|
| 1 | Deadlines are strict; missing them causes system failure. | Deadlines are flexible; delays are tolerable. |
| 2 | Used in safety-critical applications. | Used in non-critical applications. |
| 3 | Example: Airbag system, pacemaker. | Example: Video streaming, online games. |
Time Sharing vs. Multiprogramming
| Point | Time Sharing | Multiprogramming |
|---|---|---|
| 1 | CPU switches rapidly among processes to give quick response. | CPU executes one process while others wait for I/O. |
| 2 | Focus on interaction with users. | Focus on maximum CPU utilization. |
| 3 | Example: Modern operating systems (Windows, Linux). | Example: Early batch operating systems. |
Essential Linux Commands Explained
1. ls
- Lists all files and directories in the current folder.
- Can show detailed information like permissions, size, and date.
- Helps users navigate and manage files efficiently.
2. mv
- Moves files or directories from one location to another.
- Can rename files or directories.
- Useful for organizing files within the system.
3. pwd
- Stands for print working directory.
- Displays the full path of the current directory.
- Helps users know their exact location in the file system.
4. cat
- Displays the contents of a file on the screen.
- Can combine multiple files into one.
- Useful for quickly reading or creating files.
5. man
- Stands for manual, shows documentation of commands.
- Provides syntax, options, and examples.
- Helps users learn and understand commands without external help.
6. cp
- Copies files or directories from one location to another.
- Preserves the original file while creating a duplicate.
- Useful for backup or transferring data safely.
Linux Operating System Architecture
Linux is a modular, multitasking, and multiuser operating system with four main layers:
1. User Space (Application Layer)
- Where users interact with the system.
- Includes programs like browsers, text editors, and office software.
- Accessed via shells or GUI, acting as a bridge to the kernel.
2. Shell / Command Interpreter
- Interface between user and kernel.
- Converts commands into kernel instructions.
- Provides features like scripting and command history.
3. Kernel Space (Core Layer)
- Heart of Linux, manages hardware and system resources.
- Components include: process management, memory management, file system, device drivers, and system calls.
- Linux uses a monolithic kernel with modular support.
4. Hardware Layer
- Physical components like CPU, memory, storage, and I/O devices.
- Kernel interacts directly via device drivers.
- Provides abstraction for applications.
Definition of an Operating System (OS)
An Operating System (OS) is a system software that acts as an interface between the user and the computer hardware. It manages computer resources and allows users and applications to interact with the hardware efficiently.
Core Functions of an Operating System
Process Management
- Handles creation, scheduling, and termination of processes.
- Ensures efficient CPU utilization and multitasking.
Memory Management
- Allocates and deallocates memory to processes.
- Keeps track of available and used memory for efficient operation.
File System Management
- Organizes and controls files and directories.
- Provides access permissions and storage management.
Device Management
- Controls input/output devices like printer, keyboard, and disk.
- Uses device drivers for hardware communication.
Security and Access Control
- Protects system and data from unauthorized access.
- Implements user authentication and permissions.
User Interface
- Provides an interface for user interaction via command line (CLI) or GUI.
Networking and Communication
- Manages network connections and data transfer between computers.
Linux File System Hierarchy (FHS)
Linux uses a hierarchical directory structure, starting from the root directory ( / ). Every file and directory in Linux stems from /. Below are the key directories:
1. / (Root Directory)
- The top-level directory.
- Contains all other files and directories.
2. /bin
- Contains essential binary executables needed for booting and basic operations.
- Example:
ls,cp,mv.
3. /sbin
- Contains system administration commands for the root user.
- Example:
ifconfig,reboot.
4. /home
- User home directories are located here.
- Example:
/home/yashfor user Yash.
5. /etc
- Contains system configuration files and startup scripts.
- Example: network configuration, password files.
6. /var
- Stores variable data like logs, mail, and spool files.
- Example:
/var/logfor system logs.
7. /tmp
- Stores temporary files created by the system or users.
- Cleared automatically on reboot in some distributions.
8. /usr
- Contains user programs, libraries, and documentation.
- Example:
/usr/binfor non-essential binaries.
9. /lib
- Contains shared libraries required by binaries in
/binand/sbin.
10. /dev
- Contains device files representing hardware devices.
- Example:
/dev/sdafor a hard disk.
11. /opt
- Used for optional or third-party software packages.
12. /boot
- Contains boot loader files and kernel images needed for system startup.
Vi Editor: Understanding the Three Modes
The vi editor has three modes:
Command Mode
- Default mode when you open a file.
- You can navigate, delete, copy, paste, or search text.
- You cannot insert text in this mode.
Insert Mode
- Used to insert or edit text.
- Enter this mode by pressing keys like
i,a, orofrom command mode. - Press
Escto return to command mode.
Ex/Last Line Mode
- Used to save files, quit, or execute commands.
- Entered by pressing
:from command mode. - Example commands:
:w(write/save),:q(quit),:wq(write and quit).
Shell Scripting Workflow using Vi Editor
Step 1: Write a Shell Script
- Open vi with a filename:
vi script.sh - Switch to insert mode by pressing
i. - Type the shell script content, e.g.:
#!/bin/bash echo "Hello World"
Step 2: Save the Script
- Press
Escto go to command mode. - Type
:wto save the file. - Type
:qto quit vi. - Or combine as
:wqto save and quit in one command.
Step 3: Make Script Executable
- Use the command:
chmod +x script.sh
Step 4: Execute the Script
- Run the script using:
./script.sh - Output:
Hello World
Operating System Types and Concepts
1. Batch Processing
- Jobs are collected and executed in batches without user interaction.
- Efficient for large, repetitive tasks like payroll or bill processing.
- Advantage: Saves time by grouping similar jobs.
- Limitation: No real-time interaction with the user.
2. Multiprocessing
- A system with two or more processors (CPUs) working together.
- Processes can run in parallel, improving performance and reliability.
- Advantage: Faster execution, fault tolerance.
- Example: Modern servers with multiple cores.
3. Distributed Systems
- A collection of independent computers working together as a single system.
- Resources and tasks are shared via a network.
- Advantage: High scalability and reliability.
- Example: Cloud computing, Google search system.
4. Time Sharing
- CPU time is shared among multiple users/processes.
- Provides quick response by switching between tasks rapidly.
- Advantage: Interactive use of the system by many users at once.
- Example: Online systems, terminals connected to a server.
5. Multiprogramming
- Multiple programs kept in memory; the CPU switches to another program when one waits for I/O.
- Goal is to maximize CPU utilization.
- Advantage: Reduces CPU idle time.
- Limitation: No guaranteed quick response for users.
6. Scheduling Criteria
When the OS decides which process to run, it uses certain criteria:
CPU Utilization – Keep CPU as busy as possible.
Throughput – Number of processes completed per unit time.
Turnaround Time – Total time taken from submission to completion of a process.
Waiting Time – Time a process spends waiting in the ready queue.
Response Time – Time taken to give the first response to a user request.
Fairness – All processes get a fair share of the CPU.
Process State Transition Diagram
In an operating system, a process goes through different states during its execution. The diagram shows how a process transitions between these states.
Main Process States
New – Process is being created.
Ready – Process is waiting in the ready queue to be assigned to the CPU.
Running – Process is being executed by the CPU.
Waiting (or Blocked) – Process is waiting for some I/O operation or event to complete.
Terminated (Exit) – Process has finished execution.
State Transitions
- New → Ready: Process admitted into memory, ready to run.
- Ready → Running: Scheduler selects the process for the CPU.
- Running → Ready: Process is interrupted (preemption by scheduler).
- Running → Waiting: Process requests I/O or waits for an event.
- Waiting → Ready: I/O or event completes; process moves back to the ready queue.
- Running → Terminated: Process finishes execution.
Explanation in Words
- When a program starts, it is New.
- Once loaded into memory, it becomes Ready.
- The CPU scheduler picks it, and it goes to Running.
- If it needs I/O → it moves to Waiting.
- When I/O completes → it goes back to Ready.
- If the CPU is taken away (preemption) → it goes back to Ready.
- Once execution finishes → it goes to Terminated.
Types of OS Scheduling
1. Long-Term Scheduling
- Role: Controls the admission of processes into the system.
- Decides which jobs (programs) will be loaded into memory for processing.
- Keeps the balance between CPU-bound and I/O-bound processes.
- Happens less frequently.
2. Medium-Term Scheduling
- Role: Temporarily suspends or resumes processes.
- Used to improve CPU and memory utilization.
- Suspended processes may be swapped out to reduce load.
- Acts as a regulator between long-term and short-term scheduling.
3. Short-Term Scheduling
- Role: Decides which process in the ready queue gets the CPU next.
- Happens very frequently (in milliseconds).
- Directly affects system responsiveness and CPU utilization.
Summary Table: OS Scheduling Types
| Type of Scheduling | Role | Frequency |
|---|---|---|
| Long-Term | Controls job admission to system | Low |
| Medium-Term | Suspends/resumes processes | Medium |
| Short-Term | Selects process for CPU | High |
Lightweight Process (LWP) Explained
- A Lightweight Process (LWP) is essentially a thread in an operating system.
- It is called “lightweight” because, unlike a traditional (heavyweight) process, it shares most of its resources (such as code, data, open files) with other threads of the same process.
- Only minimal information like program counter, registers, and stack is unique for each LWP.
Why it is called Lightweight?
Less overhead – Creating and managing LWPs (threads) requires fewer resources compared to creating a full process.
Faster context switching – Switching between LWPs is quicker than switching between heavyweight processes.
Shared resources – LWPs use shared memory and files of the parent process, avoiding duplication.
In short: A Lightweight Process = Thread. It’s called lightweight because it needs fewer resources and less time to manage compared to a normal (heavyweight) process.
