简体   繁体   English

在C中读取/写入UNIX管道

[英]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. 当前正在处理一个工作,其中我们正在使用C中的命名管道。我需要能够读写管道。 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. 在分配作业中,我了解到您使用mkfifo在目录中创建命名管道。 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 谢谢

对FIFO /命名管道的可写文件打开调用( fopen( "pipe", "w" )open( "pipe", O_WRONLY )等等)将挂起,直到打开该管道进行读取为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM