linux内核在哪里将数据写入管道 [英] Where does the linux kernel keep the data written to a pipe

查看:164
本文介绍了linux内核在哪里将数据写入管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设以下简单代码:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

main()
{
    int fd[2];                
    pipe(fd);
 // ...               
    write(fd, buf, VERY_BIG_NUMBER);
 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
}

现在我的问题:

  1. 有人可以解释写入将数据放在何处吗?
  2. 是否将其写入用户空间或内核空间中的某些缓冲区?
  3. 一个人可以写到管道的最大长度是多少?
  4. 由于管道写操作阻塞了操作,内核为了提高效率就不会做任何事情并等待读取器然后将数据直接传递给它吗?

推荐答案

有一个管道缓冲区,在Linux上通常为64KB.写出管道缓冲区.填满后,write()系统调用将阻塞,直到管道的另一端耗尽缓冲区为止.

There is a pipe buffer, typically 64KB on Linux. Writes up the pipe buffer. Once it's full the write() syscall blocks until the other end of the pipe drains the buffer.

摘录自 pipe(7)手册页:

管道的容量有限.如果管道已满,则取决于是否设置了O_NONBLOCK标志,write(2)将阻塞或失败(请参见下文).不同的实现对管道容量有不同的限制.应用程序不应依赖于特定的容量:应设计应用程序,以使读取过程在数据可用时立即消耗数据,以免写入过程受到阻塞.

A pipe has a limited capacity. If the pipe is full, then a write(2) will block or fail, depending on whether the O_NONBLOCK flag is set (see below). Different implementations have different limits for the pipe capacity. Applications should not rely on a particular capacity: an application should be designed so that a reading process consumes data as soon as it is available, so that a writing process does not remain blocked.

在2.6.11之前的Linux版本中,管道的容量与系统页面大小相同(例如,在i386上为4096字节).从Linux 2.6.11开始,管道容量为65536字节.

In Linux versions before 2.6.11, the capacity of a pipe was the same as the system page size (e.g., 4096 bytes on i386). Since Linux 2.6.11, the pipe capacity is 65536 bytes.

缓冲区在内核空间中.管道是通过虚拟pipefs文件系统实现的.该文件系统的内核代码为每个pipe2()系统调用分配16个4KB页面,并且该缓冲区空间与为管道创建的inode关联. read()write()系统调用将数据复制到用户空间中或从用户空间中复制出去. ()

The buffer is in kernel space. Pipes are implemented via a virtual pipefs filesystem. The kernel code for this filesystem allocates 16 4KB pages for each pipe2() system call, and that buffer space is associated with the inode created for the pipe. read() and write() syscalls copy data into and out of userspace. (Source)

如果写入管道的大小小于4KB,则这些写入是原子的.超过4KB的写入可能会导致页面错误,因此不再是原子的.这意味着,如果写入大于4KB,则可以交错来自不同进程的多个写入.

Writes to a pipe are atomic if they're under 4KB. A write over 4KB could incur page faults and therefore is no longer atomic. That means that multiple writes from separate processes could be interleaved if the writes are larger than 4KB.

另请参见: 管道有多大缓冲?

这篇关于linux内核在哪里将数据写入管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆