Lab 9 walkthrough — Producer–consumer and readers–writers using semaphores
Lab 9 walkthrough — Producer–consumer and readers–writers using semaphores
Recorded by Dr. Krithika L.B. Slot lab09-walkthrough
Theory link: 3.4 Semaphores · 3.5 Classical problems
Lab 9 walkthrough — Producer–consumer and readers–writers using semaphores
Recorded by Dr. Krithika L.B. Slot lab09-walkthrough
Aim. To implement the bounded-buffer producer–consumer problem and the first readers–writers problem using POSIX threads and semaphores.
Threads stand in for processes (they share the buffer automatically). POSIX semaphores: sem_init(&s, 0, value),
sem_wait(&s) = wait/P, sem_post(&s) = signal/V.
empty = 5, full = 0, mutex = 1 — exactly the Session 3.5 solution.
rw_mutex = 1, mutex = 1, read_count = 0 — readers share, the writer is exclusive.
These programs use POSIX system calls (fork, pthread, shm, semaphore) and must be compiled and run on Linux or WSL — not with Windows MinGW/Turbo C.
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#define SIZE 5
int buffer[SIZE], in = 0, out = 0;
sem_t empty, full, mutex;
void *producer(void *arg)
{
for (int item = 1; item <= 8; item++) {
sem_wait(&empty); /* wait for a free slot */
sem_wait(&mutex);
buffer[in] = item;
printf("Produced %d at slot %d\n", item, in);
in = (in + 1) % SIZE;
sem_post(&mutex);
sem_post(&full); /* one more full slot */
}
return NULL;
}
void *consumer(void *arg)
{
for (int i = 1; i <= 8; i++) {
sem_wait(&full); /* wait for an item */
sem_wait(&mutex);
int item = buffer[out];
printf(" Consumed %d from slot %d\n", item, out);
out = (out + 1) % SIZE;
sem_post(&mutex);
sem_post(&empty); /* one more free slot */
}
return NULL;
}
int main(void)
{
pthread_t p, c;
sem_init(&empty, 0, SIZE);
sem_init(&full, 0, 0);
sem_init(&mutex, 0, 1);
pthread_create(&p, NULL, producer, NULL);
pthread_create(&c, NULL, consumer, NULL);
pthread_join(p, NULL);
pthread_join(c, NULL);
return 0;
}#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
sem_t rw_mutex, mutex;
int read_count = 0, data = 0;
void *writer(void *arg)
{
int id = *(int *)arg;
sem_wait(&rw_mutex); /* exclusive access */
data += 10;
printf("Writer %d wrote data = %d\n", id, data);
sleep(1);
sem_post(&rw_mutex);
return NULL;
}
void *reader(void *arg)
{
int id = *(int *)arg;
sem_wait(&mutex);
read_count++;
if (read_count == 1) sem_wait(&rw_mutex); /* first reader locks writers out */
sem_post(&mutex);
printf("Reader %d read data = %d (readers inside: %d)\n", id, data, read_count);
sleep(1);
sem_wait(&mutex);
read_count--;
if (read_count == 0) sem_post(&rw_mutex); /* last reader lets writers in */
sem_post(&mutex);
return NULL;
}
int main(void)
{
pthread_t r[3], w[2];
int id[3] = {1, 2, 3};
sem_init(&rw_mutex, 0, 1);
sem_init(&mutex, 0, 1);
pthread_create(&w[0], NULL, writer, &id[0]);
for (int i = 0; i < 3; i++) pthread_create(&r[i], NULL, reader, &id[i]);
pthread_create(&w[1], NULL, writer, &id[1]);
pthread_join(w[0], NULL);
for (int i = 0; i < 3; i++) pthread_join(r[i], NULL);
pthread_join(w[1], NULL);
return 0;
}gcc prodcons.c -o prodcons -pthread && ./prodconsgcc readwrite.c -o readwrite -pthread && ./readwriteProduced 1 at slot 0
Produced 2 at slot 1
Produced 3 at slot 2
Produced 4 at slot 3
Produced 5 at slot 4
Consumed 1 from slot 0
Consumed 2 from slot 1
Produced 6 at slot 0
Produced 7 at slot 1
Consumed 3 from slot 2
Consumed 4 from slot 3
Consumed 5 from slot 4
Produced 8 at slot 2
Consumed 6 from slot 0
Consumed 7 from slot 1
Consumed 8 from slot 2Writer 1 wrote data = 10
Reader 1 read data = 10 (readers inside: 1)
Reader 2 read data = 10 (readers inside: 2)
Reader 3 read data = 10 (readers inside: 3)
Writer 2 wrote data = 20Prepare these for the lab evaluation.
Revealed 0empty = buffer size, full = 0, mutex = 1.
wait/P and signal/V.
Only the first reader acquires rw_mutex and only the last releases it; readers in between pass straight through.
Yes — the first readers–writers solution favours readers.