由于您似乎打算对管道中的所有数据进行单次读取,因此我认为以下内容将比分隔符编码或其他答案中建议的miniheader技术更好地为您服务:
从管道(7)手册页:
If all file descriptors referring to
the write end of a pipe have been
closed, then an attempt to read(2)
from the pipe will see end-of-file
(read(2) will return 0).
以下示例取自pipe(2)联机帮助页并进行了反转,以便子进行写入,父进行读取(只是为了确定).我还添加了一个可变大小的缓冲区.孩子会睡5秒钟.延迟将确保子项的exit()与pipeio无关(父项将在子项退出之前打印完整的行).
#include
#include
#include
#include
#include
#include
char *
slurpfd(int fd)
{
const int bytes_at_a_time = 2;
char *read_buffer = NULL;
int buffer_size = 0;
int buffer_offset = 0;
int chars_io;
while (1) {
if (buffer_offset + bytes_at_a_time > buffer_size) {
buffer_size = bytes_at_a_time + buffer_size * 2;
read_buffer = realloc(read_buffer, buffer_size);
if (!read_buffer) {
perror("memory");
exit(EXIT_FAILURE);
}
}
chars_io = read(fd,
read_buffer + buffer_offset,
bytes_at_a_time);
if (chars_io <= 0) break;
buffer_offset += chars_io;
}
if (chars_io < 0) {
perror("read");
exit(EXIT_FAILURE);
}
return read_buffer; /* caller gets to free it */
}
int
main(int argc, char *argv[])
{
int pipefd[2];
pid_t cpid;
assert(argc == 2);
if (pipe(pipefd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
cpid = fork();
if (cpid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (cpid == 0) { /* Child writes argv[1] to pipe */
close(pipefd[0]); /* Close unused read end */
write(pipefd[1], argv[1], strlen(argv[1]) + 1);
close(pipefd[1]); /* Reader will see EOF */
/* sleep before exit to make sure that there
will be a delay after the parent prints it's
output */
sleep(5);
exit(EXIT_SUCCESS);
} else { /* Parent reads from pipe */
close(pipefd[1]); /* Close unused write end */
puts(slurpfd(pipefd[0]));
close(pipefd[0]);
wait(NULL); /* Wait for child */
_exit(EXIT_SUCCESS);
}
}
从您的评论中我现在看到,您可能希望在数据可用时读取数据,更新UI或其他任何内容,以反映系统的状态.为此,在非阻塞(O_NONBLOCK)模式下打开管道.重复读取任何可用的东西,直到-1返回并且errno == EAGAIN并进行解析.重复unil读取返回0,表示子项已关闭管道.
要为File *函数使用内存缓冲区,可以在GNU C库中使用fmemopen().