0% found this document useful (0 votes)
4 views20 pages

Interprocess Communication and Synchronization

The document discusses interprocess communication (IPC) and the importance of synchronization in managing interactions between processes and threads to prevent concurrency-related issues. It explains critical sections, mutex locks, and semaphores as solutions for ensuring mutual exclusion and coordinating access to shared resources. Additionally, it outlines the differences between synchronization and scheduling, and provides implementation details for mutex locks and semaphores.

Uploaded by

Mariem Selmi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views20 pages

Interprocess Communication and Synchronization

The document discusses interprocess communication (IPC) and the importance of synchronization in managing interactions between processes and threads to prevent concurrency-related issues. It explains critical sections, mutex locks, and semaphores as solutions for ensuring mutual exclusion and coordinating access to shared resources. Additionally, it outlines the differences between synchronization and scheduling, and provides implementation details for mutex locks and semaphores.

Uploaded by

Mariem Selmi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Synchronization

Dr. Ikram Saadaoui

Dr. Ikram Saadaoui Synchronization 1 / 20


Interprocess Communication (IPC)

Processes and threads do not only carry out read and write
operations on data, but also they :
call each other
wait for each other
coordinate with each other
In short : They must interact with each other
Important questions regarding interprocess communication (IPC) :
How can multiple processes (or threads) access shared
resources ?
How can a process (or a thread) transmit information to others ?

Dr. Ikram Saadaoui Synchronization 2 / 20


Forms of interaction
Interprocess interaction has 2 aspects :
Functional aspect : communication and cooperation
Temporal aspect : synchronization

Communication refers to the exchange of data between two or


more processes.
Cooperation refers to multiple processes (threads) working
together to accomplish a common goal or task.
Synchronization refers to the coordination of multiple processes or
threads to ensure their activities occur in a specific order or at
specific times.
Dr. Ikram Saadaoui Synchronization 3 / 20
Synchronization
Communication and cooperation are based on synchronization
Synchronization is the most elementary form of interaction.
It plays a crucial role in managing the interactions between
processes to prevent concurrency-related issues such as race
conditions, data inconsistency...etc.
Therefore, we first discuss the synchronization

Dr. Ikram Saadaoui Synchronization 4 / 20


Shared data

Between threads :
Global variables are shared among threads
Stored in static data segment, accessible by any thread
Dynamic objects are shared among threads
Stored in the heap, shared through the pointers
Local variables are not shared among threads
Refer to data on the stack. Each thread has its own stack
Between Processes :
Memory : a region of memory that is accessible by multiple
processes.
Files : Processes can share files on the file system.

Dr. Ikram Saadaoui Synchronization 5 / 20


critical section

Processes and threads that are runing in parallel, can consist of :


Uncritical sections : a piece of code that do not access shared
resources or only carry out read operations on shared resources
Critical sections :a piece of code that accesses a shared
resource, usually a variable or data structure.
We need Mutual Exclusion to execute the critical section :
The critical section must be executed atomically
(all-or-nothing)
Only one process (thread) at a time can execute in the critical
section
All other processes (threads) are forced to wait
When a process(thread) leaves a critical section, another can
enter

Dr. Ikram Saadaoui Synchronization 6 / 20


critical section Example

Withdrawing money from a bank account


Suppose your father and your mother share a bank account
with a balance of 10,000 TND
What happens if both go to separate ATM machines and
simultaneously withdraw 5,000 TND from the account ?

Dr. Ikram Saadaoui Synchronization 7 / 20


critical section

The execution of the two threads can be interleaved (interrupted at


anytime partially completing execution).

Concurrency leads to data inconsistency


Two or more concurrent threads accessing a shared resource,
which create a race condition
The output of the program is not consistent ; it varies from run
to run even with same inputs, depending on timing.

Dr. Ikram Saadaoui Synchronization 8 / 20


Need for synchronization

We need synchronization mechanisms for controlling access to


shared resources and decreasing concurrency.
What is the difference between synchronization and scheduling ?
Both synchronization and scheduling involve managing the
execution of multiple processes or threads. However :
Synchronization focuses on coordinating their activities to
maintain data consistency and correct execution.
While scheduling focuses on allocating system resources
efficiently to optimize system performance.
Dr. Ikram Saadaoui Synchronization 9 / 20
Synchronization solutions

Software and hardware solutions are available for handling critical


section problems but hardware solutions are harder to implement.
The two software solutions for process synchronization are

MUTEX LOCK SEMAPHORE

Dr. Ikram Saadaoui Synchronization 10 / 20


Mutex locks

A lock is an object that provides mutual exclusion with the


following two operations :
Acquire : wait until lock is free, then grab it
Release : unlock and wake up any thread waiting to acquire
At most one process (thread) can hold a lock (access the
critical section) at a time

Dr. Ikram Saadaoui Synchronization 11 / 20


Mutex locks

Dr. Ikram Saadaoui Synchronization 12 / 20


Mutex lock Implementation
Create and initialize a mutex based on initial attributes :
int pthread_mutex_init (pthread_mutex_t *mutex, const
pthread_mutexattr_t *attr) ;
If attr is NULL, the default mutex attribute is used.
Acquire the specified mutex lock :
int pthread_mutex_lock (pthread_mutex_t *mutex)
If the mutex is already locked by another thread, the thread
blocks and waits for the mutex to become available.
Release the specified mutex lock :
int pthread_mutex_unlock (pthread_mutex_t *mutex)
Destroy an allocated mutex
int pthread_mutex_destroy (pthread_mutex_t *mutex)
If successful, the above methods return 0, otherwise they return -1.

Dr. Ikram Saadaoui Synchronization 13 / 20


Setup
Open Bash terminal (Git Bash on Windows / Terminal on Mac)
Then open a Ubuntu container using the following command :

# On Windows :
winpty docker run -it --name Synchronization
techtn/ubuntu :p2 bash
# On Mac or Linux :
docker run -it --name Synchronization techtn/ubuntu :p2 bash
If the container exits, use the following commands to re-run it and
retrieve your previous work :
# On Windows :
winpty docker attach Synchronization
# On Mac or Linux :
docker attach Synchronization

Dr. Ikram Saadaoui Synchronization 14 / 20


Counter (without lock)

Dr. Ikram Saadaoui Synchronization 15 / 20


Counter (with lock)

Dr. Ikram Saadaoui Synchronization 16 / 20


Semaphore
A semaphore is an object with an integer value
Manipulated atomically (without interruption) through two
operations :
Wait : decrements the value of the semaphore S and blocks if
S <= 0
Signal (also called post or release) : increments the value of S,
then unblock a single waiter.

In contrast to locks, a semaphore may allow multiple processes to


access the critical section
Dr. Ikram Saadaoui Synchronization 17 / 20
Types of semaphore

Binary semaphore (similar to mutex lock)


Semaphore value is initialized to 1
Guarantees mutually exclusive access to resource
Only one thread is allowed entry at a time
Counting semaphore
Semaphore value is initialized to N
Represents a resource with many units available
Allows threads to enter as long as more units are available
Semaphore is a better option in case there are multiple instances of
resources available. In the case of single shared resource mutex is
a better choice.

Dr. Ikram Saadaoui Synchronization 18 / 20


Posix Semaphore operations

When working with semaphores you need to include <semaphore.h>


Initialize a semaphore :
int sem_init(sem_t *s,int shared,int value)
sem : Specifies the semaphore to be initialized.
pshared : A zero value means semaphore is shared between
threads. A non-zero value means shared between processes.
value : value to assign to the semaphore.
Locking (holding) a semaphore : int sem_wait(sem_t *s)
Unlocking an already locked semaphore : int sem_post(sem_t *s) :
Destroying a semaphore :int sem_destroy(sem_t *s) :

Dr. Ikram Saadaoui Synchronization 19 / 20


Implementing Semaphore

Run the above program. Then change the value of


MAX_Concurrent_Threads to 3 and see what happens.
Dr. Ikram Saadaoui Synchronization 20 / 20

You might also like