Inter Process Communication

From: Valentin Welter (valentin.welter@t-online.de)
Date: 04/24/03


From: valentin.welter@t-online.de (Valentin Welter)
Date: 24 Apr 2003 07:40:02 -0700

Inter Process Communication (IPC) mechanisms under Unix

1. MESSAGE QUEUES 1
2. SHARED MEMORY 2
2.1. USING OF SHARED MEMORY AT CLIENT / SERVER NETWORK APPLICATIONS 2
3. SEMAPHORES 3
4. SIMULTANEOUS USE OF ALL IPC RESOURCES 3
5. DEMOS 4

The IPC mechanisms permit different processes to access to the same
system resources and thus data exchange or scheduling of a process by
other processes.
The system resources are accessed by means of system functions, which
are making resources available and enable their use. The system
functions can be called in C/C++ programs.
The following examples describe C/C++ programs.
1. Message queues
Two or several processes can send and receive messages using this
resource.
Restrictions:
While several processes can send messages, only one process can
receive messages.
The process, which receives messages, reads all incoming messages and
waits endless for message messages. It means, normally the reading
process does not finish.

Before messages can be exchanged, a Message Queue must be requested
from the system.

Example
Queue_create: Creates a message queue. The message queue key is given
as program argument.
Queue_sender: Sends messages. The program needs the message queue ID;
this value can be
determined by means of the system call ipcs.
You can start several sender processes or the same sender process
several times.
For checking purpose, the messages could be displayed.
Queue_reader: Receives all sent messages. The program needs the
message queue ID; see above.
The received messages are shown on the screen.
When all messages have been received, the program waits endless for
new messages.
You can avoid this state by means of an end-message.
Queue_finish Sends an end-message to the queue_reader process. The
program needs the message
queue ID; see above.
 
The data, sent by the queue_sender and received from the queue_reader
are displayed and compared.
 
Hint: Because the queue_reader program waits endless for messages, it
must be started on another system session than the other programs.
 
2. Shared memory
Two or several processes can exchange data through this resource.
Before data can be exchanged, a shared memory heap must be made
available by the system.
 
Example
shm_server:
• Creates a shared memory heap.
• Writes data in the heap.
• Displays the data.
• Waits until another process (shm_client, see below) will write data
into this heap.
• Displays the new data from the heap.
 
The shared memory ID is given as program argument.
 
shm_client:
• Takes off the data from the shared memory heap, which has the shared
memory ID.
• Displays these data.
• Drites new data in the heap.
• Displays these data.
 
Now, the shm_server process takes off the new data from the heap and
finishes.
The data, displayed by the shm_server and queue_reader has to be
compared.
 
Hint: the shm_server process must be started either as background
process or from a system session
other than shm_client processes.
2.1. Using of Shared Memory at Client / Server network applications
At Client / Server network applications the clients produce
children-processes of the server.
The server can communicate and exchange data with its children also
only by means of IPC resources.
Using the Shared Memory resource the children-processes can exchange
data as well as follows:
To each child will be assigned an ID, which will be sent each time
together with the actual data.
The server stores the sent data into the Shared Memory heap.
At a child demand, the server sends the stored data.
3. Semaphores
As the name of this resource outpoints, this resource is concerned
with the releasing of a process by other processes.
That means, a process waits for an external signal, namely from other
processes, in order to start or go ahead.
Before semaphores can schedule processes, they must be requested from
the system.
Example
sem_create: creates a semaphore set. The semaphore ID is given as
program argument.
sem_V: increases the semaphore value. The program needs the semaphore
ID.
sem_P: decreases the semaphore value. The program needs the semaphore
ID. When the semaphore value equals zero, the execution of the
following processes will be released. At this time the program
displays a message.

Hint: the sem_P process must be started either as background process
or from a system session other than the shm_V processes.

Since the sem_V processes increase the semaphore value, when they are
executed several times, the sem_P process must be executed the same
number of times in order to decrease the semaphore value to zero and
thus to release the following processes.

The increase of the semaphore value is called V-Operation and
corresponds to a stop (Wait) in the execution of the process.
The decrease of the semaphore value is called P-Operation and
corresponds to revocation of the stop (Wait) demand.
These terms come from the Dutch language, whereby V is a short key for
Wait and P for Signal.
4. Simultaneous use of all IPC resources
Example
IPC_create:
• Creates a shared memory heap. The shared memory ID is given as
program argument.
• Creates a "private" message queues.
• Creates a "private" semaphores set.
• Stores in the shared memory heap the keys and the IDs of the message
queue and semaphores.

IPC_server takes off the keys and the IDs of the message queue and
semaphores from the shared
memory heap, which has the shared memory ID and:
• Sets the semaphore on "release" status
• Receives from the IPC_client integers, which were sent over the
message queue (see below).
• Rates these integers based on a certain rule.
• Writes the rating result in the shared memory heap.
• Displays the received and written data.
• Repeats these operations until the received integer has a certain
value.

IPC_server is a background process.

IPC_client takes off the keys and the IDs of the message queue and
semaphores from the shared
memory heap, which has the shared memory ID and::
• generates random integers.
• Sends these integers to the IPC server over the message queue.
• Sets the semaphore on wait status.
• Reads the data from the memory heap, which has been entered by the
IPC_server.
• Displays the sent and read data.
• Repeats these operations until the generated integer gets a certain
value.
   
In order to have a high probability that the generated integer will
get a certain value and thus the program will last an acceptable time
you can use a "modulo" function:
MOD = random integer - ((random integer/ base integer) * base
integer).
The probability to get a specific value and thus a short program
duration is greater when a small basis integer is used. A suitable
value for the base integer lays between 10 and 20

Hints
• The system call ipcs displays all system resources and their owners.
• By means of the system call ipcrm you can release resources you own.
5. Demos
You find all examples under http://valentin.welter.bei.t-online.de .

IT Dipl.-Ing. Valentin Welter
mail-to: valentin.welter@t-online.de
Internet http://valentin.welter.bei.t-online.de



Relevant Pages

  • Re: semaphore question
    ... What do you mean by "user A uses semaphore S1"? ... resource R in his code. ... ID in the shared memory so that multiple processes can acquire the ... operating system helps by providing the atomic check-and-open capability. ...
    (comp.os.linux.development.apps)
  • Re: ADC Tool Step 3 - Resource Mailbox Wizard
    ... The resource mailboxes are still going to be replicated ... > will still exist on Exchange 5.5 server, ... I think of the AD as Exchange 2K3's ...
    (microsoft.public.exchange.setup)
  • Re: ADC Tool Step 3 - Resource Mailbox Wizard
    ... The resource mailboxes are still going to be replicated ... > will still exist on Exchange 5.5 server, ... I think of the AD as Exchange 2K3's ...
    (microsoft.public.exchange2000.setup.installation)
  • Re: Inter-processor atomic operationss
    ... difference between the implementation of this hypothetical synchronization ... Need to either always use a keyed event for the wait semaphore ... Don't refer to any memory not in the shared memory region. ... Does the Interlocked API set guarantee inter CPU atomic operations ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Non blocking ioctl CDROMEJECT
    ... |> device gets stuck and won't function, ... thread, or task, or any other resource, upon the extinction of the entity ... design of Unix, however, is not oriented to that. ... If a user space process can grab a semaphore and exit leaving that ...
    (comp.os.linux.development.system)