与 FIFO 通信的两个程序使用 for 循环但不使用 while 循环
Two programs communicating with FIFOs work with for loop but not with while loop
我正在尝试编写两个将通过 C 中的 FIFO 进行通信的程序。我正在尝试使用 FIFO 完成我的任务。
当我知道消息的数量并使用 for 循环读取它们时,它会打印出从另一端发送的所有消息。如果我使用 while 循环,它只会发送其中的两个。这个问题的代码略有变化 How to send a simple string between two programs using pipes?
这个有效:
/* writer */
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int fd;
char * myfifo = "/tmp/myfifo";
/* create the FIFO (named pipe) */
/* write "Hi" to the FIFO */
fd = open(myfifo, O_WRONLY);
int i;
for(i = 0; i < 10; i++)
write(fd, "Hi", sizeof("Hi"));
close(fd);
return 0;
}
并且:(已编辑)
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#define MAX_BUF 1024
int main()
{
int fd;
char * myfifo = "/tmp/myfifo";
char buf[MAX_BUF];
mkfifo(myfifo, 0666);
/* open, read, and display the message from the FIFO */
fd = open(myfifo, O_RDONLY);
int i;
for(i = 0; i < 10; i++)
{
int n = read(fd, buf, MAX_BUF);
printf("n = %d , Received: %s\n",n, buf);
}
close(fd);
/* remove the FIFO */
unlink(myfifo);
return 0;
}
编辑:现在打印
n = 18 , Received: Hi
n = 12 , Received: Hi
n = 0 , Received: Hi
n = 0 , Received: Hi
n = 0 , Received: Hi
n = 0 , Received: Hi
n = 0 , Received: Hi
n = 0 , Received: Hi
n = 0 , Received: Hi
n = 0 , Received: Hi
当我将 reader 更改为此时,它不起作用:
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#define MAX_BUF 1024
int main()
{
int fd;
char * myfifo = "/tmp/myfifo";
char buf[MAX_BUF];
mkfifo(myfifo, 0666);
/* open, read, and display the message from the FIFO */
fd = open(myfifo, O_RDONLY);
int i;
while(read(fd, buf, MAX_BUF))
printf("Received: %s\n", buf);
close(fd);
/* remove the FIFO */
unlink(myfifo);
return 0;
}
我运行在两个不同的终端和所有终端中安装这两个程序。
当我 运行 它们与第二个 reader 时,它只打印出:
Received: Hi
Received: Hi
如有任何帮助,我们将不胜感激。
在第二个版本中,循环的继续执行取决于read()
的返回值,而在第一个版本中,它是无条件循环十次。
并且由于它不清除缓冲区,所以只要第一次迭代读取 'Hi',所有后续迭代都会打印 'Hi',无论成功、部分成功或失败read()
.
管道是基于流的,而不是基于消息的。虽然 bytes 读取的数量应与写入的数量匹配,但 read
调用的数量不一定与 write
调用的数量相同。
如果我们修改 reader 以打印接收到的字节数:
int len;
while((len=read(fd, buf, MAX_BUF)) > 0) {
printf("Received %d: %s\n", len, buf);
}
我得到以下输出:
Received 30: Hi
所以在第二种情况下,有 10 次写入 3 个字节(2 次写入字母 H
和 i
,一次写入空终止字节)和 1 次读取 30 字节。每次 write
调用写入 3 个字节的原因是字符串常量 "Hi"
的类型为 char [3]
.
您只看到 "Hi" 的一个实例被打印,因为第三个字节是终止字符串的空字节,因此不会打印任何超过它的内容。
我正在尝试编写两个将通过 C 中的 FIFO 进行通信的程序。我正在尝试使用 FIFO 完成我的任务。
当我知道消息的数量并使用 for 循环读取它们时,它会打印出从另一端发送的所有消息。如果我使用 while 循环,它只会发送其中的两个。这个问题的代码略有变化 How to send a simple string between two programs using pipes?
这个有效:
/* writer */
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int fd;
char * myfifo = "/tmp/myfifo";
/* create the FIFO (named pipe) */
/* write "Hi" to the FIFO */
fd = open(myfifo, O_WRONLY);
int i;
for(i = 0; i < 10; i++)
write(fd, "Hi", sizeof("Hi"));
close(fd);
return 0;
}
并且:(已编辑)
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#define MAX_BUF 1024
int main()
{
int fd;
char * myfifo = "/tmp/myfifo";
char buf[MAX_BUF];
mkfifo(myfifo, 0666);
/* open, read, and display the message from the FIFO */
fd = open(myfifo, O_RDONLY);
int i;
for(i = 0; i < 10; i++)
{
int n = read(fd, buf, MAX_BUF);
printf("n = %d , Received: %s\n",n, buf);
}
close(fd);
/* remove the FIFO */
unlink(myfifo);
return 0;
}
编辑:现在打印
n = 18 , Received: Hi
n = 12 , Received: Hi
n = 0 , Received: Hi
n = 0 , Received: Hi
n = 0 , Received: Hi
n = 0 , Received: Hi
n = 0 , Received: Hi
n = 0 , Received: Hi
n = 0 , Received: Hi
n = 0 , Received: Hi
当我将 reader 更改为此时,它不起作用:
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#define MAX_BUF 1024
int main()
{
int fd;
char * myfifo = "/tmp/myfifo";
char buf[MAX_BUF];
mkfifo(myfifo, 0666);
/* open, read, and display the message from the FIFO */
fd = open(myfifo, O_RDONLY);
int i;
while(read(fd, buf, MAX_BUF))
printf("Received: %s\n", buf);
close(fd);
/* remove the FIFO */
unlink(myfifo);
return 0;
}
我运行在两个不同的终端和所有终端中安装这两个程序。 当我 运行 它们与第二个 reader 时,它只打印出:
Received: Hi
Received: Hi
如有任何帮助,我们将不胜感激。
在第二个版本中,循环的继续执行取决于read()
的返回值,而在第一个版本中,它是无条件循环十次。
并且由于它不清除缓冲区,所以只要第一次迭代读取 'Hi',所有后续迭代都会打印 'Hi',无论成功、部分成功或失败read()
.
管道是基于流的,而不是基于消息的。虽然 bytes 读取的数量应与写入的数量匹配,但 read
调用的数量不一定与 write
调用的数量相同。
如果我们修改 reader 以打印接收到的字节数:
int len;
while((len=read(fd, buf, MAX_BUF)) > 0) {
printf("Received %d: %s\n", len, buf);
}
我得到以下输出:
Received 30: Hi
所以在第二种情况下,有 10 次写入 3 个字节(2 次写入字母 H
和 i
,一次写入空终止字节)和 1 次读取 30 字节。每次 write
调用写入 3 个字节的原因是字符串常量 "Hi"
的类型为 char [3]
.
您只看到 "Hi" 的一个实例被打印,因为第三个字节是终止字符串的空字节,因此不会打印任何超过它的内容。