2

我希望 aio 在读取操作完成时向我的程序发出信号,并且根据此页面,可以通过内核发送的信号或通过启动运行用户函数的线程来接收此类通知。可以通过设置正确的值来选择任一行为sigev_notify

我试了一下,很快发现即使设置为通过信号接收通知,也会创建另一个线程。

(gdb) info threads
  Id   Target Id         Frame 
  2    Thread 0x7ffff7ff9700 (LWP 6347) "xnotify" 0x00007ffff7147e50 in gettimeofday () from /lib64/libc.so.6
* 1    Thread 0x7ffff7fc3720 (LWP 6344) "xnotify" 0x0000000000401834 in update (this=0x7fffffffdc00)

该文档还指出:这些功能的实现可以使用内核中的支持(如果可用)或使用基于用户级线程的实现来完成。我想根本没有线程,这可能吗?

我检查了我的内核,看起来没问题:

qdii@localhost /home/qdii $ grep -i aio /usr/src/linux/.config
CONFIG_AIO=y

是否可以在没有任何(用户态)线程的情况下运行 aio(当然,除了主线程)?

编辑:我深入研究了它。librt 似乎提供了 aio 函数的集合:查看 glibc 源代码暴露了一些可疑的东西:在 /rt/aio_read.c 内部是一个函数存根:

int aio_read (struct aiocb *aiocbp)
{
  __set_errno (ENOSYS);
  return -1;
}

stub_warning (aio_read)

我在子目录 sysdeps/pthread 中找到了第一个相关的实现,它直接调用__aio_enqueue_request(..., LIO_READ)了 ,进而创建了 pthread。但是当我想知道为什么在这种情况下会有一个 stub 时,我想也许 stub 可以由 linux 内核本身实现,并且 pthread 实现将是某种后备代码。

通过我的 / usr aio_read/src/linux 目录搜索会得到很多结果,我现在正试图理解这些结果。

4

1 回答 1

1

I found out that there are actually two really different aio libraries: one is part of glibc, included in librt, and performs asynchronous access by using pthreads. The other aio library implements the same interface as the first one, but is built upon the linux kernel itself and can use signals to run asynchronously.

于 2012-02-29T14:13:52.793 回答