0

My file should be filled with 0. I want to do that using aio_write As a result my file should look like 000000000.... but as a result I get that my file is filled with garbage

^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
s|▒^@^@^@^@^@^@^@^@^@^@^@^@▒r|▒^@^@^@^@▒▒^?▒(▒▒▒▒s|▒▒
y▒^P^@^@^@^X▒▒▒
s|▒^X^@^@^@▒.....

I don't even imagine what's wrong. First of all I'm using asynchronous write so I need to wait when da_aio_write is completed

int da_test_wait( struct aiocb *aiorp ){
   const struct aiocb *aioptr[1];
   int rv;
   aioptr[0] = aiorp;
   rv = aio_suspend( aioptr, 1, NULL );
   if( rv != 0 ){
      perror( "aio_suspend failed" );
      abort();
   }
   rv = aio_return( aiorp );
   printf( "AIO complete, %d bytes write.\n", rv );
   return 1;
}

Also here is my writing function

 int da_aio_write(const int d, struct aiocb *aiorp, void *buf, const int count){
   int rv = 0;
   memset( (void *)aiorp, 0, sizeof( struct aiocb ) );
   aiorp->aio_fildes = d;
   aiorp->aio_buf = buf;
   aiorp->aio_nbytes = count;
   aiorp->aio_offset = 0;

   rv = aio_write( aiorp );

   if( rv == -1) {
       perror("Error da_aio_write\n");
       exit(1);
       return rv;
   }
   return rv;
}

and my main

int main(int argc, char *argv[] ){
        int sk;
        int d;
        struct aiocb aior;
        if(argc == 3){
                sk = atoi(argv[2]);
                char buffer[MB * MB * sk];
                //memset(&aior, 0, sizeof( struct aiocb ));
                d = da_open(argv[1]);
                da_aio_write( d, &aior, buffer, sizeof(buffer) );
                da_test_wait( &aior );
                da_close( d );
        }
        return 0;
}

Any ideas what I'm doing wrong? EDIT: I know that my da_aio write ends after main because ehen I compile I get

File created
 dskr1 = 3 
AIO complete, 1048576 bytes write. 
closed

EDIT My full updated code

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

#define MB 1024

int da_open(const char *name);
int da_aio_write(const int d, struct aiocb *aiorp, void *buf, const int count);
int da_test_wait( struct aiocb *aiorp );
int da_close(int fd);

int da_open(const char *name){
   int dskr;
   int dskr2;
   dskr = open( name, O_RDWR );
   if( dskr == -1 ){
       printf("File created\n");
       dskr2 = open( name, O_WRONLY | O_CREAT, 0644);
   }else{
       printf("End job!\n");
       exit(1);
   }
   printf( "dskr1 = %d\n", dskr2 );
   return dskr2;
}

int da_aio_write(const int d, struct aiocb *aiorp, void *buf, const int count){
   int rv = 0;
   memset( (void *)aiorp, 0, sizeof( struct aiocb ) );
   aiorp->aio_fildes = d;
   aiorp->aio_buf = buf;
   aiorp->aio_nbytes = count;
   aiorp->aio_offset = 0;

   rv = aio_write( aiorp );

   if( rv == -1) {
       perror("Error da_aio_write\n");
       exit(1);
       return rv;
   }
   return rv;
}

int da_test_wait( struct aiocb *aiorp ){
   const struct aiocb *aioptr[1];
   int rv;
   aioptr[0] = aiorp;
   rv = aio_suspend( aioptr, 1, NULL );
   if( rv != 0 ){
      perror( "aio_suspend failed" );
      abort();
   }
   rv = aio_return( aiorp );
   printf( "AIO complete, %d bytes write.\n", rv );
   return 1;
}

int da_close(int fd){
   int rv;
   rv = close( fd );
   if( rv != 0 ) perror ( "close() failed" );
   else puts( "closed" );
   return rv;
}

int main(int argc, char *argv[] ){
        int sk;
        int d;
        struct aiocb aior;
        if(argc == 3){
                sk = atoi(argv[2]);
                char buffer[MB * MB * sk];
                int size; 
                size = MB * MB * sk;
                memset( buffer, '\0', size);
                //memset(&aior, '\0', sizeof( struct aiocb ));
                d = da_open(argv[1]);
                da_aio_write( d, &aior, buffer, sizeof(buffer) );
                da_test_wait( &aior );
                da_close( d );
        }
        return 0;
}
David
  • 3,055
  • 4
  • 30
  • 73

1 Answers1

3
char buffer[MB * MB * sk];

Stack variables are not automatically initialiased. So your buffer contains garbage. memset it to 0 first if that's what you want to write to file.

kaylum
  • 13,833
  • 2
  • 22
  • 31
  • Thanks but then I get this `^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^` Here how I did it `memset( buffer, '\0', size);` – David May 06 '15 at 11:24
  • Please update your question with your new code. Also include `da_open`. – kaylum May 06 '15 at 11:41
  • 2
    That is the expected output. Because writing all 0's to a file creates a binary file not a text file. If you want to see the 0's open the file with an editor that can view in hexl mode (like emacs). Or if you have `hexdump` use that to view the file instead of `cat` and friends. – kaylum May 06 '15 at 11:59
  • Thanks! But if I want to create a file filled with 'A' characters? Why I get invalid arguments error in my `da_aio_write` function? Even when I try to fill with 1 instead of 0 I get invalid argument. I know that memset second argument is `int` so with 1 memset should work. – David May 06 '15 at 13:20
  • Please give the exact code you changed and what the exact error message is. To set to all 'A' you change the memset to be `memset( buffer, 'A', size)`. There's no reason why aio_write would give an error for that. It doesn't care what is inside the buffer. – kaylum May 06 '15 at 20:36