Winter Semester 2026–27 · VIT-AP · BACSE106 · Operating Systems · Dr. Krithika L.B

Lab 9: Producer–consumer and readers–writers using semaphores

Theory link: 3.4 Semaphores · 3.5 Classical problems

Experiment 9 of 15 Module 3 2 hours

Video walkthrough

Lab 9 walkthrough — Producer–consumer and readers–writers using semaphores

Video coming soon

Lab 9 walkthrough — Producer–consumer and readers–writers using semaphores

Recorded by Dr. Krithika L.B. Slot lab09-walkthrough

Aim

Aim. To implement the bounded-buffer producer–consumer problem and the first readers–writers problem using POSIX threads and semaphores.

Explanation

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.

Producer–consumer

empty = 5, full = 0, mutex = 1 — exactly the Session 3.5 solution.

Readers–writers

rw_mutex = 1, mutex = 1, read_count = 0 — readers share, the writer is exclusive.

Environment

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.

Code

Program 1 · Producer–consumer

prodcons.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;
}

Program 2 · Readers–writers

readwrite.c
#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;
}

How to run

  1. gcc prodcons.c -o prodcons -pthread && ./prodcons
  2. gcc readwrite.c -o readwrite -pthread && ./readwrite
  3. Run each several times: the interleaving changes, but the producer never overwrites an unconsumed slot and a writer is never inside with a reader.

Sample input & output

prodcons (one possible run)

Input
(none)
Output
Output
Produced 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 2

readwrite (one possible run)

Input
(none)
Output
Output
Writer 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 = 20

Try it yourself

  1. Use 2 producers and 2 consumers. Does the solution still work?
  2. Swap the order of sem_wait(&empty) and sem_wait(&mutex) in the producer and run with a small buffer — explain the hang.
  3. Remove the semaphores from prodcons.c and observe corrupted results.
  4. Modify readers–writers so that writers have priority (second readers–writers problem).

Viva questions

Prepare these for the lab evaluation.

Revealed 0
Initial values of empty, full and mutex?

empty = buffer size, full = 0, mutex = 1.

What do sem_wait and sem_post correspond to?

wait/P and signal/V.

Why can several readers be inside together?

Only the first reader acquires rw_mutex and only the last releases it; readers in between pass straight through.

Can writers starve in your solution?

Yes — the first readers–writers solution favours readers.