examples/PIPS/IPC/src/parent/ipcparent.c

00001 // ipcparent.c
00002 //
00003 /*
00004 Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00005 
00006 Redistribution and use in source and binary forms, with or without
00007 modification, are permitted provided that the following conditions are met:
00008 
00009 * Redistributions of source code must retain the above copyright notice, this
00010   list of conditions and the following disclaimer.
00011 * Redistributions in binary form must reproduce the above copyright notice,
00012   this list of conditions and the following disclaimer in the documentation
00013   and/or other materials provided with the distribution.
00014 * Neither the name of Nokia Corporation nor the names of its contributors
00015   may be used to endorse or promote products derived from this software
00016   without specific prior written permission.
00017 
00018 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00021 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00022 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00023 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00024 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00025 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00027 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 
00029 Description:  
00030 */
00031 
00032 
00033 
00034 
00035 #include <stdio.h>
00036 #include <fcntl.h>
00037 #include <unistd.h>
00038 #include <stdlib.h>
00039 #include <sys/stat.h>
00040 #include <errno.h>
00041 #include <spawn.h>
00042 #include <string.h>
00043 #include <sys/wait.h>
00044 
00049 char PressKey()
00050         {
00051         char ch;
00052         fflush(stdout);
00053         ch=getchar();
00054         return ch;
00055         }
00056 
00062 int Error(char msg[])
00063         {
00065         printf("%s [Error NUMBER = %d]\n",msg,errno);
00066         PressKey();
00067         return EXIT_FAILURE;
00068         }
00069 
00070 int main()
00071         {
00073         FILE* childProcessStream;
00075         char fifoFileName[] = "myfifofile";
00077         char fifoExecFileName[] = "z:\\sys\\bin\\fifochild";
00079         char popenExecFileName[] = "z:\\sys\\bin\\ipcchild";
00081         pid_t fifoChildPid;
00083         int fifoRetVal;
00084         int unlinkResult;
00085         int fifoResult;
00087         int     childProcessFD;
00088         
00089         char **argv=(char**)malloc(2*sizeof(char*));
00090     argv[0]=(char*)malloc(50*sizeof(char));
00091         argv[1] = 0;
00092         strcpy(argv[0], fifoExecFileName);
00093 
00097     unlinkResult=unlink(fifoFileName);
00098         fifoResult = mkfifo(fifoFileName,S_IXGRP);
00100         if(fifoResult == -1)
00101                 {
00102                         Error("*** failure mkfifo ***");
00103                         PressKey();
00104                         return EXIT_FAILURE;
00105                 }
00107         fifoRetVal  = posix_spawn(&fifoChildPid, fifoExecFileName, NULL, NULL, argv, (char**)NULL);
00109         free((void*)argv[0]);
00110         free((void*)argv);
00111         if(fifoRetVal != 0)
00112                 {
00113                 printf("failure posix_spawn [Error NUMBER = %d]\n",errno);
00114         unlinkResult=unlink(fifoFileName);
00115         if(unlinkResult!=0)
00116             {
00117             printf("failure posix_spawn [Error NUMBER = %d]\n",errno);
00118             }
00119                 PressKey();
00120                 return EXIT_FAILURE;
00121                 }
00122         else
00123                 {
00125                 int fifoFd = open(fifoFileName,O_RDONLY);
00127                 if(fifoFd == -1)
00128                         {
00130                         Error("*** failure FIFO Open ***");
00131                         PressKey();
00132                         return EXIT_FAILURE;
00133                         }
00134                 else
00135                         {
00137                         char buffer[100];
00138                         int nbytes;
00139                         memset(buffer,0,sizeof(buffer));
00141                         nbytes = read(fifoFd,buffer,sizeof(buffer));
00142                         printf("\nBytes Received=%d, Message Received from fifochild using [FIFO]=%s",nbytes,buffer);
00144                         (void)close(fifoFd);
00145                         }
00147                 childProcessStream = popen(popenExecFileName,"r");
00149                 if(childProcessStream == NULL)
00150                         {
00151                         Error("\n Failure to create child process with popen()");
00152                         PressKey();
00153                         return EXIT_FAILURE;
00154                         }
00155                 else
00156                         {
00158                         char buffer[100];
00160                         int nbytes;
00162                         childProcessFD = fileno(childProcessStream);
00163                         memset(buffer,0,sizeof(buffer));
00164                         printf("\n\nWaiting for message from Child[popen()]\n");
00166                         nbytes = read(childProcessFD,buffer,sizeof(buffer));
00167                         printf("Bytes Received=%d, Message Received from ipcchild using[popen()]=%s\n[ok]\n",nbytes,buffer);
00169                         pclose(childProcessStream);
00170                         }
00172                 (void)waitpid(fifoChildPid,NULL,0);
00174                 unlinkResult=unlink(fifoFileName);
00175                 if(unlinkResult!=0)
00176                         {
00177                         Error("Unlink error");
00178                         }
00179                 }
00180         printf("Press any key to exit...");
00181         PressKey();
00182         return EXIT_SUCCESS;
00183         }

Generated by  doxygen 1.6.2