Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between mutual exclusion and synchronization?

Tags:

What is the difference between above two?

This question came to my mind because I found that

  1. Monitors and locks provide mutual exclusion

  2. Semaphores and conditional variables provide synchronization

Is this true?

Also while searching I found this article

Any clarifications please.

like image 769
Tanya Avatar asked Apr 11 '12 06:04

Tanya


People also ask

What is meant by mutual exclusion?

A mutual exclusion (mutex) is a program object that prevents simultaneous access to a shared resource. This concept is used in concurrent programming with a critical section, a piece of code in which processes or threads access a shared resource.

What do you mean by process Synchronisation?

Processes Synchronization or Synchronization is the way by which processes that share the same memory space are managed in an operating system. It helps maintain the consistency of data by using variables or hardware so that only one process can make changes to the shared memory at a time.

What is condition synchronization?

Condition Synchronization (or merely synchronization) is any mechanism that protects areas of memory from being modified by two different threads at the same time.

What is the difference between semaphore and mutex?

A Mutex is different than a semaphore as it is a locking mechanism while a semaphore is a signalling mechanism. A binary semaphore can be used as a Mutex but a Mutex can never be used as a semaphore.


1 Answers

Mutual exclusion means that only a single thread should be able to access the shared resource at any given point of time. This avoids the race conditions between threads acquireing the resource. Monitors and Locks provide the functionality to do so.

Synchronization means that you synchronize/order the access of multiple threads to the shared resource.
Consider the example:
If you have two threads, Thread 1 & Thread 2.
Thread 1 and Thread 2 execute in parallel but before Thread 1 can execute say a statement A in its sequence it is a must that Thread 2 should execute a statement B in its sequence. What you need here is synchronization. A semaphore provides that. You put a semapohore wait before the statement A in Thread 1 and you post to the semaphore after statement B in Thread 2.
This ensures the synchronization you need.

like image 166
Alok Save Avatar answered Sep 22 '22 18:09

Alok Save