You are on page 1of 6

Message Queue Implementation

with semaphore and shared


memory
Semaphore and shared memory
example
Here semaphores and shared memory are used to implement the message queue
instead of using messages.
Shared memory is created within the kernel region which is used for establishing two
way communication between server and client processes.
Semaphore control which process can access the shared memory.
Semaphore and shared memory
example
Server initially creates the semaphore set consisting of two semaphores and a shared
memory, and it initializes the semaphore set value to be 0, 1
Server performs semop(-1, 0), which is not possible so server enters into waiting state
ie., waiting for a client to send a request to the shared memory.
Client willing to send a message into a shared memory performs semop(0,-1) and the
semaphore values in the set are changed to 0,0. which means client is now able to
access shared memory for writing a message.
Client process sends a service request command and its process id to the shared
memory. Once its done server needs to be unblocked so client will perform semop(1,0).
this will unblock the server process. And client needs to enter into blocked state
waiting for its reply from server process so client also perform semop(-1,-1).
Server unblocked by changing the semaphore set values as 0,0 so requesting client
which is performing semop(-1,-1) will enter into blocked state. server writes the
response into the shared memory then performs the semop(1,1) to unblock the client
process
Semaphore and shared memory
example
Server perform semop(-1,0) with which it will enter into waiting state.
Client that is unblocked by the server sets the semaphore set value to 0,0 and reads
the servers response data and sets the semaphore values to (0,1) before it terminates
itself.
Server is created and converted into deamon process and it creates shared memory and semaphore
set and initializes the semaphore values as 0,1.

0 1

Deamon process should wait for a request message from a client process so perform semop(-1,0)
which is not possible so server will enter into waiting state.
Client willing to send a message will perform an operation semop(0,-1) so semaphore values are
changed to 0,0.

0 0

Client got access to shared memory and write a message into it and next major activity is to unblock
server process so client changes the semaphore values accordingly semop(1,0) and client also
specifies that there is a request
1 0

message in the shared memory by updating the first semaphore value as 1. and client needs to enter
into waiting state which is accomplished by an semop(-1,0) so perform semop(-1,-1)
Server will get unblocked with this values and semaphore values will be changed to
0,0.
0 0

Server will read the request and generate a response and will send the response into
the shared memory and perform an operation semop(1,1) which is responsible for
doing two things it unblocks the client and it also responsible for releasing the shared
1 1
memory completely from server.

Server needs to enter into waiting state so it performs the operation semop(-1,0)
Now client is unblocked by changing the semaphore values as 0,0
0 0

Client will read the response and reset the semaphore values as 0,1 by performing an
operation semop(0,1)

You might also like