write() 和 read() 不适用于 stdio 而不是 fifo
write() and read() do not work on stdio instead of fifo
我有一个与 fifos 以及 write()
和 read()
函数相关的问题。我确实通过 mkfifo()
函数创建了 fifo,然后我使用 fork()
生成两个进程:第一个进程通过 write() 打开并写入 fifo;另一个打开并读取 read()。
我的问题是 write()
在 stdout 上写,read()
等我在 stdin 上写东西然后按 Enter,就像 scanf()
一样,因此不使用 fifo。我真的不知道该怎么办了,我什么都试过了,到目前为止还没有在互联网上发现任何相关问题。
我正在使用 gcc 编译器和 Xubuntu 15.10。
进程生成器
int main (void) {
int f = mkfifo("try", S_IRUSR | S_IWUSR);
if (f < 0)
printf("mkfifo went fine\n");
else
printf("mkfifo went wrong\n");
pid_t fo = fork();
switch (fo) {
case -1:
printf("err\n");
case 0:
execlp("prova_fifo2", "prova_fifo2", NULL);
default:
execlp("prova_fifo", "prova_fifo", NULL);
}
return (0);
}
作家(又名 prova_fifo)
int main (void) {
int fd;
if (fd = open("try", O_WRONLY) == -1)
printf("Error opening FIFO\n");
char buf[6];
sprintf(buf, "hello");
int writer = write(fd, buf, strlen(buf));
printf("%d", writer);
return (0);
}
Reader(又名 prova_fifo2)
int main (void) {
int fd;
if (fd = open("try", O_RDONLY) ==-1)
printf("Errore in apertura FIFO\n");
char buf[6];
read(fd,&buf, strlen(buf)); //tried removing the "&", didn't work
printf("READ: %s", buf);
return (0);
}
注意 我包括所有必要的库,代码编译和运行。我唯一的问题是上面提到的那个。
这是一个 operator precedence 问题,表达式 fd=open("try", O_WRONLY )==-1
等同于 fd=(open("try", O_WRONLY )==-1)
这绝对不是您想要的,因为它将分配 open(...)==-1
的结果如果可以找到文件,fd
将为 false(即 0
,这是标准 input 的文件描述符),1
如果找不到文件。
你需要做 (fd=open("try", O_WRONLY ))==-1
.
您不能明智地在未初始化的 char
数组上调用 strlen()
。你应该这样做:
read(fd, buf, sizeof(buf));
相反。
请注意,read()
不会对您的数组进行零终止,并且 printf()
预计会收到 %s
这样的内容,因此您可能希望将整个内容更改为:
char buf[7] = {0};
if ( read(fd, buf, sizeof(buf) - 1) > 0 ) {
printf("READ: %s\n", buf);
}
我有一个与 fifos 以及 write()
和 read()
函数相关的问题。我确实通过 mkfifo()
函数创建了 fifo,然后我使用 fork()
生成两个进程:第一个进程通过 write() 打开并写入 fifo;另一个打开并读取 read()。
我的问题是 write()
在 stdout 上写,read()
等我在 stdin 上写东西然后按 Enter,就像 scanf()
一样,因此不使用 fifo。我真的不知道该怎么办了,我什么都试过了,到目前为止还没有在互联网上发现任何相关问题。
我正在使用 gcc 编译器和 Xubuntu 15.10。
进程生成器
int main (void) {
int f = mkfifo("try", S_IRUSR | S_IWUSR);
if (f < 0)
printf("mkfifo went fine\n");
else
printf("mkfifo went wrong\n");
pid_t fo = fork();
switch (fo) {
case -1:
printf("err\n");
case 0:
execlp("prova_fifo2", "prova_fifo2", NULL);
default:
execlp("prova_fifo", "prova_fifo", NULL);
}
return (0);
}
作家(又名 prova_fifo)
int main (void) {
int fd;
if (fd = open("try", O_WRONLY) == -1)
printf("Error opening FIFO\n");
char buf[6];
sprintf(buf, "hello");
int writer = write(fd, buf, strlen(buf));
printf("%d", writer);
return (0);
}
Reader(又名 prova_fifo2)
int main (void) {
int fd;
if (fd = open("try", O_RDONLY) ==-1)
printf("Errore in apertura FIFO\n");
char buf[6];
read(fd,&buf, strlen(buf)); //tried removing the "&", didn't work
printf("READ: %s", buf);
return (0);
}
注意 我包括所有必要的库,代码编译和运行。我唯一的问题是上面提到的那个。
这是一个 operator precedence 问题,表达式 fd=open("try", O_WRONLY )==-1
等同于 fd=(open("try", O_WRONLY )==-1)
这绝对不是您想要的,因为它将分配 open(...)==-1
的结果如果可以找到文件,fd
将为 false(即 0
,这是标准 input 的文件描述符),1
如果找不到文件。
你需要做 (fd=open("try", O_WRONLY ))==-1
.
您不能明智地在未初始化的 char
数组上调用 strlen()
。你应该这样做:
read(fd, buf, sizeof(buf));
相反。
请注意,read()
不会对您的数组进行零终止,并且 printf()
预计会收到 %s
这样的内容,因此您可能希望将整个内容更改为:
char buf[7] = {0};
if ( read(fd, buf, sizeof(buf) - 1) > 0 ) {
printf("READ: %s\n", buf);
}