0
votes

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

1
You're trying to write to something you opened read-only by the "r" flag to fopen(). - Andrew Henle

1 Answers

3
votes

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.