我有一个函数,它启动一个aio_read任务并返回到主程序。
我希望定期检查任务是否已经完成,以关闭文件描述符,并可能通知用户。
我的当前方法是声明一个struct,其中包含任务的struct aiocb和文件描述符。将其添加到全局数组中,并检查是否有任何任务使用以下内容(aio_check函数):
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <aio.h>
#include <unistd.h>
#include "aioqueue.h"
void aio_add(struct aiocb a, int fd) {
for (int i = 0; i < MAX; i++) {
if (aio_array[i].valid == 0) {
aio_pack tmp;
tmp.cb = a;
tmp.fd = fd;
tmp.valid = 1;
aio_array[i] = tmp;
printf("request enqueued\n");
return;
}
}
printf("This shell cannot keep track of so many IO operations :/ \n");
}
void aio_check() {
for (int i = 0; i < MAX; i++) {
// wait until the request has finished
if(aio_array[i].valid) {
if (aio_error(&aio_array[i].cb) != EINPROGRESS) {
int nleidos = aio_return(&aio_array[i].cb);
if (nleidos != -1)
printf("AIO Task finished: %d B\n", nleidos);
else
printf("Error!");
close(aio_array[i].fd);
aio_array[i].valid = 0;
} else {
printf("At least one AIO task is in progress\n");
}
}
}
}以及aioQuee.h的代码
#define MAX 10
typedef struct {
struct aiocb cb;
int fd;
int valid;
} aio_pack;
aio_pack aio_array[MAX];
void aio_add(struct aiocb a, int fd);
void aio_check();问题是,如果我在添加任务后调用aio_check,则始终会得到
至少有一个AIO任务正在进行中。
消息。即使很明显任务已经完成了。
我想,这可能是因为我在struct aiocb是EINPROGRESS的时刻传递一个aio_error的副本,而作为一个副本,它永远不会被更新。但此刻我很迷茫。任何帮助都将不胜感激。
提前谢谢你。
发布于 2018-12-05 10:33:16
您需要调用aio_suspend()来处理I/O已经完成的操作,否则aio_error()等将不会以不同的方式返回。见service.ipp#L290。
https://stackoverflow.com/questions/53510586
复制相似问题