在我的代码中,我open使用了一个 FIFO(使用创建mkfifo),然后我继续使用 aQSocketNotifier来接收传入数据的通知,并在它到达时读取它。
// create the FIFO
if(!mkfifo(SERIAL_FIFO, 0600)) {
// nonblocking open (even open itself would block until a first write)
in_fifo = ::open(SERIAL_FIFO, O_RDONLY | O_NONBLOCK);
if(in_fifo >= 0) {
// create notifier
in_fifo_notifier = new QSocketNotifier(in_fifo, QSocketNotifier::Read, this);
connect(&*in_fifo_notifier, &QSocketNotifier::activated,
this, [this](QSocketDescriptor /*socket*/, QSocketNotifier::Type /*type*/){
// copy all the available data
char buf[4096];
for(;;) {
ssize_t rl = ::read(in_fifo, buf, sizeof(buf));
if(rl <= 0) break;
::write(out_fd, buf, rl);
}
});
}
问题在于,每当有人在管道的另一端写入数据时,即使每次我读取所有数据时,信号也会不断被激活(相关的 CPU 使用率为 100%)。问题出在哪里?