Appearance
Linux poll 监听管道与 Unix 域套接字
示例
c
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
// 子进程用数据报送读数,用管道报告状态。
static void report(int data_fd, int status_fd)
{
const char reading = 42;
const char done = 'D';
const int success = send(data_fd, &reading, 1U, 0) == 1 &&
write(status_fd, &done, 1U) == 1;
close(data_fd);
close(status_fd);
_exit(success ? EXIT_SUCCESS : EXIT_FAILURE);
}
static int collect(int data_fd, int status_fd)
{
struct pollfd inputs[] = {{.fd = data_fd, .events = POLLIN},
{.fd = status_fd, .events = POLLIN}};
unsigned remaining = 2;
while (remaining != 0U)
{
// 1. 一个等待同时覆盖管道和 Unix 域套接字,等待失败就报告。
const int result = poll(inputs, 2, 1000);
if (result < 0) { perror("poll"); return EXIT_FAILURE; }
if (result == 0) { fputs("poll timeout\n", stderr); return EXIT_FAILURE; }
for (unsigned i = 0; i < 2U; ++i)
{
// 2. 即使同时到达 hangup,仍先消费已排队的数据。
if (inputs[i].revents & POLLIN)
{
unsigned char byte;
if (read(inputs[i].fd, &byte, 1U) != 1)
{
fputs("incomplete IPC message\n", stderr);
return EXIT_FAILURE;
}
if (i == 0U) { printf("reading=%u\n", (unsigned)byte); }
else { printf("status=%c\n", byte); }
inputs[i].fd = -1;
--remaining;
}
else if (inputs[i].revents & (POLLERR | POLLHUP | POLLNVAL))
{
fputs("IPC endpoint closed early\n", stderr);
return EXIT_FAILURE;
}
}
}
return EXIT_SUCCESS;
}
int main(void)
{
// 1. 先创建两个通道,随后 fork。两进程关闭不再使用的端点。
int data[2], state[2];
if (socketpair(AF_UNIX, SOCK_DGRAM, 0, data) < 0)
{
perror("socketpair");
return EXIT_FAILURE;
}
if (pipe(state) < 0)
{
perror("pipe"); close(data[0]); close(data[1]);
return EXIT_FAILURE;
}
const pid_t child = fork();
if (child < 0)
{
perror("fork");
close(data[0]); close(data[1]); close(state[0]); close(state[1]);
return EXIT_FAILURE;
}
if (child == 0)
{
close(data[0]); close(state[0]);
report(data[1], state[1]);
}
// 2. 父进程同时监听两类输入,最后回收子进程并检查退出状态。
close(data[1]); close(state[1]);
const int result = collect(data[0], state[0]);
close(data[0]); close(state[0]);
int status;
pid_t waited;
do { waited = waitpid(child, &status, 0); } while (waited < 0 && errno == EINTR);
if (waited < 0) { perror("waitpid"); return EXIT_FAILURE; }
return WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS ? result : EXIT_FAILURE;
}执行和验证
父进程通过同一次 poll() 等子进程的读数和状态。读数走 Unix 域数据报,状态走管道。只使用本地进程间通道。
sh
mkdir -p .work
cc -std=c11 -Wall -Wextra -Werror examples/linux/poll-ipc/main.c -o .work/poll-ipc
./.work/poll-ipc预期输出包含 reading=42 和 status=D,输出先后取决于事件何时到达。读完后移除对应的描述符;父进程最后用 waitpid() 接收子进程的退出状态。
预测题 写端发送一个字节后立即关闭,父进程为什么仍有机会读到该字节?
改动题 让子进程提前退出,只发送读数,观察父进程状态。
核对改法
读取端可能同时看到 POLLIN 和 POLLHUP,此时优先消费已经排队的数据。缺少状态时,管道挂断且无可读数据,父进程报错退出。
验证记录
已在 Alpine 3.22.8 的 WSL 环境编译并运行,核对两个事件及子进程正常退出。