Unit 1 - Notes
CSE316
Unit 1: Introduction to Operating System and Process Management
1. Operating System Overview
Meaning and Definition
An Operating System (OS) is system software that manages computer hardware, software resources, and provides common services for computer programs. It acts as an intermediary between the user of a computer and the computer hardware.
- Primary Goal: Convenience (Making the computer easier to use).
- Secondary Goal: Efficiency (Using computer resources like CPU and memory in an efficient manner).
Functions of an Operating System
- Process Management: Creating, scheduling, and terminating processes. Handling synchronization and deadlock.
- Memory Management: Allocation and deallocation of memory space to programs.
- File Management: Creating, deleting, reading, and writing files and directories.
- Device Management (I/O): Managing device drivers and buffers for hardware peripherals.
- Security and Protection: Protecting data from unauthorized access and ensuring distinct processes do not interfere with each other.
- Command Interpretation: Interpreting user commands (via CLI or GUI) and acting on them.
2. Hardware Protection: Supervisor & User Mode
To ensure proper operation, the OS must distinguish between the execution of operating system code and user-defined code. This is achieved through Dual Mode Operation.
Modes of Operation
- User Mode (Bit = 1):
- Used when the computer is executing user applications.
- Restricted access to hardware instructions.
- Cannot execute privileged instructions (e.g., handling interrupts, switching modes).
- Supervisor Mode (Kernel/System/Monitor Mode) (Bit = 0):
- Used when the operating system code is running.
- Has unrestricted access to all hardware instructions and memory.
- Privileged instructions can only be executed in this mode.
Mode Transition
- User to Kernel: Occurs via a System Call (software interrupt) or a hardware interrupt/trap/exception. The mode bit flips from 1 to 0.
- Kernel to User: Occurs when the OS finishes servicing the request and returns control to the application. The mode bit flips from 0 to 1.
3. Evolution and Types of Operating Systems
The evolution of operating systems correlates with the development of hardware architectures.
A. Simple Batch Systems
- Concept: Jobs with similar needs were batched together and executed through a card reader.
- Operation: The operator sorts programs into batches with similar requirements. The CPU executes one job after another without user interaction.
- Drawback: High CPU idle time. If a job performs I/O, the CPU sits idle.
B. Multiprogramming Systems
- Concept: Keeps several jobs in memory simultaneously to maximize CPU utilization.
- Operation: The OS picks a job and begins to execute it. If that job needs to wait (e.g., for I/O), the CPU switches to another job.
- Key Benefit: Efficient use of CPU; the CPU is rarely idle.
C. Multitasking (Time-Sharing) Systems
- Concept: A logical extension of multiprogramming. The CPU switches jobs so frequently that users can interact with each program while it is running.
- Mechanism: Uses Time Slicing (Quantum). Each process gets a small amount of CPU time.
- Key Benefit: Response time is rapid; allows for interactive computing.
D. Multiprocessing Systems
- Concept: Systems with two or more processors (CPUs) in close communication, sharing the computer bus, clock, and sometimes memory.
- Types:
- Symmetric Multiprocessing (SMP): Each processor runs an identical copy of the OS. All processors are peers.
- Asymmetric Multiprocessing: Master-slave relationship. The master processor schedules and controls the slave processors.
- Benefits: Increased throughput, economy of scale, and increased reliability (fault tolerance).
E. Parallel Systems
- Concept: Tightly coupled systems where processors share memory and a clock. They are designed to solve a single complex problem by breaking it into smaller parts.
- Focus: High performance and speed.
F. Distributed Systems
- Concept: A collection of physically separate, possibly heterogeneous computer systems that are networked to provide the users with access to the various resources that the system maintains.
- Characteristics: Loosely coupled. Each processor has its own local memory.
- Benefits: Resource sharing, load balancing, reliability.
G. Real-Time Operating Systems (RTOS)
- Concept: Used when there are rigid time requirements on the operation of a processor or the flow of data.
- Hard Real-Time: Critical tasks must complete on time (e.g., pacemaker, flight control). Missing a deadline is a system failure.
- Soft Real-Time: A critical real-time task gets priority over other tasks and retains that priority until it completes (e.g., video streaming).
4. Operating System Structure
Simple Structure (Monolithic)
- No well-defined structure. Application programs have direct access to I/O routines.
- Example: MS-DOS.
- Pros: Fast.
- Cons: Difficult to debug/maintain; one crash crashes the whole system.
Layered Approach
- The OS is broken into a number of pieces (layers).
- Layer 0: Hardware.
- Layer N: User Interface.
- Each layer uses functions and services of only lower-level layers.
- Pros: Easier debugging (verify layer 0, then layer 1, etc.).
Microkernel
- Moves as much functionality as possible from the kernel into "user space."
- The kernel only handles minimal IPC, memory, and CPU scheduling.
- Example: Mach, QNX.
- Pros: More secure and reliable (if a service crashes, the kernel stays up).
- Cons: Performance overhead due to increased system function communication.
5. System Calls
The System Call provides the interface between a running program (user mode) and the operating system (kernel mode).
Mechanism
- User program calls a function (e.g.,
open()). - A trap (software interrupt) is triggered.
- Control switches to Kernel Mode.
- OS executes the specific service code.
- Control returns to User Mode.
Types of System Calls
- Process Control:
load,execute,end,abort,create process,terminate process. - File Management:
create file,delete file,open,close,read,write. - Device Management:
request device,release device,read,write. - Information Maintenance:
get time or date,set time or date,get system data. - Communications:
create connection,delete connection,send message,receive message.
6. Process Management
Process Concept
A process is a program in execution. A program is a passive entity (stored on disk), while a process is an active entity.
Memory Layout of a Process:
- Text Section: The compiled program code.
- Data Section: Global and static variables.
- Heap: Dynamically allocated memory during run time.
- Stack: Temporary data (function parameters, return addresses, local variables).
Process Life Cycle (Process States)
As a process executes, it changes state. The standard 5-state model includes:
- New: The process is being created.
- Ready: The process is waiting to be assigned to a processor. It is in the main memory.
- Running: Instructions are being executed (the CPU is working on this process).
- Waiting (Blocked): The process is waiting for some event to occur (such as an I/O completion or reception of a signal).
- Terminated: The process has finished execution.
State Transitions:
- Admitted: New Ready
- Scheduler Dispatch: Ready Running
- Interrupt: Running Ready (e.g., time slice expired)
- I/O or Event Wait: Running Waiting
- I/O or Event Completion: Waiting Ready
- Exit: Running Terminated
Process Control Block (PCB)
The OS maintains a data structure for each process called the PCB (also known as the Task Control Block). It serves as the repository for any information that may vary from process to process.
Contents of PCB:
- Process State: (Running, waiting, etc.)
- Program Counter: Address of the next instruction to execute.
- CPU Registers: Accumulators, index registers, stack pointers.
- CPU Scheduling Information: Priority, pointers to scheduling queues.
- Memory-Management Information: Base and limit registers, page tables.
- Accounting Information: CPU used, time limits, account numbers.
- I/O Status Information: List of I/O devices allocated, list of open files.
7. Operations on Processes
1. Process Creation
- A process may create several new processes.
- Parent Process: The creating process.
- Child Process: The new process.
- System Call:
fork()(in UNIX/Linux).fork()creates an exact duplicate of the parent.- It returns 0 to the child and the Child's PID to the parent.
- Execution Options:
- Parent and child execute concurrently.
- Parent waits until child terminates.
2. Process Termination
- A process finishes execution and asks the OS to delete it (
exit()system call). - Resources are deallocated.
- Cascading Termination: In some systems, if a parent terminates, all its children must also be terminated.
- Zombie Process: A process that has terminated, but whose parent has not yet called
wait()(the entry is still in the process table). - Orphan Process: A process whose parent has terminated without waiting (adopted by the
initprocess).
8. Cooperating vs. Independent Processes
Independent Processes
- A process is independent if it cannot affect or be affected by the other processes executing in the system.
- It does not share data with any other process.
Cooperating Processes
- A process is cooperating if it can affect or be affected by other processes.
- They share data (variables, memory, files).
- Reasons for Cooperation:
- Information Sharing: Several users may be interested in the same file.
- Computation Speedup: Breaking a task into subtasks to run in parallel.
- Modularity: Constructing a system in modular fashion.
- Convenience: A user may perform many tasks at one time.
Inter-Process Communication (IPC)
Cooperating processes require an IPC mechanism. Two main models exist:
-
Shared Memory:
- A region of memory is established to be shared by cooperating processes.
- Processes can read and write to this region.
- Pros: Very fast (memory speed).
- Cons: Complicated to implement synchronization (avoiding race conditions).
-
Message Passing:
- Communication takes place by means of exchanging messages via the kernel.
- Operations:
send(message)andreceive(message). - Pros: Easier to implement for distributed systems; no conflict handling needed for memory.
- Cons: Slower than shared memory (requires system calls/kernel intervention).