linux管道发送长度,UNIX / Linux IPC:从管道读取.如何在运行时知道数据长度?

由于您似乎打算对管道中的所有数据进行单次读取,因此我认为以下内容将比分隔符编码或其他答案中建议的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().

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值