| 阻塞 | 非阻塞 | |
|---|---|---|
| 同步 | 同步阻塞 I/O (預設 read/write) | 同步非阻塞 I/O (O_NONBLOCK) |
| 非同步 | 非同步阻塞 I/O (較少見) | 非同步非阻塞 I/O (AIO, fasync/SIGIO) |

(建議下載原圖放大檢視)
/* include/linux/fs.h */
struct fasync_struct {
int magic;
int fa_fd; // 檔案描述符
struct fasync_struct *fa_next; // 鏈表下一節點
struct file *fa_file; // 對應的 file 結構
struct rcu_head fa_rcu;
};
每個 struct file 持有一個 struct fasync_struct *f_fasync 指標,指向該檔案所有註冊非同步通知的行程鏈表。
void sigio_handler(int signo) {
// 資料可讀/可寫,執行非阻塞 read/write
}
signal(SIGIO, sigio_handler);
fcntl(fd, F_SETOWN, getpid()); // 或設定負值表示進程組
int flags = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, flags | FASYNC);
此呼叫會觸發驅動程式的 fasync() file_operation,將當前行程加入 f_fasync 鏈表。
static struct fasync_struct *my_fasync_queue = NULL;
static int my_fasync(int fd, struct file *filp, int on) {
return fasync_helper(fd, filp, on, &my_fasync_queue);
}
fasync_helper() 核心輔助函數自動處理鏈表增刪。
// 在中斷處理常式、poll_wakeup、資料到達等路徑呼叫
kill_fasync(&my_fasync_queue, SIGIO, POLL_IN); // 可讀
// 或 POLL_OUT (可寫), POLL_ERR, POLL_PRI 等
/* 字元裝置驅動範例 */
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/poll.h>
#define DEV_NAME "fasync_demo"
static struct fasync_struct *async_queue;
static char kernel_buf[1024];
static int buf_len;
static int demo_open(struct inode *inode, struct file *filp) { return 0; }
static int demo_release(struct inode *inode, struct file *filp) {
fasync_helper(-1, filp, 0, &async_queue); // 移除註冊
return 0;
}
static ssize_t demo_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos) {
if (buf_len == 0) return 0;
count = min(count, (size_t)buf_len);
if (copy_to_user(buf, kernel_buf, count)) return -EFAULT;
memmove(kernel_buf, kernel_buf + count, buf_len - count);
buf_len -= count;
return count;
}
static ssize_t demo_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos) {
count = min(count, sizeof(kernel_buf) - buf_len);
if (copy_from_user(kernel_buf + buf_len, buf, count)) return -EFAULT;
buf_len += count;
kill_fasync(&async_queue, SIGIO, POLL_IN); // 通知應用層可讀
return count;
}
static int demo_fasync(int fd, struct file *filp, int on) {
return fasync_helper(fd, filp, on, &async_queue);
}
static struct file_operations fops = {
.owner = THIS_MODULE,
.read = demo_read,
.write = demo_write,
.fasync = demo_fasync,
.open = demo_open,
.release = demo_release,
};
/* 模組初始化/註冊省略 */
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#define DEV "/dev/fasync_demo"
volatile sig_atomic_t data_ready = 0;
void sigio_handler(int sig) { data_ready = 1; }
int main() {
int fd = open(DEV, O_RDWR);
if (fd < 0) { perror("open"); exit(1); }
signal(SIGIO, sigio_handler);
fcntl(fd, F_SETOWN, getpid());
int flags = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, flags | FASYNC);
printf("Waiting for data (press Ctrl+C to exit)...\n");
while (1) {
if (data_ready) {
data_ready = 0;
char buf[128];
int n = read(fd, buf, sizeof(buf));
if (n > 0) {
buf[n] = '\0';
printf("Received: %s\n", buf);
}
}
pause(); // 睡眠等待訊號
}
close(fd);
return 0;
}
ControlPath),後續連線複用該 socket。SIGIO,非阻塞喚醒機制。F_SETOWN 決定)。EAGAIN,否則會遺漏後續資料。