免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3395 | 回复: 1
打印 上一主题 下一主题

关于linux aio(异步IO)函数的使用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-01-14 14:31 |只看该作者 |倒序浏览
本帖最后由 rick_cheung 于 2011-01-14 14:36 编辑

我想使用Linux异步io 做一个写文件的测试,没有成功。请教各位原因何在。谢谢!
程序如下:
#include <stdio.h>
#include <aio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

struct aiocb my_aiocb;

void aio_completion_handler( sigval_t sigval);
void setup_io(int fd, char *buf)
{
        //struct aiocb my_aiocb;
        int ret;


        bzero( (char *)&my_aiocb, sizeof(struct aiocb) );
        
        my_aiocb.aio_fildes = fd;
        my_aiocb.aio_buf = buf;
        my_aiocb.aio_nbytes = 4;

        my_aiocb.aio_sigevent.sigev_notify = SIGEV_THREAD;
        my_aiocb.aio_sigevent.sigev_notify_function = aio_completion_handler;
        my_aiocb.aio_sigevent.sigev_notify_attributes = NULL;
        my_aiocb.aio_sigevent.sigev_value.sival_ptr = &my_aiocb;

        ret = aio_write( &my_aiocb );
        printf("send the aio request\n");
}

void aio_completion_handler( sigval_t sigval)
{
        struct aiocb *req;
        int ret;
        req = (struct aiocb *)sigval.sival_ptr;

        if( aio_error(req) == 0)
        {
                ret = aio_return(req);
        }
        printf("io done\n");
        return;
}

int main()
{
        int fd;
        char *buf;

        buf = "123";
        fd = open("/home/heihei.txt", O_CREAT | O_RDWR | O_APPEND , 77777);
        printf("fd = %d\n", fd);
        setup_io(fd, buf);
}

输出如下:
fd = 3
send the aio request
并且heihei.txt  已经生成。但是文件里面没有任何内容,说明没有写成功,原因何在呢?
wkj5945 该用户已被删除
2 [报告]
发表于 2012-01-18 11:21 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP