首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >aio_write()的简单示例

aio_write()的简单示例
EN

Stack Overflow用户
提问于 2020-02-11 04:19:43
回答 1查看 2K关注 0票数 2

我正在为POSIX aio_write函数寻找一个简单的示例。

到目前为止我尝试过的

下面的内容并不太重要。跳出来回答

下面的代码创建一个文件,但不向其写入任何内容。aio_error返回22 (= quota exceeded,但驱动器上有足够的空间和r/w权限)。

代码语言:javascript
复制
#include <aio.h>
#include <stdio.h>


char CONTENT[] = "asdf;";
const int LENGTH = 5;

struct aiocb createIoRequest(int fd,  
                            off_t offset, 
                            volatile void * content, 
                            size_t length){
    struct aiocb ret; // <-- not initialized. Will lead to an error ( see answer) 
    {
        ret.aio_fildes = fd;
        ret.aio_offset = offset;
        ret.aio_buf = content;
        ret.aio_nbytes = length;            
    }
    return ret;
}


int main(){

    FILE * file = fopen("outfile.txt","w");
    int fd = fileno(file);
    {    
        struct aiocb op  = createIoRequest(fd,0,CONTENT,LENGTH);        

        // schedule write
        // for valgrind mem leak output see comments from answer in
        //   https://stackoverflow.com/questions/4248720/aio-h-aio-read-and-write-memory-leak
        int ret = aio_write(&op);
        printf("aio_write 1: %d\n",ret);

        // wait until everything is done
        {
            const struct aiocb * aiolist[1];
            aiolist[0] = &op;

            int ret = aio_suspend(aiolist,1,NULL);
            printf("aio_suspend: %d\n",ret);

            // report possible errors
            {
                ret = aio_error(&op);
                printf("errno 1: %d\n",ret);
            }
        }
    }
    fclose(file);


    return 0;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-11 04:19:43

使用valgrind Conditional jump or move depends on uninitialised value(s)检查代码

代码有问题

未初始化aiocb。

示例

下面的示例不包含任何“成功”-checks(例如,file != NULL等)。这是为了可读性。在生产代码中,您应该检查返回值。

Example1

下面的程序将asdf;写成outfile.txt。aiocb结构在createIoRequest()内部初始化。

代码语言:javascript
复制
#include <aio.h>
#include <stdio.h>


char CONTENT[] = "asdf;";
const int LENGTH = 5;

struct aiocb createIoRequest(int fd,  
                            off_t offset, 
                            volatile void * content, 
                            size_t length){
    // create and initialize the aiocb structure.
    // If we don't init to 0, we have undefined behavior.
    // E.g. through sigevent op.aio_sigevent there could be 
    //      a callback function being set, that the program
    //      tries to call - which will then fail.
    struct aiocb ret = {0};
    {
        ret.aio_fildes = fd;
        ret.aio_offset = offset;
        ret.aio_buf = content;
        ret.aio_nbytes = length;            
    }
    return ret;
}


int main(){

    FILE * file = fopen("outfile.txt","w");
    int fd = fileno(file);
    {    
        struct aiocb op  = createIoRequest(fd,0,CONTENT,LENGTH);        

        // schedule write
        // for valgrind mem leak output see comments from answer in
        //   https://stackoverflow.com/questions/4248720/aio-h-aio-read-and-write-memory-leak
        int ret = aio_write(&op);
        printf("aio_write 1: %d\n",ret);

        // wait until everything is done
        {
            const struct aiocb * aiolist[1];
            aiolist[0] = &op;

            int ret = aio_suspend(aiolist,1,NULL);
            printf("aio_suspend: %d\n",ret);

            // report possible errors
            {
                ret = aio_error(&op);
                printf("errno 1: %d\n",ret);
            }
        }
    }
    fclose(file);


    return 0;
}

Example2

下面的程序简单地将上面的程序扩展为对文件进行两次异步写入。

代码语言:javascript
复制
#include <aio.h>
#include <stdio.h>


char CONTENT[] = "asdf;";
const int LENGTH = 5;

struct aiocb createIoRequest(int fd,  
                            off_t offset, 
                            volatile void * content, 
                            size_t length){
    // create and initialize the aiocb structure.
    // If we don't init to 0, we have undefined behavior.
    // E.g. through sigevent op.aio_sigevent there could be 
    //      a callback function being set, that the program
    //      tries to call - which will then fail.
    struct aiocb ret = {0};
    {
        ret.aio_fildes = fd;
        ret.aio_offset = offset;
        ret.aio_buf = content;
        ret.aio_nbytes = length;            
    }
    return ret;
}


int main(){

    FILE * file = fopen("outfile.txt","w");
    int fd = fileno(file);
    {    
        struct aiocb op  = createIoRequest(fd,0,CONTENT,LENGTH);
        struct aiocb op2 = createIoRequest(fd,LENGTH,CONTENT,LENGTH);

        // schedule write
        // for valgrind mem leak output see comments from answer in
        //   https://stackoverflow.com/questions/4248720/aio-h-aio-read-and-write-memory-leak
        int ret = aio_write(&op);
        printf("aio_write 1: %d\n",ret);
        ret = aio_write(&op2);
        printf("aio_write 2: %d\n",ret);        

        // wait until everything is done
        {
            const int OPs = 2;
            const struct aiocb * aiolist[OPs];
            aiolist[0] = &op;
            aiolist[1] = &op2;

            int ret = aio_suspend(aiolist,OPs,NULL);
            printf("aio_suspend: %d\n",ret);

            // report possible errors
            {
                ret = aio_error(&op);
                printf("errno 1: %d\n",ret);
                ret = aio_error(&op2);
                printf("errno 2: %d\n",ret);
                // error codes can be checked in <errno.h>
            }
        }
    }
    fclose(file);


    return 0;
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60161967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档