[英]Writing and reading named pipes
我想从管道中写入和读取。 我用 C 编写了一个代码,它编译成功,但是当我通过 ./write 运行它时,它有运行时错误。 这是我的读写代码。
#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
int main(void) {
int fd, retval;
char buffer[] = "TESTDATAAA";
fflush(stdin);
retval = mkfifo("/temp/myfifo",0666);
fd = open("/temp/myfifo",O_WRONLY);
write(fd,buffer,sizeof(buffer));
close(fd);
return 0;
}
#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
int main() {
int fd, retval;
char buffer[] = "TESTDATA";
fd = open("/temp/myfifo",O_RDONLY);
retval = read(fd, buffer, sizeof(buffer));
fflush(stdin);
write(1, buffer, sizeof(buffer));
printf("\n");
close(fd);
return 0;
}
任何帮助将不胜感激。
“什么都不做”与“有运行时错误”非常不同。 如果您运行./write
并且程序块,这是预期的行为。 fopen
不会返回,直到某个其他进程打开 fifo 进行读取。
打开第二个终端并在生产者运行时执行您的消费者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.