0
0
Operating-systemsConceptBeginner · 3 min read

What is Mutex in OS: Definition, Usage, and Example

A mutex in an operating system is a tool that allows only one thread or process to access a shared resource at a time. It helps prevent conflicts and errors by making sure that multiple threads do not use the same resource simultaneously.
⚙️

How It Works

A mutex works like a lock on a door. Imagine a room where only one person can enter at a time to use a computer. When someone enters, they lock the door behind them so no one else can come in until they leave and unlock it. This prevents two people from trying to use the computer at the same time and causing problems.

In an operating system, when a thread wants to use a shared resource like a file or memory, it must first "lock" the mutex. If the mutex is already locked by another thread, the new thread waits until the mutex is unlocked. Once the first thread finishes, it unlocks the mutex, allowing the next waiting thread to enter. This ensures safe and orderly access to resources.

💻

Example

This example shows two threads trying to increase the same number safely using a mutex to avoid conflicts.

c
#include <stdio.h>
#include <pthread.h>

int counter = 0;
pthread_mutex_t lock;

void* increment(void* arg) {
    for (int i = 0; i < 100000; i++) {
        pthread_mutex_lock(&lock);  // Lock the mutex
        counter++;                 // Critical section
        pthread_mutex_unlock(&lock); // Unlock the mutex
    }
    return NULL;
}

int main() {
    pthread_t t1, t2;
    pthread_mutex_init(&lock, NULL);

    pthread_create(&t1, NULL, increment, NULL);
    pthread_create(&t2, NULL, increment, NULL);

    pthread_join(t1, NULL);
    pthread_join(t2, NULL);

    printf("Final counter value: %d\n", counter);

    pthread_mutex_destroy(&lock);
    return 0;
}
Output
Final counter value: 200000
🎯

When to Use

Use a mutex whenever multiple threads or processes need to access the same resource, like a file, database, or shared memory, to avoid conflicts and data corruption. For example, in a banking app, if two transactions try to update the same account balance at the same time, a mutex ensures one finishes before the other starts.

Mutexes are essential in multithreaded programs to keep data consistent and prevent crashes caused by simultaneous access.

Key Points

  • A mutex allows only one thread to access a resource at a time.
  • It prevents race conditions and data corruption.
  • Threads must lock the mutex before accessing shared data and unlock it afterward.
  • Mutexes are widely used in multithreaded programming for safe resource sharing.

Key Takeaways

A mutex ensures only one thread accesses a shared resource at a time to prevent conflicts.
Always lock a mutex before accessing shared data and unlock it after to keep data safe.
Mutexes are crucial in multithreaded programs to avoid race conditions and crashes.
Use mutexes when multiple threads or processes share resources like files or memory.