IPC: IPC Mechanisms

This example demonstrates how a parent process communicates with its child process using various IPC mechanisms. Two executables, a parent and a child (spawned by the parent) are created to demonstrate these mechanisms. The child process is created by the parent using the posix_spawn() function. The parent process communicates with the child process using the following IPC mechanisms:

  • Named pipes (FIFO - First in First Out)

  • Single pipe (popen() system call)

Download

Click on the following link to download the example: IPC.zip

Click: browse to view the example code.

Description

Spawning the child process

The posix_spawn() function creates the child process. A pointer to the process ID (pid) of the child process and the path of the child process are passed as arguments to this function. The parent waits for the child to terminate by calling the waitpid() function.

Creating a named pipe

This example creates a named pipe using the mkfifo() function. The child writes data into the FIFO and then exits. The parent reads data from FIFO and writes it to the console. The first argument of the mkfifo() function indicates the path of the FIFO file.

Opening a single pipe

The parent creates a child process and a pipe to the child process using the popen() function. A file handle to the pipe is returned to the parent and the parent uses this handle to read the data written to the pipe by the child. The path of the child process is passed as the first argument to the popen() function.

Build

The following statements are present in all the .mmp files of the example:

SYSTEMINCLUDE epoc32\include\stdapis
LIBRARY libc.lib
STATICLIBRARY libcrt0.lib

Note: You require these statements because of the following reasons:

  • The epoc32\include\stdapis directory contains header files such as, stdio.h, unistd.h and so on that are required to build an Open Environment application.

  • The libc.lib file contains the definitions of all functions that are defined in the header files associated with this example.

  • The libcrt0.lib file provides the E32Main() function, which is the entry point for the main() function of the example.

The IPC builds the following binaries in the standard location (\epoc32\release\winscw\<build_variant> for Carbide.c++).

  • ipcparent.exe: Executable of the parent process.

  • ipcchild.exe: Executable of the child process created using the popen() function.

  • fifochild.exe: Executable of the child process created using the posix_spawn() function.

Runtime setting

Two eshells must be opened before executing this example. The executable, ipcparent.exe is run in one eshell and the other eshell is used to run the child process fifochild.exe. You can switch between the two eshells by pressing CTRL +ALT +SHIFT +T.