Inter-process communication in Linux
Design 1: Shared File
• There is a shared file called design1.dat. All communications are carried out on this file.
• Clients can write to the file, server can read and write to the file.
• We can create as many clients as we want. There is only one server.
• Chat outputs are visible only on the server’s terminal. Client terminals are used for only inputs.
• The file is locked and unlocked before and after read/write actions at all times.
To run the Design 1:
- Run the client:
gcc design_1/client.c
./a.out
- Enter client name
- Enter the message to be written on shared file
- Run the server to see messages:
gcc design_1/server.c
./a.out
Design 2: Shared Memory
• There is a shared memory. All communications are carried out on this memory.
• Clients can write to the memory, server can read and write to the memory.
• We can create as many clients as we want. There is only one server.
• Chat outputs are visible only on the server’s terminal. Client terminals are used for only inputs.
• The memory is locked and unlocked using semaphores before and after read/write actions at all times.
• There are two semaphores: one for reader(server) one for writing permissions.
• Server opens the shared memory and waits for a client to write something into the memory (waits on a semaphore (read_semaphore which is initially 0))
To run the Design 2:
- Run the client:
gcc design_2/client.c -o client -lrt -pthread
./client
- Enter client name
- Enter the message to be written on shared memory
- Run the server to see messages:
gcc design_2/server.c -o server -lrt -pthread
./server
Design 3: Unnamed Pipes
• There is an unnamed pipeline. All communications are carried out on this pipeline.
• Clients can write to the pipeline, server can read and write to the pipeline.
• Chat outputs are visible only on the server’s terminal. Client terminals are used for only inputs.
• The pipeline is locked and unlocked using semaphores before and after read/write actions at all times.
• There are two semaphores: one for reader(server) one for writing permissions.
To run the Design 3:
- Run the client:
gcc design_3/client.c
./a.out
- Enter client name
- Enter the message to be written on shared pipeline
- Run the server to see messages:
gcc design_3/server.c
./a.out
Design 4: Named Pipes
• This is similar to unnamed pipes question. However, this time there is no parent-child relation.
• We can create as many clients as we want.
Design 5: Message Queues
• This is similar to the named pipes.
• There is a single semaphore, only for the writers.
Design 6: Sockets
• There is a single socket.
• There is a single semaphore.
Design 7: Sockets + Signals
• This is same as the previous design.
• Server is able to kill the clients by receiving a message “killme”. Server sends a kill signal to the client and the client will be terminated.