Skip to content

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

Master-Worker Processes using Shared Memory.

Problem Statement

Design and implement an application consisting of:

  • One master process (server) and multiple worker processes.
  • All processes share access to a common memory region (request buffer) where:
    • The master process receives client requests (via network), and writes them into the shared buffer.
    • The worker processes read requests from the buffer, process them, and write the responses back into the same buffer.
    • The master process sends responses back to the client.

Constraints & Requirements

  • Every request should be processed by exactly one worker.
  • No request should wait unnecessarily if a worker is free.
  • Workers should share the workload fairly.
  • Communication must be achieved via IPC mechanisms (shared memory + semaphores).
  • Processes are single-threaded (event-driven I/O for master; workers run independently).
  • Requests and responses are assumed to be of the same fixed size.

Solution Overview

This project implements the problem using:

  • System V Shared Memory (shmget, shmat) → Request/response buffer pool.
  • POSIX Semaphores (sem_t) → Synchronization per buffer slot.
  • Forked Worker Processes → Multiple child processes created by the master at startup.
  • TCP Sockets → Client ↔ Master communication over port 5050.
  • State Machine per Buffer Slot to coordinate request lifecycle:
    • 0 → Empty
    • 1 → Ready for worker
    • 2 → Being processed
    • 3 → Result ready

Flow

  1. Client sends a message to the server over TCP.
  2. Master process:
    • Finds a free buffer slot.
    • Places the request and marks it as ready.
  3. Worker process:
    • Picks up a ready slot, locks it, and processes the message (converts text to uppercase).
    • Marks the slot as result ready.
  4. Master process:
    • Reads processed results from result ready slots.
    • Sends them back to the client over TCP.

Execution Steps:

Server:

gcc server.c -o server -pthread
./server

Client:

gcc client.c -o client -pthread
./client

About

Master - Worker Processes with Shared Memory

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages