Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!cs.utexas.edu!uunet!virtech!cpcahil From: cpcahil@virtech.uucp (Conor P. Cahill) Newsgroups: comp.lang.c Subject: Re: help on interprocess communication Message-ID: <1990Feb19.164006.918@virtech.uucp> Date: 19 Feb 90 16:40:06 GMT References: <6456@cps3xx.UUCP> Reply-To: cpcahil@virtech.UUCP (Conor P. Cahill) Distribution: usa Organization: Virtual Technologies Inc., Sterling VA Lines: 91 In article <6456@cps3xx.UUCP> bruey@cpsin2.cps.msu.edu () writes: > pipe(filedes); > if (fork()) > { > printf("%d %d are filedes \n",filedes[0],filedes[1]); > buf = "first message"; > write(filedes[1],buf,14); > if(fork()) > { > read(filedes[1],buf1,14); > printf("in second %s \n",buf1); > } > } Read PIPE(2) which should state something like: filedes[0] is opened for reading and filedes[1] is opened for writing. There are also several comments that could be made as to the error checking that is going on (or isn't going on), but I will just shut up and give you an example of the use of fork() and pipe() from the imake sources: cppit(Imakefile, template, outfd) char *Imakefile; char *template; FILE *outfd; { FILE *pipeFile; int pid, pipefd[2]; #ifdef SYSV int status; #else /* !SYSV */ union wait status; #endif /* !SYSV */ char *cleanedImakefile; /* * Get a pipe. */ if (pipe(pipefd) < 0) LogFatal("Cannot make a pipe.", ""); /* * Fork and exec cpp */ pid = vfork(); if (pid < 0) LogFatal("Cannot fork.", ""); if (pid) { /* parent */ close(pipefd[0]); cleanedImakefile = CleanCppInput(Imakefile); if ((pipeFile = fdopen(pipefd[1], "w")) == NULL) LogFatalI("Cannot fdopen fd %d for output.", pipefd[1]); fprintf(pipeFile, "#define IMAKE_TEMPLATE\t\"%s\"\n", template); fprintf(pipeFile, "#define INCLUDE_IMAKEFILE\t\"%s\"\n", cleanedImakefile); fprintf(pipeFile, "#include IMAKE_TEMPLATE\n"); fclose(pipeFile); while (wait(&status) > 0) { errno = 0; #ifdef SYSV if ((status >> 8) & 0xff) LogFatalI("Signal %d.", (status >> 8) & 0xff); if (status & 0xff) LogFatalI("Exit code %d.", status & 0xff); #else /* !SYSV */ if (status.w_termsig) LogFatalI("Signal %d.", status.w_termsig); if (status.w_retcode) LogFatalI("Exit code %d.", status.w_retcode); #endif /* !SYSV */ } CleanCppOutput(outfd); } else { /* child... dup and exec cpp */ if (verbose) showargs(cpp_argv); dup2(pipefd[0], 0); dup2(fileno(outfd), 1); close(pipefd[1]); execv(cpp, cpp_argv); LogFatal("Cannot exec %s.", cpp); } } -- +-----------------------------------------------------------------------+ | Conor P. Cahill uunet!virtech!cpcahil 703-430-9247 ! | Virtual Technologies Inc., P. O. Box 876, Sterling, VA 22170 | +-----------------------------------------------------------------------+