我使用管道通過傳遞文件描述符來讀取函數的輸出。要異步讀取輸出,我在ASYNC NON BLOCKING模式下設置管道的「讀」端,並使用SIGACTION配置一個IO處理程序。我的代碼如下所示:如何確保異步IO管道上的所有輸出在關閉之前完成
struct sigaction io_act;
struct sigaction io_act_old;
sigset_t block_mask;
/* create the pipe */
pipe (pipe_fd);
/* set the reading end of pipe in ASYNC mode */
fcntl (pipe_fd[0], F_SETOWN, getpid());
fcntl (pipe_fd[0], F_SETFL, O_ASYNC | O_NONBLOCK);
/* add an I/O handler for SIGIO */
sigemptyset (&block_mask);
io_act.sa_handler = sigio_handler;
io_act.sa_mask = block_mask;
io_act.sa_flags = SA_RESTART;
sigaction (SIGIO, &io_act, &io_act_old);
/* executing the function that generate output in given FILEDES */
my_generate_output (pipe_fd[1]);
close(pipe_fd[1]);
/* this sleep should not be needed (subject of question) */
sleep(1);
close (pipe_fd[0]);
/* restore previous signal handler */
sigaction (SIGIO, &io_act_old, NULL);
的問題是,如果我忽略了睡眠函數調用我可以關閉該管道,並完成了輸出前禁用SIGIO處理程序。其結果是應用程序由於未處理的SIGIO信號而中止(消息「可能的I/O」)。
在禁用SIGIO處理程序之前,如何確保所有輸出都已從管道中讀取?
輸出到管? –
我對此並不完全確定,但是你不能'F_SETOWN'爲0以防止信號? – Hasturkun
aio_suspend()不會給你你想要的東西嗎? –