Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

how semaphore mutual exclusion works, Study notes of Operating Systems

how semaphore mutual exclusion works and code in c as example

Typology: Study notes

2023/2024

Available from 03/13/2024

sreebhadra-u-s
sreebhadra-u-s 🇮🇳

2 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Semaphore is a synchronization primitive used in concurrent programming to
control access to a shared resource. Mutual exclusion using semaphores
ensures that only one process or thread can access the shared resource at a
time. Here’s how semaphore mutual exclusion works:
Explanation:
1. **Semaphore Initialization**: Initialize a semaphore with an initial value
of 1. This semaphore acts as a lock for the shared resource.
2. **Acquiring the Semaphore**: When a process wants to access the
shared resource, it must acquire the semaphore. If the semaphore’s
value is greater than 0 (i.e., the resource is available), the process
decrements the semaphore value and proceeds. If the semaphore’s
value is 0 (i.e., the resource is currently being used), the process is
blocked until the semaphore’s value becomes greater than 0.
3. **Releasing the Semaphore**: After a process finishes using the shared
resource, it releases the semaphore by incrementing its value. This
allows other processes waiting for the resource to acquire it.
### Example Code (Using Pthreads and Semaphores in C):
```c
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#define NUM_THREADS 5
pf3

Partial preview of the text

Download how semaphore mutual exclusion works and more Study notes Operating Systems in PDF only on Docsity!

Semaphore is a synchronization primitive used in concurrent programming to control access to a shared resource. Mutual exclusion using semaphores ensures that only one process or thread can access the shared resource at a time. Here’s how semaphore mutual exclusion works: Explanation:

  1. Semaphore Initialization: Initialize a semaphore with an initial value of 1. This semaphore acts as a lock for the shared resource.
  2. Acquiring the Semaphore: When a process wants to access the shared resource, it must acquire the semaphore. If the semaphore’s value is greater than 0 (i.e., the resource is available), the process decrements the semaphore value and proceeds. If the semaphore’s value is 0 (i.e., the resource is currently being used), the process is blocked until the semaphore’s value becomes greater than 0.
  3. Releasing the Semaphore: After a process finishes using the shared resource, it releases the semaphore by incrementing its value. This allows other processes waiting for the resource to acquire it.

Example Code (Using Pthreads and Semaphores in C):

#include <stdio.h> #include <pthread.h> #include <semaphore.h> #define NUM_THREADS 5 Sem_t mutex; // Semaphore for mutual exclusion Void *thread_function(void *thread_id) { Int tid = *((int *)thread_id); Printf(“Thread %d is attempting to acquire the semaphore.\n”, tid); Sem_wait(&mutex); // Acquire the semaphore Printf(“Thread %d has acquired the semaphore and is now accessing the shared resource.\n”, tid); // Access the shared resource (critical section) Printf(“Thread %d is releasing the semaphore.\n”, tid); Sem_post(&mutex); // Release the semaphore Pthread_exit(NULL); } Int main() { Pthread_t threads[NUM_THREADS]; Int thread_ids[NUM_THREADS]; Sem_init(&mutex, 0, 1); // Initialize the semaphore with value 1 For (int I = 0; I < NUM_THREADS; ++i) { Thread_ids[i] = I; Pthread_create(&threads[i], NULL, thread_function, (void *)&thread_ids[i]); } For (int I = 0; I < NUM_THREADS; ++i) {