Symbian3/SDK/Source/GUID-383C128C-15EB-4C64-9A1F-F1EA826323D0.dita
changeset 7 51a74ef9ed63
parent 0 89d6a7a84779
equal deleted inserted replaced
6:43e37759235e 7:51a74ef9ed63
       
     1 <?xml version="1.0" encoding="utf-8"?>
       
     2 <!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
       
     3 <!-- This component and the accompanying materials are made available under the terms of the License 
       
     4 "Eclipse Public License v1.0" which accompanies this distribution, 
       
     5 and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
       
     6 <!-- Initial Contributors:
       
     7     Nokia Corporation - initial contribution.
       
     8 Contributors: 
       
     9 -->
       
    10 <!DOCTYPE concept
       
    11   PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
       
    12 <concept id="GUID-383C128C-15EB-4C64-9A1F-F1EA826323D0" xml:lang="en"><title>Parent
       
    13 and Child IPC Using Named Pipes or FIFOs</title><prolog><metadata><keywords/></metadata></prolog><conbody>
       
    14 <p>An alternative approach to using the <xref href="GUID-A9DB6E7C-B8D6-377A-BBE6-39E0A7A09E5D.dita"><apiname>popen()</apiname></xref> function
       
    15 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
       
    16 is that they allow the code in both the parent and child processes to continue
       
    17 to use file descriptors for communication rather than streams (and avoid modification
       
    18 to the<codeph> stdin()</codeph>/<codeph>stdout()</codeph> streams of the child
       
    19 process). </p>
       
    20 <p>In addition, since each created FIFO is referenced as a file in the file
       
    21 system, FIFOs allow for more complicated IPC schemes than those offered by
       
    22 the <xref href="GUID-A9DB6E7C-B8D6-377A-BBE6-39E0A7A09E5D.dita"><apiname>popen()</apiname></xref> function, for example, inter-child process
       
    23 communication. For more information about the use of FIFOs, see <xref href="http://www.opengroup.org" scope="external">http:\\www.opengroup.org</xref>. </p>
       
    24 <p><b>Parent process P.I.P.S. example using FIFOs</b></p>
       
    25 <p>The following code shows how FIFOs can be used in P.I.P.S. by the parent
       
    26 process.</p>
       
    27 <codeblock xml:space="preserve">int main(int argc, char *argv[])
       
    28 {
       
    29    char fifoFileName[] = "/root/PortDoc/Example2_c/Symbian/fifofile";
       
    30 
       
    31    int fifoResult = mkfifo(fifoFileName,S_IXGRP);
       
    32   
       
    33    if(fifoResult == -1)
       
    34    {
       
    35       //FIFO creation failure.
       
    36       printf("\n*** failure mkfifo ***\n");
       
    37 
       
    38       return EXIT_FAILURE;
       
    39    }
       
    40    else
       
    41    {
       
    42       //FIFO creation successful.
       
    43       
       
    44       //Spawn the child process.
       
    45       pid_t Childpid;
       
    46       char execFileName[] = "/root/PortDoc/Example2_c/Symbian/ChildProg";
       
    47       int RetVal= posix_spawn(&amp;Childpid,execFileName,NULL,NULL,NULL,NULL);
       
    48       
       
    49       if(RetVal != 0)
       
    50       {
       
    51          printf("\n*** failure posix_spawn ***\n");
       
    52 
       
    53          return EXIT_FAILURE;
       
    54       }
       
    55       else
       
    56       {
       
    57          //Open the FIFO. Parent reads from the FIFO
       
    58          int ReadFifoFd = open(fifoFileName,O_RDONLY);
       
    59  
       
    60          if(ReadFifoFd == -1)
       
    61          {
       
    62             //Failed to open the Fifo
       
    63             printf("\n*** failure Fifo Open ***\n");
       
    64 
       
    65             return EXIT_FAILURE;
       
    66          }
       
    67          else
       
    68          {
       
    69             //create a receive buffer and clear
       
    70             char RxBuffer[100];
       
    71             memset(RxBuffer,0,sizeof(RxBuffer));
       
    72       
       
    73             //Wait for data from the child process. Child sends a string.
       
    74             int nbytes = read(ReadFifoFd,RxBuffer,sizeof(RxBuffer));
       
    75 
       
    76             printf("\nMessage Received by Parent=%s",RxBuffer);
       
    77       
       
    78             //close the FIFO
       
    79             (void)close(ReadFifoFd);
       
    80          }
       
    81 
       
    82          //wait for the child process to finish      
       
    83          (void)waitpid(Childpid,NULL,0);
       
    84    
       
    85          //unlink the FIFO
       
    86          unlink(fifoFileName);
       
    87       }
       
    88    }
       
    89    return EXIT_SUCCESS;
       
    90 }
       
    91 </codeblock>
       
    92 <p><b>Child process P.I.P.S. example using FIFOs</b></p>
       
    93 <p>The following code shows how FIFOs can be used in P.I.P.S. by the child
       
    94 process. </p>
       
    95 <codeblock xml:space="preserve">int main(int argc, char *argv[])
       
    96 {
       
    97    char fifoFileName[] = "/root/PortDoc/Example2_c/Symbian/fifofile";
       
    98 
       
    99    //Open the FIFO. child writes to parent
       
   100    int WriteFifoFd = open(fifoFileName,O_WRONLY);
       
   101      
       
   102    if(WriteFifoFd == -1)
       
   103    {
       
   104       //Failed to open the Fifo
       
   105       printf("\n*** child failure Fifo Open ***\n");
       
   106       return EXIT_FAILURE;
       
   107    }
       
   108    else
       
   109    {
       
   110       //create a message to send.
       
   111       char TxMsg[] = "Hello Parent\n";
       
   112        
       
   113       //Wait for data from the child process. Child sends a string.
       
   114       write(WriteFifoFd,TxMsg,sizeof(TxMsg));
       
   115      
       
   116       //close the FIFO
       
   117      (void)close(WriteFifoFd);
       
   118    }
       
   119    return EXIT_SUCCESS;
       
   120 }
       
   121 </codeblock>
       
   122 </conbody></concept>