Daniel Moody
Daniel Moody

Reputation: 45

Reading/Writing to UNIX pipes in C

Currently working on an assignment in which we are working with named pipes in C. I need to be able to read and write to a pipe. Here's where I am confused. I know there are kind of two different types of pipes (or different ways to make them). I know that there is a pipe function that you can read and write to an anonymous pipe. In our assignment writeup, I learned that you use mkfifo to create a named pipe in the directory. However, it also stated that you can then use that pipe like a normal file but when I try to use it as such, it just hangs. Here's my code:

#include <stdio.h>

int main(int argc, char * argv[]){

  FILE *fp;
  fp = fopen("pipe", "r");
  char c = 'o';

  fputc(c, fp);
  fclose(fp);

}

So are pipes not actually usable in this way? Any help is greatly appreciated! Thanks

Upvotes: 0

Views: 586

Answers (1)

Andrew Henle
Andrew Henle

Reputation: 1

A writeable file open call (fopen( "pipe", "w" ), open( "pipe", O_WRONLY ), etc.) to a FIFO/named pipe will hang until the pipe is opened for reading.

Upvotes: 3

Related Questions