Concurrency and Synchronization- Basic principles

 

Basic Principles of Concurrency and Synchronization

Modern operating systems are designed to run many programs at the same time. Whether it’s a web browser playing music while downloading files, or a server handling thousands of users, this ability depends on two fundamental concepts: concurrency and synchronization.

This post explains these ideas from the ground up.


What Is Concurrency?

Concurrency means that multiple tasks make progress during the same time period.

  • On a single-core system, the CPU rapidly switches between tasks

  • On a multi-core system, tasks may truly run in parallel

From the user’s point of view, everything appears to run at once.

Why Concurrency Matters

  • Better utilization of the CPU

  • Faster system response

  • Efficient use of shared resources

  • Support for multitasking and multithreaded applications


Processes, Threads, and Shared Resources

Concurrency usually involves:

  • Processes (independent programs)

  • Threads (multiple execution paths within a process)

These execution units often share resources, such as:

  • Variables in memory

  • Files

  • Devices (printer, disk, network)

Sharing resources improves efficiency—but also introduces problems.


The Core Problem: Race Conditions

A race condition occurs when:

  • Two or more concurrent threads access shared data

  • The final result depends on the order of execution

Since scheduling is unpredictable, the program may produce different results each time it runs.

👉 This is why concurrency without control is dangerous.


Critical Sections

A critical section is a part of a program where:

  • Shared data is accessed or modified

Only one thread or process should execute a critical section at a time.

Each process typically has:

  • An entry section (request access)

  • A critical section (use shared resource)

  • An exit section (release access)

  • A remainder section (other work)




Requirements for Safe Concurrency

Any correct solution to concurrency problems must satisfy three principles:

1. Mutual Exclusion

Only one process or thread can be inside the critical section at a time.

2. Progress

If no one is in the critical section, a process that wants to enter should not be delayed unnecessarily.

3. Bounded Waiting

Every process should get a chance to enter the critical section within a reasonable time.


What Is Synchronization?

Synchronization is the technique used to control the execution order of concurrent tasks so that shared data remains consistent.

In simple terms:

Synchronization prevents chaos when multiple threads work together.


How Synchronization Works (Conceptually)

Synchronization ensures that:

  • Access to shared resources is ordered

  • Conflicting operations do not overlap

  • Data remains correct and predictable

This is usually achieved using:

  • Locks

  • Semaphores

  • Atomic operations (handled by the OS and hardware)

The details vary, but the goal is always the same: safe cooperation.


Busy Waiting vs Blocking

Two common ways threads wait for access:

  • Busy waiting: a thread keeps checking until it gets access (wastes CPU)

  • Blocking: a thread sleeps until it is allowed to continue (efficient)

Good systems prefer blocking whenever possible.


Why Synchronization Is Essential

Without synchronization:

  • Programs behave unpredictably

  • Data corruption occurs

  • Bugs become hard to detect and reproduce

With proper synchronization:

  • Programs are reliable

  • Shared resources are protected

  • Concurrency becomes a powerful advantage instead of a risk


Final Thoughts

Concurrency allows operating systems to do more, faster.
Synchronization ensures they do it correctly.

Understanding these basic principles is essential before studying:

  • Mutexes and semaphores

  • Deadlocks

  • Scheduling

  • Multithreaded programming

Master these foundations, and the rest of operating systems will make much more sense.

Comments

Popular posts from this blog

Operating Systems OS PCCST403 Semester 4 BTech KTU CS 2024 Scheme

Introduction to Operating System -Virtualization, Concurrency, and Persistence

Differences Between Linux and Classic UNIX Kernels