00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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 }