Home System Tutorial LINUX Artifact in Linux: Principles and Applications of eventfd

Artifact in Linux: Principles and Applications of eventfd

Feb 13, 2024 pm 08:30 PM
Skill Order overflow

Linux is a powerful operating system that provides many efficient inter-process communication mechanisms, such as pipes, signals, message queues, shared memory, etc. But is there a simpler, more flexible, and more efficient way to communicate? The answer is yes, that is eventfd. eventfd is a system call introduced in Linux version 2.6. It can be used to implement event notification, that is, to deliver events through a file descriptor. eventfd contains a 64-bit unsigned integer counter maintained by the kernel. The process can read/change the counter value by reading/writing this file descriptor to achieve inter-process communication. What are the advantages of eventfd? It has the following characteristics:

Artifact in Linux: Principles and Applications of eventfd

  • eventfd does not need to create any additional files or memory space, it only needs a file descriptor;
  • eventfd can be used in conjunction with multiplexing mechanisms such as select, poll, and epoll to achieve efficient event-driven programming;
  • eventfd can be set to non-blocking or semaphore mode, providing different communication semantics;
  • eventfd can cross process or thread boundaries to achieve different levels of communication.

So, how does eventfd work? What application scenarios does it have? This article will introduce the artifact eventfd from two aspects: principle and application.

Generally speaking: There are five major solutions for Linux inter-process communication: pipes, message queues, semaphores, shared memory, and sockets.
I am not very familiar with pipes. I only know about the limitations of general pipes and the relationship between parent and child processes. I ruled it out at first because what I want to do is independent inter-process communication. Named pipes do not seem to be limited to parent and child processes, but in the kernel state. Not sure how to use it.
I don't understand Message Queue at all.
The core of the semaphore is an atomic operation of kernel variables, but the interface is only reflected in the user mode, and the PV operation of the semaphore seems to be mutually exclusive, rather than the notification wake-up mechanism I want.
Shared memory is even more troublesome. The interface is only in user mode. If you want to share memory between kernel mode and user mode, you have to write the file yourself and then provide the mmap interface.
Sockets have only been used with af_inet's tcp/udp and af_unix's dgram before. The problem is still the same. The kernel does not provide a clear interface. Although you can call it yourself using functions such as sock->ops->recvmsg, but After all, you need to construct the input parameters yourself, which still feels unsafe.

The only thing left seems to be netlink. This socket clearly provides the kernel's packet sending function, because it clearly exports the netlink_kernel_create function, so the kernel mode function can use this sock to send packets. But one is that the user mode needs to register a packet receiving function, and the other is that the kernel mode still needs to assemble skb to send packets. It is still too complicated for me who simply just want to wake up by notification.

So I searched again and found the artifact eventfd. Between the communication between KVM and Qemu, eventfd was used superbly by Daniel. After carefully analyzing the source code, I found that this thing is just as the name says, purely for exists for notification.
As a file (is there anything in Linux that is not a file~~), its private_data structure eventfd_ctx has only four pitiful variables.

struct eventfd_ctx {
  struct kref kref;  /* 这个就不多说了,file计数用的,用于get/put */
  wait_queue_head_t wqh; /* 这个用来存放用户态的进程wait项,有了它通知机制才成为可能 */
/*
\* Every time that a write(2) is performed on an eventfd, the
\* value of the __u64 being written is added to "count" and a
\* wakeup is performed on "wqh". A read(2) will return the "count"
\* value to userspace, and will reset "count" to zero. The kernel
\* side eventfd_signal() also, adds to the "count" counter and
\* issue a wakeup.
*/
  __u64 count;  /* 这个就是一个技术器,应用程序可以自己看着办,read就是取出然后清空,write就是把value加上 */
  unsigned int flags;  /* 所有的file都有的吧,用来存放阻塞/非阻塞标识或是O_CLOEXEC之类的东西 */
};
  我之所以选用它是因为它有 eventfd_signal 这个特地为内核态提供的接口,下面的是注释。
 \* This function is supposed to be called by the kernel in paths that do not
 \* allow sleeping. In this function we allow the counter to reach the ULLONG_MAX
 \* value, and we signal this as overflow condition by returining a POLLERR to poll(2).
Copy after login

In fact, it will be clearer if you look at the code

int eventfd_signal(struct eventfd_ctx *ctx, int n)
{
  unsigned long flags;

  if (n return -EINVAL;
  spin_lock_irqsave(&ctx->wqh.lock, flags);
  if (ULLONG_MAX - ctx->count count);
  ctx->count += n;
  if (waitqueue_active(&ctx->wqh))
    wake_up_locked_poll(&ctx->wqh, POLLIN);
  spin_unlock_irqrestore(&ctx->wqh.lock, flags);

  return n;
}  
Copy after login

The essence is to wake up once, without reading or writing. The difference from eventfd_write is that no blocking is required

Let me talk about my specific usage:
The kernel state is a module that registers a misc device and creates a kernel thread to work (the parameter is the module's file->private_data). Provide an ioctl interface for the user mode process to deliver the fd created by its own eventfd, and save it in file->private_data that can be accessed by the kernel thread.
When the kernel state wants to notify the user state, eventfd_signal is used directly. At this time, the user state thread needs to first place itself on eventfd_ctx->wqh. There are two solutions, one is to call read, and the other is to call poll. If it is a read, eventfd_ctx->count will be cleared later, and it can be blocked next time. However, if poll is used, the count is not cleared afterwards, causing poll to return immediately even if there is no eventfd_signal in the kernel state when polling again.
It is a little more troublesome to notify the kernel state from the user state. First, you need to create an eventfd and then send it to file->private_data (the operation here is the same as above). In addition, you need to make an iotcl in the module, which is responsible for the user state to notify the kernel state. , eventfd_signal is done in the function. The kernel state thread needs to be placed on eventfd_ctx->wqh first. You can use vfs_read, or do a poll in the kernel state yourself (seems to be troublesome again).

This article introduces eventfd, an artifact in Linux. It is a simple, flexible and efficient inter-process communication mechanism. We analyzed the creation, reading and writing, and flag bits of eventfd from the principle aspect, and gave corresponding code examples. We also introduced the use of eventfd in scenarios such as user mode and kernel mode communication, timers and event triggers from the application perspective, and gave corresponding code examples. Through the study of this article, we can master the basic usage of eventfd, and can flexibly use eventfd in actual development to achieve different communication needs. Hope this article is helpful to you!

The above is the detailed content of Artifact in Linux: Principles and Applications of eventfd. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The price of Bitcoin since its birth 2009-2025 The most complete summary of BTC historical prices The price of Bitcoin since its birth 2009-2025 The most complete summary of BTC historical prices Jan 15, 2025 pm 08:11 PM

Since its inception in 2009, Bitcoin has become a leader in the cryptocurrency world and its price has experienced huge fluctuations. To provide a comprehensive historical overview, this article compiles Bitcoin price data from 2009 to 2025, covering major market events, changes in market sentiment, and important factors influencing price movements.

Overview of the historical price of Bitcoin since its birth. Complete collection of historical price trends of Bitcoin. Overview of the historical price of Bitcoin since its birth. Complete collection of historical price trends of Bitcoin. Jan 15, 2025 pm 08:14 PM

Bitcoin, as a cryptocurrency, has experienced significant market volatility since its inception. This article will provide an overview of the historical price of Bitcoin since its birth to help readers understand its price trends and key moments. By analyzing Bitcoin's historical price data, we can understand the market's assessment of its value, factors affecting its fluctuations, and provide a basis for future investment decisions.

A list of historical prices since the birth of Bitcoin BTC historical price trend chart (Latest summary) A list of historical prices since the birth of Bitcoin BTC historical price trend chart (Latest summary) Feb 11, 2025 pm 11:36 PM

Since its creation in 2009, Bitcoin’s price has experienced several major fluctuations, rising to $69,044.77 in November 2021 and falling to $3,191.22 in December 2018. As of December 2024, the latest price has exceeded $100,204.

The latest price of Bitcoin in 2018-2024 USD The latest price of Bitcoin in 2018-2024 USD Feb 15, 2025 pm 07:12 PM

Real-time Bitcoin USD Price Factors that affect Bitcoin price Indicators for predicting future Bitcoin prices Here are some key information about the price of Bitcoin in 2018-2024:

How to customize the resize symbol through CSS and make it uniform with the background color? How to customize the resize symbol through CSS and make it uniform with the background color? Apr 05, 2025 pm 02:30 PM

The method of customizing resize symbols in CSS is unified with background colors. In daily development, we often encounter situations where we need to customize user interface details, such as adjusting...

Is H5 page production a front-end development? Is H5 page production a front-end development? Apr 05, 2025 pm 11:42 PM

Yes, H5 page production is an important implementation method for front-end development, involving core technologies such as HTML, CSS and JavaScript. Developers build dynamic and powerful H5 pages by cleverly combining these technologies, such as using the <canvas> tag to draw graphics or using JavaScript to control interaction behavior.

The text under Flex layout is omitted but the container is opened? How to solve it? The text under Flex layout is omitted but the container is opened? How to solve it? Apr 05, 2025 pm 11:00 PM

The problem of container opening due to excessive omission of text under Flex layout and solutions are used...

Why are the inline-block elements misaligned? How to solve this problem? Why are the inline-block elements misaligned? How to solve this problem? Apr 04, 2025 pm 10:39 PM

Regarding the reasons and solutions for misaligned display of inline-block elements. When writing web page layout, we often encounter some seemingly strange display problems. Compare...

See all articles