2015-05-04 35 views
1

我需要用相同的数字填充我的文件,例如00000....我想用异步aio_write函数。但在这里我得到什么用垃圾填充的C文件

^@ w▒(▒▒▒▒▒l▒@^@Y▒^@^@^@^@▒▒▒▒u▒l▒@*`▒^@ w▒h▒▒▒ ...... 

这里是我的代码

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; 
} 

,这里是我的异步写入功能(我认为这个功能是坏的)

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; 
} 

而且,这里是我等待asyncronius功能以结束

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; 
} 

另外这里是我的全部程序

#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]; 
       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; 
} 
+0

为什么调用'read'函数来执行'写入'操作?建议命名该函数:da_aio_write() – user3629249

回答

1
#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> 

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

int da_open(const char *name){ 
    int dskr; 
    int dskr2; 
    dskr = open(name, O_RDWR); 
    if(dskr == -1){ 
     dskr2 = open(name, O_WRONLY | O_CREAT, 0644); 
    }else{ 
     exit(1); 
    } 
    return dskr2; 
} 

这到底是怎么回事?这是一个混淆的方式,以确保您不写入文件,如果它已经存在?这显然是不正确的,因为该文件可能在第二次打开之前出现。您可以改用O_EXCL。

该代码由于未提前处理退出而造成混淆。

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

首先,什么是fsck。你为什么打电话写作函数something_ 阅读

rv有什么用?

如果您阅读了关于aio的任何文档,您将看到您必须明确地等待事件完成。

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

首先,我只想创建文件,如果它不存在。如果文件存在,我希望我的程序打印错误。其次,我认为所有的麻烦都是使用'da_aio_write'函数(我尝试用aio_suspend等待事件完成) – David