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.
- 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.
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→ Empty1→ Ready for worker2→ Being processed3→ Result ready
- Client sends a message to the server over TCP.
- Master process:
- Finds a free buffer slot.
- Places the request and marks it as ready.
- Worker process:
- Picks up a ready slot, locks it, and processes the message (converts text to uppercase).
- Marks the slot as result ready.
- Master process:
- Reads processed results from result ready slots.
- Sends them back to the client over TCP.
Server:
gcc server.c -o server -pthread
./serverClient:
gcc client.c -o client -pthread
./client