Symbian3/SDK/Source/GUID-4AEBF49C-E62A-5D0D-8181-600BF29DFFC7.dita
author Dominic Pinkman <dominic.pinkman@nokia.com>
Tue, 20 Jul 2010 12:00:49 +0100
changeset 13 48780e181b38
parent 7 51a74ef9ed63
permissions -rw-r--r--
Week 28 contribution of SDK documentation content. See release notes for details. Fixes bugs Bug 1897 and Bug 1522.

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
<!-- This component and the accompanying materials are made available under the terms of the License 
"Eclipse Public License v1.0" which accompanies this distribution, 
and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
<!-- Initial Contributors:
    Nokia Corporation - initial contribution.
Contributors: 
-->
<!DOCTYPE concept
  PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
<concept id="GUID-4AEBF49C-E62A-5D0D-8181-600BF29DFFC7" xml:lang="en"><title>IPC: IPC Mechanisms</title><prolog><metadata><keywords/></metadata></prolog><conbody>
<p>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 <xref href="GUID-E7C4DE71-BC5B-34AE-ACB3-C34A0DB1FC16.dita"><apiname>posix_spawn()</apiname></xref> function. The parent process
communicates with the child process using the following IPC mechanisms: </p>
<ul>
<li id="GUID-D1CABBD1-AC3A-5216-B2A6-D860C876DEDE"><p>Named pipes
(FIFO - First in First Out) </p> </li>
<li id="GUID-8FE87BCB-200D-5498-8F1A-851B861F8673"><p>Single pipe
(<xref href="GUID-A9DB6E7C-B8D6-377A-BBE6-39E0A7A09E5D.dita"><apiname>popen()</apiname></xref> system call) </p> </li>
</ul>
<section id="GUID-E584CAE4-BC05-48D5-837C-40B73DD4524F"><title>Download</title><p>Click on the following link to download the example: <xref href="guid-6013a680-57f9-415b-8851-c4fa63356636/zips/guid-c5b6ac05-a3fb-45df-ab11-bd8ccc13537f.zip" scope="external">IPC.zip</xref></p><p>Click: <xref href="guid-6013a680-57f9-415b-8851-c4fa63356636/guid-c5b6ac05-a3fb-45df-ab11-bd8ccc13537f.html" scope="peer">browse</xref> to view the example code. </p> </section>
<section id="GUID-5513D686-D38A-49A9-B3B2-2259992A69A8"><title>Description</title> <p><b> Spawning the child process</b> </p> <p>The <xref href="GUID-E7C4DE71-BC5B-34AE-ACB3-C34A0DB1FC16.dita"><apiname>posix_spawn()</apiname></xref> function creates the child process. A pointer to the process ID
(<codeph>pid</codeph>) 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 <xref href="GUID-8F5C89A4-7813-32C7-973E-F7F0F3690BC6.dita"><apiname>waitpid()</apiname></xref> function. </p> <p><b> Creating a named pipe</b> </p> <p>This example
creates a named pipe using the <xref href="GUID-F4749DAA-1B29-3D1D-A3AA-0D52B851E501.dita"><apiname>mkfifo()</apiname></xref> 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 <xref href="GUID-F4749DAA-1B29-3D1D-A3AA-0D52B851E501.dita"><apiname>mkfifo()</apiname></xref> function indicates the path of the
FIFO file. </p> <p><b> Opening a single pipe</b> </p> <p>The parent
creates a child process and a pipe to the child process using the <xref href="GUID-A9DB6E7C-B8D6-377A-BBE6-39E0A7A09E5D.dita"><apiname>popen()</apiname></xref> 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 <xref href="GUID-A9DB6E7C-B8D6-377A-BBE6-39E0A7A09E5D.dita"><apiname>popen()</apiname></xref> function. </p> </section>
<section id="GUID-367A6465-F1C3-46B3-820B-7086E0BE1BEB"><title>Build</title> <p>The following statements are present in all the <filepath>.mmp</filepath> files of the example: </p> <codeblock id="GUID-09782F92-8B25-5CF5-8027-599685F5726D" xml:space="preserve">SYSTEMINCLUDE epoc32\include\stdapis
LIBRARY libc.lib
STATICLIBRARY libcrt0.lib</codeblock> <p> <b>Note:</b> You require
these statements because of the following reasons: </p> <ul>
<li id="GUID-C735ED4D-4FDD-5535-ACA2-C645C3F10762"><p>The <filepath>epoc32\include\stdapis</filepath> directory contains header files
such as, <filepath>stdio.h</filepath>, <filepath>unistd.h</filepath> and so on that are required to build an Open Environment application. </p> </li>
<li id="GUID-6351CD50-ED02-5DC9-BAAF-971CB92EEAD3"><p>The <filepath>libc.lib</filepath> file contains the definitions of all functions
that are defined in the header files associated with this example. </p> </li>
<li id="GUID-C7A1D9C5-07E6-5FD1-B770-24180F04623A"><p>The <filepath>libcrt0.lib</filepath> file provides the <codeph>E32Main()</codeph> function, which is the entry point for the <codeph>main()</codeph> function of the example. </p> </li>
</ul> <p> The <codeph>IPC</codeph> builds the following binaries in
the standard location (<filepath>\epoc32\release\winscw\&lt;build_variant&gt;</filepath> for Carbide.c++). </p> <ul>
<li id="GUID-1F256024-8210-5C50-8B18-0498C0D5C15F"><p> <b>ipcparent.exe:</b> Executable of the parent process. </p> </li>
<li id="GUID-1C755509-96FA-510A-944B-3FEA065C9936"><p> <b>ipcchild.exe:</b> Executable of the child process created using the <xref href="GUID-A9DB6E7C-B8D6-377A-BBE6-39E0A7A09E5D.dita"><apiname>popen()</apiname></xref> function. </p> </li>
<li id="GUID-C056BC80-D664-5F25-A730-35220CA0A6B5"><p> <b>fifochild.exe:</b> Executable of the child process created using the <xref href="GUID-E7C4DE71-BC5B-34AE-ACB3-C34A0DB1FC16.dita"><apiname>posix_spawn()</apiname></xref> function. </p> </li>
</ul> </section>
<section id="GUID-9F7DC4C5-16C8-4223-80E0-19DDF9E8303E"><title>Runtime
setting</title> <p>Two eshells must be opened before executing this
example. The executable, <filepath>ipcparent.exe</filepath> is run
in one eshell and the other eshell is used to run the child process <filepath>fifochild.exe</filepath>. You can switch between the two eshells
by pressing <b>CTRL</b> +<b>ALT</b> +<b>SHIFT</b> +<b>T</b>. </p> </section>
</conbody></concept>