Symbian3/PDK/Source/GUID-383C128C-15EB-4C64-9A1F-F1EA826323D0.dita
author Dominic Pinkman <Dominic.Pinkman@Nokia.com>
Tue, 30 Mar 2010 11:56:28 +0100
changeset 5 f345bda72bc4
parent 3 46218c8b8afa
child 14 578be2adaf3e
permissions -rw-r--r--
Week 12 contribution of PDK documentation_content. See release notes for details. Fixes Bug 2054, Bug 1583, Bug 381, Bug 390, Bug 463, Bug 1897, Bug 344, Bug 1319, Bug 394, Bug 1520, Bug 1522, Bug 1892"

<?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-383C128C-15EB-4C64-9A1F-F1EA826323D0" xml:lang="en"><title>Parent
and Child IPC Using Named Pipes or FIFOs</title><prolog><metadata><keywords/></metadata></prolog><conbody>
<p>An alternative approach to using the <xref href="GUID-A9DB6E7C-B8D6-377A-BBE6-39E0A7A09E5D.dita"><apiname>popen()</apiname></xref> function
is to use named pipes, or FIFOs. The advantage of using them over the <xref href="GUID-A9DB6E7C-B8D6-377A-BBE6-39E0A7A09E5D.dita"><apiname>popen()</apiname></xref> mechanism
is that they allow the code in both the parent and child processes to continue
to use file descriptors for communication rather than streams (and avoid modification
to the<codeph> stdin()</codeph>/<codeph>stdout()</codeph> streams of the child
process). </p>
<p>In addition, since each created FIFO is referenced as a file in the file
system, FIFOs allow for more complicated IPC schemes than those offered by
the <xref href="GUID-A9DB6E7C-B8D6-377A-BBE6-39E0A7A09E5D.dita"><apiname>popen()</apiname></xref> function, for example, inter-child process
communication. For more information about the use of FIFOs, see <xref href="http://www.opengroup.org" scope="external">http:\\www.opengroup.org</xref>. </p>
<p><b>Parent process P.I.P.S. example using FIFOs</b></p>
<p>The following code shows how FIFOs can be used in P.I.P.S. by the parent
process.</p>
<codeblock xml:space="preserve">int main(int argc, char *argv[])
{
   char fifoFileName[] = "/root/PortDoc/Example2_c/Symbian/fifofile";

   int fifoResult = mkfifo(fifoFileName,S_IXGRP);
  
   if(fifoResult == -1)
   {
      //FIFO creation failure.
      printf("\n*** failure mkfifo ***\n");

      return EXIT_FAILURE;
   }
   else
   {
      //FIFO creation successful.
      
      //Spawn the child process.
      pid_t Childpid;
      char execFileName[] = "/root/PortDoc/Example2_c/Symbian/ChildProg";
      int RetVal= posix_spawn(&amp;Childpid,execFileName,NULL,NULL,NULL,NULL);
      
      if(RetVal != 0)
      {
         printf("\n*** failure posix_spawn ***\n");

         return EXIT_FAILURE;
      }
      else
      {
         //Open the FIFO. Parent reads from the FIFO
         int ReadFifoFd = open(fifoFileName,O_RDONLY);
 
         if(ReadFifoFd == -1)
         {
            //Failed to open the Fifo
            printf("\n*** failure Fifo Open ***\n");

            return EXIT_FAILURE;
         }
         else
         {
            //create a receive buffer and clear
            char RxBuffer[100];
            memset(RxBuffer,0,sizeof(RxBuffer));
      
            //Wait for data from the child process. Child sends a string.
            int nbytes = read(ReadFifoFd,RxBuffer,sizeof(RxBuffer));

            printf("\nMessage Received by Parent=%s",RxBuffer);
      
            //close the FIFO
            (void)close(ReadFifoFd);
         }

         //wait for the child process to finish      
         (void)waitpid(Childpid,NULL,0);
   
         //unlink the FIFO
         unlink(fifoFileName);
      }
   }
   return EXIT_SUCCESS;
}
</codeblock>
<p><b>Child process P.I.P.S. example using FIFOs</b></p>
<p>The following code shows how FIFOs can be used in P.I.P.S. by the child
process. </p>
<codeblock xml:space="preserve">int main(int argc, char *argv[])
{
   char fifoFileName[] = "/root/PortDoc/Example2_c/Symbian/fifofile";

   //Open the FIFO. child writes to parent
   int WriteFifoFd = open(fifoFileName,O_WRONLY);
     
   if(WriteFifoFd == -1)
   {
      //Failed to open the Fifo
      printf("\n*** child failure Fifo Open ***\n");
      return EXIT_FAILURE;
   }
   else
   {
      //create a message to send.
      char TxMsg[] = "Hello Parent\n";
       
      //Wait for data from the child process. Child sends a string.
      write(WriteFifoFd,TxMsg,sizeof(TxMsg));
     
      //close the FIFO
     (void)close(WriteFifoFd);
   }
   return EXIT_SUCCESS;
}
</codeblock>
</conbody></concept>