时间:2011-01-06 15:46:28

标签: linux sockets kernel nonblocking aio

1 个答案:

答案 0 :(得分:3)

首先,O_NONBLOCK和AIO不混合。当相应的readwrite没有被阻止时,AIO将报告异步操作完成 - 而O_NONBLOCK,他们永远不会阻止,因此{ {1}}请求将始终立即完成(aio提供aio_return())。

其次,不要对两个同时发生的未完成的aio请求使用相同的缓冲区。在发出aio请求和EWOULDBLOCK告诉你它已完成的时间之间,缓冲区应被视为完全禁止。

第三,AIO对同一文件描述符的请求排队,以便给出明智的结果。这意味着在读取完成之前不会发生写入 - 如果您需要先写入数据,则需要以相反的顺序发出AIO。以下工作正常,没有设置aio_error()

O_NONBLOCK

或者,如果您不关心相互排序的读取和写入,则可以使用struct aiocb my_aiocb1, my_aiocb2; char pBuffer1[BUFSIZE+1], pBuffer2[BUFSIZE+1] = "Some test message"; const struct aiocb *cblist[2] = { &my_aiocb1, &my_aiocb2 }; // Start the read and write operations on the same socket IssueWriteOperation(&my_aiocb2, pBuffer2); IssueReadOperation(&my_aiocb1, pBuffer1); // Wait for I/O completion on both operations int nRound = 1; int aio_status1, aio_status2; do { printf("\naio_suspend round #%d:\n", nRound++); ret = aio_suspend( cblist, 2, NULL ); assert (ret == 0); // Check the error status for the read and write operations aio_status1 = aio_error(&my_aiocb1); if (aio_status1 == EINPROGRESS) puts("aio1 still in progress."); else puts("aio1 completed."); aio_status2 = aio_error(&my_aiocb2); if (aio_status2 == EINPROGRESS) puts("aio2 still in progress."); else puts("aio2 completed."); } while (aio_status1 == EINPROGRESS || aio_status2 == EINPROGRESS); // Get the return code for the read ssize_t retcode; retcode = aio_return(&my_aiocb1); printf("First operation results: aio_error=%d, aio_return=%d\n", aio_status1, retcode); retcode = aio_return(&my_aiocb1); printf("Second operation results: aio_error=%d, aio_return=%d\n", aio_status1, retcode); 为套接字创建两个文件描述符,并使用一个用于读取,另一个用于写入 - 每个人的AIO操作都会单独排队。