1

我想创建一个填充 0 或其他字母的文件。这是我的功能

int fill(const int d, struct aiocb *aiorp, void *buf, const int count){
        int rv = 0;
        memset( (void *)aiorp, 0, sizeof( struct aiocb ) ); // <-here second paramether is 0
        aiorp->aio_fildes = d;
        aiorp->aio_buf = buf;
        aiorp->aio_nbytes = count;
        aiorp->aio_offset = 0;
        rv = aio_write( aiorp );
        return rv;
}

这是我的主要内容

int main(int argc, char * argv[]){
        int des;
        int rv;
        struct aiocb aior;
        char buffer[1000];
        if(argc == 3){
                printf("just %s\n", argv[1]);
                des = createFile(argv[1]);
                rv = fill(des, &aior, buffer, sizeof(buffer));
        }
        return 0;
}

所以我的输出应该是用零值填充的文件,但我的文件充满了垃圾

^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@$
y▒^X^@^@^@^@^@^@^@
s|▒^@^@^@^@^@^@^@^@^@^@^@^@▒r|▒^@^@^@^@▒▒^?▒(▒▒▒▒▒{▒▒
y▒^P^@^@^@▒
y▒^A^@^@^@d^Cy▒^@^@^@^@▒
y▒^T
y▒^P▒▒▒^@^@^@^@^@^@^@^
...

为什么?怎么了?

这是代码:

sukurti - 如果该文件不存在则创建新文件并填充 - 填充创建的文件

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

int sukurti(char *name);
int fill(const int d, struct aiocb *aiorp, void *buf, const int count);

int sukurti(char *name){
   int dskr;
   dskr = open( name, O_RDONLY );
   if( dskr == -1 ){
          printf("Failas sukurtas, nes jo nebuvo\n");
          dskr = open( name, O_WRONLY | O_CREAT, 0644);

   }else{
           printf("Jau yra toks failas!\n");
           exit(1);
   }
   return dskr;
}

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

}

int main(int argc, char * argv[]){
        int des;
        int rv;
        struct aiocb aior;
        int x = atoi(argv[2]);
        printf("%d\n", x);
        int size = MB * MB * x;
        char buffer[size];
        if(argc == 3){
                printf("just %s\n", argv[1]);
                des = sukurti(argv[1]);
                rv = fill(des, &aior, buffer, sizeof(buffer));
        }else{
                printf("Blogas\n");
        }
        return 0;
}

编辑:我知道我写到文件结束了

4

2 回答 2

5

这里有三个问题:

  1. buffer未初始化。这样做使用

    memset(buffer, 
      <what ever 8bit value you want the file to be filled with>, 
      sizeof buffer);
    

    main().

  2. aior初始化错误。将其初始化为所有0使用

    memset(aiorb, 0, sizeof aior);
    

    在定义它main()并删除对 in 的调用memset()之后fill()

  3. 最后,程序很可能在缓冲区异步写入磁盘之前结束。

    要解决此问题,请定义一个通知方法,如下所述man 7 aio。并让程序在结束程序之前等待收到此通知。

    例如,这可以通过通过信号请求完成通知并等待该信号来完成。

    为此,请按如下方式修改您的代码:

    • 将以下两行添加aiorp到 in 指向的内容的初始化中fill()

      aiorp->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
      aiorp->aio_sigevent.sigev_signo = SIGUSR1;
      
    • 为了能够处理发送的通知信号(不结束程序),需要设置信号处理程序:

      void handler(int sig)
      {
        /* Do nothing. */
      }
      
    • 通过调用安装此处理程序

      signal(handler, SIGUSR1);
      

      就在程序的开头。

    • return调用main()之前wait_for_completion(SIGUSR1)可能看起来像这样:

      void wait_for_completion(int sig)
      {
        sigset_t set;
        sigemptyset(&set);
        sigaddset(&set, sig);
      
        sigwait(&set, &sig); /* This blocks until sig had 
                                been received by the program. */
        printf("Completion notification for asynchronous 'write'-operation received.\n");
      }  
      

    根据需要添加错误处理。为了可读性,我把它省略了。

于 2015-04-28T16:28:03.200 回答
4
  memset( (void *)aiorp, '0', sizeof( struct aiocb ) );

0不是'0',您需要实际使用 ASCII 值(如果我没记错的话是 48。)

于 2015-04-28T14:23:42.817 回答