Search code examples
caio

aio_write() isn't working when I try to write a simple text


I am trying write a simple thing to a file it seems AIO doesn't working. What can be the problem ? I know there are extra headers which are unnecessary.

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>
#include<aio.h>

const int SIZE_TO_WRITE = 100;
char buffer[100];
struct aiocb cb;

int main()
{   

    int file = open("samp", O_CREAT|O_RDWR|O_TRUNC,0664);

    strcpy(buffer,"Sample");

    cb.aio_nbytes = SIZE_TO_WRITE;
    cb.aio_fildes = file;
    cb.aio_buf = buffer;    
    if(aio_write(&cb) == -1){
        printf("ERROR");
    }

    while(aio_error(&cb) == EINPROGRESS)    

    close(file);

    return 0;
}

Solution

  • while(aio_error(&cb) == EINPROGRESS)    
    close(file);
    

    is actually

    while(aio_error(&cb) == EINPROGRESS)    
        close(file);
    

    Did you mean to busy wait until the write completed instead?

    while(aio_error(&cb) == EINPROGRESS);
    //                                  ^
    close(file);