Skip to content

Instantly share code, notes, and snippets.

@blaquee
Last active August 29, 2015 14:08
Show Gist options
  • Save blaquee/fe8f0cd3b80b4f971832 to your computer and use it in GitHub Desktop.
Save blaquee/fe8f0cd3b80b4f971832 to your computer and use it in GitHub Desktop.
c pipes
int open_pipe(int *readpipe, int *writepipe)
{
int fdpipe[2]; // read, write
#if defined(WIN32) || defined(_WIN32)
int r = pipe( fdpipe, BUFFER_SIZE, O_BINARY );
#else
int r = pipe(fdpipe);
#endif
*readpipe = fdpipe[0];
*writepipe = fdpipe[1];
return r;
}
Use case:
printf("CMD: %s\n", consumer_cmd);
int rdpipe, wrpipe;
open_pipe(&rdpipe, &wrpipe);
// on windows must open in binary mode
FILE *consumerpipe = popen(consumer_cmd, OPEN_MODE_STR);
// send the pipe handle
fwrite(&wrpipe, sizeof(int), 1, consumerpipe);
// send it to the consumer
fwrite(buf, 1, recvlen, consumerpipe);
fflush(consumerpipe);
pclose(consumerpipe);
Binary mode was only limitation I encoutnered.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment