DEV Community

Dipsan Kadariya
Dipsan Kadariya

Posted on • Edited on

Master OS Concepts 💡 | Understanding Threads, Processes & IPC 🔧

Operating Systems: Concepts and Structures

Introduction

Operating systems are the backbone of modern computing, serving as the crucial interface between hardware and software. Whether you're a seasoned developer or just starting your journey in computer science, understanding the fundamentals of operating systems is essential. In this comprehensive guide, we'll explore the core concepts, structures, and mechanisms that make operating systems tick.

The Dual Role of Operating Systems

Operating system as a extended Machine acts as a intermediate between the user and Hardware. it helps in data abstraction by hiding the unnecessary details and information and provides a user friendly interface for the user to interact with the computer system.

Operating system as a resource manager manages CPU memory and input output devices. it helps efficient resources allocation by ensuring that the system resources are used optimally preventing wastage of resources and maximizing the performance.

Types of OS

  1. Batch processing operating system is operating system that executes jobs in batches without the user interaction. The jobs are process sequentially one after another.

  2. Multiprogramming operating system is operating system that allows multiple programs to run simultaneously. The CPU switches between the programs to maximize utilization

  3. Multi tasking operating system is operating system that runs multiple task t co -currently It works by rapidly switching between the task which makes it seem like they are running simultaneously.

  4. Multiprocessor operating system is on operating system that supports multiple processor within a single system. It Distributes tasks across multiple CPUs for better performance.

  5. Network operating system is a operating system that managers network resources and communication between computers. It enables file sharing and data access over a network.

  6. Distributed operating system is a operating system that manages a group of independent computers making them appear as a single system. It facilitates resource sharing and task distribution among computers.

  7. Real time operating system is a operating system that Provides precise and predictable task execution within strict time constraints. This OS prioritizes tasks based on importance, ensuring timely completion.

OS structure

OS structure includes components that make up an OS.

  • includes kernel, user interface, device drivers
  • kernel: manages core functions like memory, CPU and peripheral devices

Monolithic Structure

In a monolithic structure, the operating system is built as a single, large kernel where all the core functions are tightly integrated. This means that everything from file management to memory management and device drivers operates within a unified space. The advantage of this structure is its high performance since components can communicate directly with each other without intermediaries. However, because all functionalities are part of the same large kernel, debugging and maintaining the system can be more challenging. Unix and early versions of Linux are examples of monolithic operating systems.

Layered Structure

The layered structure organizes the operating system into a hierarchy of layers, where each layer is built on top of the previous one. This design allows for a clear separation of concerns, as each layer has its own specific responsibilities. For instance, lower layers might handle hardware interactions, while higher layers manage user interfaces. A key benefit of this structure is modularity, which makes the OS easier to develop, manage, and debug. Each layer only interacts with the one directly below it, reducing the complexity of the system as a whole. An example of a layered OS is the Windows NT architecture.

Microkernel Structure

The microkernel structure focuses on minimizing the functions that run in the kernel. Only essential services, such as basic inter-process communication (IPC) and simple I/O operations, are included in the kernel. Other services, such as file systems, network protocols, and device drivers, operate in user space. This modular approach enhances the security and stability of the system, as fewer components running in kernel mode means fewer chances for critical failures. Additionally, microkernels are more extensible and easier to update or modify. Minix and QNX are examples of microkernel-based operating systems.

Client-Server Model

In the client-server model, the operating system's functions are divided between clients (user applications) and servers (OS services). The clients make requests to the servers, which provide the necessary services through inter-process communication (IPC). This model is highly suited for distributed systems, where services may run on different machines across a network. It allows for a clear separation between user-level processes and system-level services, making the OS more modular and easier to scale. An example of an OS that uses the client-server model is the Mach kernel, which influenced the development of macOS.

Virtual Machine Structure

The virtual machine structure of an operating system involves creating a virtualized environment that emulates hardware, allowing multiple OS instances to run on a single physical machine. Each virtual machine operates as if it were running on its own independent hardware, with the OS managing resources and ensuring isolation between different virtual machines. This structure is particularly useful in cloud computing and environments where resource isolation and efficient use of hardware are critical. Virtualization platforms like VMware, Microsoft Hyper-V, and Xen use this structure to allow multiple operating systems to coexist on the same hardware.

Open-Source Operating Systems

An open-source operating system is a type of OS whose source code is made publicly available, allowing anyone to view, modify, and distribute it. This openness encourages community collaboration, innovation, and transparency in the development process.

System Calls

A system call is a mechanism that allows a program to request a service from the operating system's kernel. It provides an interface for user applications to perform operations that require privileged access to system resources, such as hardware devices, file systems, and inter-process communication.

example of system call is
key Feature: interface between user and kernel,access to system resources.

Process Model

Process Model refers to the conceptual structure that describes how processes are represented and managed within an operating system. It defines how the system views a process and organizes its operations.

Key Concepts in the Process Model

Single-Threaded Process

A process that contains only one thread of execution, meaning it can only execute one sequence of instructions at a time.
Characteristics: Simple to manage, but less efficient in utilizing system resources, as it cannot perform multiple tasks simultaneously within the same process.

Multi-Threaded Process

A process that contains multiple threads, each of which can execute independently but share the same resources like memory and file handles.
Characteristics: More efficient and responsive, as it allows concurrent execution of multiple tasks within the same process, making better use of system resources.

Benefits of the Process Model

  • Resource sharing
  • Modularity

Process States

  • New: The process is being created.
  • Ready: The process is waiting to be assigned to the CPU.
  • Running: Instructions are being executed.
  • Waiting: The process is waiting for some event (e.g., I/O completion
  • Terminated: The process has finished execution.

Process Control Block (PCB)

A data structure used by the OS to store all the information about a process.

Components of PCB

1) Process State: Current state of the process (new, ready, running, etc.).
2) Program Counter: Address of the next instruction to be executed.
3) CPU Registers: Information about the CPU's registers when the process is not running.
4) Memory Management Information: Details on memory allocation for the process.
5) I/O Status Information: Details about the process's I/O devices and files.
more are identifers etc

Threads

The smallest unit of processing that can be scheduled by an operating system.

Types of Threads

User Threads

Definition: Managed by user-level libraries rather than directly by the OS.Advantages: Faster context switching.
If one thread makes a blocking system call, the entire process is blocked.

Kernel Threads

Managed directly by the operating system, which schedules and handles thread execution.
multiple threads can run simultaneously on multiple processors.
Slower context switching compared to user threads.

Inter Process Communication (IPC)

A mechanism that allows processes to communicate and synchronize their actions when they are executing concurrently.
Essential for resource sharing, data exchange, and process synchronization in multiprogramming environments.

Race Condition

Occurs when multiple processes or threads read and write shared data, and the final outcome depends on the sequence of execution, leading to unpredictable results.
ex..Two threads incrementing the same variable simultaneously.

Critical Section

A part of the code where shared resources are accessed. Proper synchronization is required to prevent race conditions.
soln:Mechanisms like locks, semaphores, or monitors are used to ensure only one thread enters the critical section at a time.

IPC PROBLEMS

1. Producer-Consumer Problem

Definition:
The Producer-Consumer Problem is a classic synchronization problem where producers generate data and put it into a buffer, while consumers take data out of the buffer. The challenge is to coordinate between them to avoid overflows and underflows.

2. Sleeping Barber Problem

Definition:
The Sleeping Barber Problem involves a barber shop with one barber and several chairs. If no customers are present, the barber sleeps. If customers arrive and all chairs are occupied, they must wait. If chairs are available, they are served immediately.

3. Dining Philosophers Problem

Definition:
The Dining Philosophers Problem involves five philosophers sitting at a round table with a fork between each pair. They need both forks to eat. The challenge is to avoid deadlock and ensure that each philosopher gets a chance to eat.

Implementing Mutual Exclusion

Techniques for Mutual Exclusion

Busy Waiting

Definition: A technique where a process repeatedly checks a condition to achieve synchronization, consuming CPU time while waiting.
Drawback: Wasteful use of CPU resources.

Disabling Interrupts

Definition: Prevents the OS from switching processes by disabling hardware interrupts.
Drawback: Not practical for multi-user systems because it gives the running process control over the CPU for too long.

Lock Variables

Definition: A simple flag that processes can use to signal whether a resource is in use. However, this can lead to busy waiting.

Strict Alternation

Definition: A simple solution for two processes, where they take turns accessing a shared resource.
Drawback: Not efficient for more than two processes, and can cause delays.

Peterson's Solution

Definition: A classic software-based solution for two processes that ensures mutual exclusion without busy waiting.
Key Points: Relies on two variables, flag and turn, to manage access to the critical section.

Test and Set Lock

Definition: A hardware instruction that atomically tests and sets a lock variable, avoiding race conditions.

Sleep and Wakeup

Definition: A synchronization mechanism where processes go to sleep (stop execution) when they can't proceed and are woken up by other processes when they can continue.

Semaphore

Definition: A synchronization tool that uses counters to control access to shared resources.
Types:
Binary Semaphore: Can be either 0 or 1.
Counting Semaphore: Can take any integer value.
Operations: wait() and signal() are the primary operations.

Monitors

Definition: High-level synchronization constructs that allow threads to have both mutual exclusion and the ability to wait for a certain condition to become true.
Components: Include condition variables for wait() and signal() operations.

Message Passing

Definition: A method of IPC where processes communicate by sending and receiving messages.
Use Case: Useful in distributed systems where processes do not share memory.

Batch System Scheduling

First-Come First-Served (FCFS)

Definition: The first process to arrive is the first to be executed.
Issue: Can cause the "convoy effect," where short processes get stuck behind long ones.

Shortest Job First (SJF)

Definition: The process with the shortest burst time is executed next.
Issue: Can cause starvation of long processes.
Optimality: Minimizes average waiting time in the system.

Shortest Remaining Time Next (SRTN)

Definition: A preemptive version of SJF where the process with the shortest remaining time is executed next.
Advantage: Better response time for shorter processes.

Interactive System Scheduling

Round-Robin Scheduling

Definition: Each process is assigned a fixed time slice, and if it doesn't finish in that time, it is placed back in the queue.
Advantage: Fair and responsive.
Time Quantum: The choice of time quantum is crucial for balancing responsiveness and overhead.

Priority Scheduling

Definition: Processes are assigned priorities, and the process with the highest priority is executed first.
Issue: Can lead to starvation if low-priority processes are never executed.
Solution: Aging can be used to gradually increase the priority of a process waiting in the queue.

Multiple Queues

Definition: Processes are divided into several queues based on priority or other criteria, with each queue having its own scheduling algorithm.
Example: A system might have one queue for foreground (interactive) processes and another for background (batch) processes.

Real-Time System Scheduling

Definition: Ensures that critical processes are completed within a specific time frame.
Use Case: Used in systems where timing is crucial (e.g., embedded systems in medical devices).

Top comments (0)