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

00001 // ipcparent.c
00002 //
00003 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
00004 // All rights reserved.
00005 // This component and the accompanying materials are made available
00006 // under the terms of "Eclipse Public License v1.0"
00007 // which accompanies this distribution, and is available
00008 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00009 //
00010 // Initial Contributors:
00011 // Nokia Corporation - initial contribution.
00012 //
00013 // Contributors:
00014 //
00015 // Description:
00016 //
00017 
00018 //
00019 
00020 #include <stdio.h>
00021 #include <fcntl.h>
00022 #include <unistd.h>
00023 #include <stdlib.h>
00024 #include <sys/stat.h>
00025 #include <errno.h>
00026 #include <spawn.h>
00027 #include <string.h>
00028 #include <sys/wait.h>
00029 
00034 char PressKey()
00035         {
00036         char ch;
00037         fflush(stdout);
00038         ch=getchar();
00039         return ch;
00040         }
00041 
00047 int Error(char msg[])
00048         {
00050         printf("%s [Error NUMBER = %d]\n",msg,errno);
00051         PressKey();
00052         return EXIT_FAILURE;
00053         }
00054 
00055 int main()
00056         {
00058         FILE* childProcessStream;
00060         char fifoFileName[] = "myfifofile";
00062         char fifoExecFileName[] = "z:\\sys\\bin\\fifochild";
00064         char popenExecFileName[] = "z:\\sys\\bin\\ipcchild";
00066         pid_t fifoChildPid;
00068         int fifoRetVal;
00069         int unlinkResult;
00070         int fifoResult;
00072         int     childProcessFD;
00073         
00074         char **argv=(char**)malloc(2*sizeof(char*));
00075     argv[0]=(char*)malloc(50*sizeof(char));
00076         argv[1] = 0;
00077         strcpy(argv[0], fifoExecFileName);
00078 
00082     unlinkResult=unlink(fifoFileName);
00083         fifoResult = mkfifo(fifoFileName,S_IXGRP);
00085         if(fifoResult == -1)
00086                 {
00087                         Error("*** failure mkfifo ***");
00088                         PressKey();
00089                         return EXIT_FAILURE;
00090                 }
00092         fifoRetVal  = posix_spawn(&fifoChildPid, fifoExecFileName, NULL, NULL, argv, (char**)NULL);
00094         free((void*)argv[0]);
00095         free((void*)argv);
00096         if(fifoRetVal != 0)
00097                 {
00098                 printf("failure posix_spawn [Error NUMBER = %d]\n",errno);
00099         unlinkResult=unlink(fifoFileName);
00100         if(unlinkResult!=0)
00101             {
00102             printf("failure posix_spawn [Error NUMBER = %d]\n",errno);
00103             }
00104                 PressKey();
00105                 return EXIT_FAILURE;
00106                 }
00107         else
00108                 {
00110                 int fifoFd = open(fifoFileName,O_RDONLY);
00112                 if(fifoFd == -1)
00113                         {
00115                         Error("*** failure FIFO Open ***");
00116                         PressKey();
00117                         return EXIT_FAILURE;
00118                         }
00119                 else
00120                         {
00122                         char buffer[100];
00123                         int nbytes;
00124                         memset(buffer,0,sizeof(buffer));
00126                         nbytes = read(fifoFd,buffer,sizeof(buffer));
00127                         printf("\nBytes Received=%d, Message Received from fifochild using [FIFO]=%s",nbytes,buffer);
00129                         (void)close(fifoFd);
00130                         }
00132                 childProcessStream = popen(popenExecFileName,"r");
00134                 if(childProcessStream == NULL)
00135                         {
00136                         Error("\n Failure to create child process with popen()");
00137                         PressKey();
00138                         return EXIT_FAILURE;
00139                         }
00140                 else
00141                         {
00143                         char buffer[100];
00145                         int nbytes;
00147                         childProcessFD = fileno(childProcessStream);
00148                         memset(buffer,0,sizeof(buffer));
00149                         printf("\n\nWaiting for message from Child[popen()]\n");
00151                         nbytes = read(childProcessFD,buffer,sizeof(buffer));
00152                         printf("Bytes Received=%d, Message Received from ipcchild using[popen()]=%s\n[ok]\n",nbytes,buffer);
00154                         pclose(childProcessStream);
00155                         }
00157                 (void)waitpid(fifoChildPid,NULL,0);
00159                 unlinkResult=unlink(fifoFileName);
00160                 if(unlinkResult!=0)
00161                         {
00162                         Error("Unlink error");
00163                         }
00164                 }
00165         printf("Press any key to exit...");
00166         PressKey();
00167         return EXIT_SUCCESS;
00168         }

Generated by  doxygen 1.6.2