The following table describes some IPC examples you can download and what they demonstrate. IPC Examples
pipes Shows how to use the pipe system call to create a communication channel between a parent and child process. FIFO (named pipe) The previous example shows how to create a pipe, but that method is limited in that it can only be used between related processes. Using named pipes (or FIFOs) overcomes this limitation. This example shows how to use a named pipe. Message Passing While FIFOs are better than pipes, they too have some limitations as their name implies, data can only be read out in the order in which it entered the pipe. This example shows how to use the Unix message passing facility, which allows you to extract particular messages from the queueónot necessarily in FIFO order. (Note that this example doesn't actually use this functionality, though!) To compile this one on hawk, you need to specify the POSIX library so that a better sleep function is available. Use this command line: gcc -o msg msg.c -lposix4
Shared Memory (Unix) While all of the previous examples accomplish IPC, they all have to go through the kernel to get the data from one process to another. The fastest way to get data from one process to another is to just put the data in the memory space of the other process! This is the idea of shared memory. This example shows how to use the shared memory facility in Unix to do just that. Shared Memory (NT) While all of the ways of doing IPC shown so far are for Unix, nearly all of them can be done in Windows 9x/NT as well. Unfortunately, the code is a lot more complicated in most cases (and some things only work on NT and not 9xólike creating named pipes). Therefore, we'll concentrate on how to share memory between 2 NT/9x processes. This is done via what is called a memory mapped file. The basic idea is to map the contents of a disk file to some memory address so you can just use the memory to modify the file. Putting a character in the memory location causes the system to write it to the file as well. (Unix also supports this idea via the mmap function call.)
In NT you don't have to have an actual file, though. If you specify a special value to the CreateFileMapping function, then the system will create a section of memory from the page file and share it. The effect is to share "anonymous" memory.
This example includes 2 processes: a parent and a child each of which maps the same shared memory region. The child then puts some random data in the memory and the parent reads it back (this is also what the Unix shared memory example does).
To hide some of the uglies of creating the shared memory, I created a SharedMem class that both processes use.
Thanks to Gary Weinstein for these examples.