Symbian3/SDK/Source/GUID-209F3620-2361-4AED-9F7A-02E72F9EE8FE.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-209F3620-2361-4AED-9F7A-02E72F9EE8FE" xml:lang="en"><title>Parent
       
    13 and Child without IPC</title><prolog><metadata><keywords/></metadata></prolog><conbody>
       
    14 <p>In this scenario, the parent process creates the child, but there is no
       
    15 subsequent communication between the two (except maybe the parent waiting
       
    16 for termination of the child via use of the <xref href="GUID-8F5C89A4-7813-32C7-973E-F7F0F3690BC6.dita"><apiname>waitpid()</apiname></xref> function).</p>
       
    17 <p>Rather than use the <codeph>fork()</codeph>/<codeph>exec()</codeph> combination
       
    18 to achieve this the <xref href="GUID-E7C4DE71-BC5B-34AE-ACB3-C34A0DB1FC16.dita"><apiname>posix_spawn()</apiname></xref> API can be used instead.
       
    19 This API will create a process by a single API call. Additionally, actions
       
    20 can be performed on inherited file descriptors to change access before the
       
    21 child's main is called. For more detailed information on the <xref href="GUID-E7C4DE71-BC5B-34AE-ACB3-C34A0DB1FC16.dita"><apiname>posix_spawn()</apiname></xref> function
       
    22 consult the relevant Open Group standard pages available at <xref href="http://www.opengroup.org" scope="external">http:\\www.opengroup.org</xref>.</p>
       
    23 <p><b>Parent process <codeph>fork()</codeph> and <codeph>exec()</codeph> functions</b></p>
       
    24 <p>The following code shows how the creation of a child process can be implemented
       
    25 using the <codeph>fork()</codeph> and <codeph>exec()</codeph> functions. Note
       
    26 that inclusion of header files has been omitted for clarity. </p>
       
    27 <codeblock xml:space="preserve">int main(int argc, char *argv[])
       
    28 {
       
    29   pid_t Childpid = fork();
       
    30      
       
    31   if(Childpid == 0)
       
    32   {
       
    33       //Running in Child process
       
    34 
       
    35       //exec() to replace the child process executable
       
    36       char execFileName[] = "/root/PortDoc/Example0_c/Posix/child/ChildProg";
       
    37 
       
    38       execl(execFileName,NULL);
       
    39   }
       
    40   else
       
    41   {
       
    42      //Running in parent process
       
    43 
       
    44      //Wait for the child process to terminate
       
    45      waitpid(Childpid,NULL,0);
       
    46      
       
    47      printf("\r\n*** Child process finished ***\r\n");
       
    48      
       
    49   }
       
    50    
       
    51   return EXIT_SUCCESS;
       
    52 }
       
    53 </codeblock>
       
    54 <p><b>Using the <codeph>posix_spawn()</codeph> function</b></p>
       
    55 <p>The following code shows how the above can be modified to use the <codeph>posix_spawn()</codeph> operation.</p>
       
    56 <codeblock xml:space="preserve">int main(int argc, char *argv[])
       
    57 {
       
    58    pid_t Childpid;
       
    59    char execFileName[] = "/root/PortDoc/Example0_c/Symbian/child/ChildProg";
       
    60    
       
    61    int RetVal= posix_spawn(&amp;Childpid,execFileName,NULL,NULL,NULL,NULL);
       
    62    
       
    63    (void)waitpid(Childpid,NULL,0);
       
    64    
       
    65    printf("\r\n*** Child process finished ***\r\n");
       
    66     
       
    67    return EXIT_SUCCESS;
       
    68 }
       
    69 </codeblock>
       
    70 <p><b>Child process example</b></p>
       
    71 <p>The child code shown is unaffected by the mechanism used to spawn it, that
       
    72 is, <codeph>fork()</codeph>/<codeph>exec()</codeph> or <xref href="GUID-E7C4DE71-BC5B-34AE-ACB3-C34A0DB1FC16.dita"><apiname>posix_spawn()</apiname></xref>,
       
    73 but an example is shown below for completeness.</p>
       
    74 <codeblock xml:space="preserve">int main(void)
       
    75 {
       
    76     printf("\r\n*** Child Running ***\r\n");
       
    77     
       
    78     return EXIT_SUCCESS;
       
    79 }
       
    80 </codeblock>
       
    81 </conbody></concept>