Shared Memory

Shared memory objects may be allocated and used to pass information between specific processes. Such memory objects may be named or anonymous.

The example that follows assumes that two processes are created in the system: a client process that accepts user input and displays results, and a server process that accepts requests from the client, accesses data objects and returns the requested data to the client. Both of these processes create windows.

In order to communicate a request to the server, the client must first allocate a shared memory object, using the DosAllocSharedMem() function as described in The Flat Memory Model. Since the process ID of the server process is known to the client, the client can provide access to the shared memory object for the server process, using the DosGiveSharedMem() function, which is also described in The Flat Memory Model. This technique is shown in Figure "Interprocess Communication Using Shared Memory (Part 1)".

If the technique described in Creating Another Process is followed, and the server process has posted a message to the client at the completion of server initialization, containing the window handle of the server's object window, the client can dispatch the request as an application-defined Presentation Manager message to the server's object window, with a pointer to the memory object as a message parameter. The server process then obtains access to the object using the DosGetSharedMem() function, as shown in Figure "Interprocess Communication Using Shared Memory (Part 2)".

In the simplest case where the client process has only one window, the handle of this window is passed to the server process when it is created, as part of the DosExecPgm() call, as shown in Figure "Starting a Child Process". Hence the server has access to the client's window handle and can pass the return data to the client. In a more complex situation where the client process has several windows and where a request can come from any of these, the handle can be passed as part of the request structure, as shown in Figure "Interprocess Communication Using Shared Memory (Part 1)" and Figure "Interprocess Communication Using Shared Memory (Part 2)".

Another issue that arises when using shared memory to communicate between processes is that of freeing the shared memory object. When a process issues a DosGiveSharedMem() or DosGetSharedMem() call, the operating system increments a usage counter for the shared memory object, and will not release the memory until all processes using the object have issued a DosFreeMem() call.

For the server process that receives access to the shared memory object, the DosFreeMem() call is simply made whenever the server process has finished with the contents of the memory object. For the client process that initially creates the memory object, the DosFreeMem() call can be made at either of two points:

  • If the client process does not care whether the request is correctly received by the server process, the DosFreeMem() call can be made immediately after passing the message to the server, as shown in Figure "Interprocess Communication Using Shared Memory (Part 1)".

  • If the client wishes to guarantee delivery of the request, it must pass the message, wait for an acknowledgement of receipt from the server, and then issue the DosFreeMem() call. This acknowledgement may simply be an indication of receipt, prior to the server processing the request, or may be the returned data from the request.

    Note that it is common for returned data from a request to be passed using the same memory object as was used to contain the original request. In such cases, the memory object cannot be freed by the client process until the returned data is received and processed.


    [Back: Presentation Manager Messages]
    [Next: Atoms]