gpt4 book ai didi

c - aio_write 和 memset 参数无效和段错误(核心已转储)

转载 作者:太空宇宙 更新时间:2023-11-04 03:39:07 24 4
gpt4 key购买 nike

我正在尝试用“a”字符填充我的文件,我需要使用 aio_write。这是我的写作功能

int da_aio_write(const int d, struct aiocb *aiorp, void *buf, const int count){
int rv = 0;
// memset( (void *)aiorp, 'a', sizeof( struct aiocb ) ); // <--- I get Invalid argument (Error da_aio_write)

// memset(&aiorp, 'a', sizeof( struct aiocb )); // <--- I get Segmentation Fault (core dumped)
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;
}

当我使用 memset( (void *)aiorp, 0, sizeof( struct aiocb ) );//<--- 我得到无效参数(错误 da_aio_write ) 所以我的 rv == -1 并且打印了 perror当我使用 memset(&aiorp, 'a', sizeof( struct aiocb ));//<-- 我得到 Segmentation Fault (core dumped)为什么我的两个 memset 都不起作用?我还添加了我的完整代码

#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, 'a', sizeof( struct aiocb ) ); // <--- I get Invalid argument (Error da_aio_write)
//memset(&aiorp, 'a', sizeof( struct aiocb )); / <--- I get Segmentation Fault (core dumped)
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;
}

最佳答案

如果你想将 aiorp 的每个字节设置为 'a' 的 ASCII 值,你的第一个变量:

memset((void *) aiorp, 'a', sizeof(struct aiocb));

很好。 (不过,您不需要转换为 void *,并且可以将大小重写为 sizeof(*aiorp) 以遵循通用模式。)

但是你为什么要这样做呢?那是控件,结构,它现在包含许多由 'a' 字节组成的(无意义的)数据,除了您之后明确覆盖的值。

难怪你会得到一个EINVAL,其中“一个或多个aio_offsetaio_reqprioaio_nbytes无效”,根据联机帮助页:aiorp->aio_erqprio0x61616161

您想将数据缓冲区设置为包含'a':

memset(buf, 'a', count);

(但考虑到您的aiocb 结构分配在main 的本地存储中,因此未初始化,您可能会考虑memset 将其设置为所有上述调用为零。)

关于c - aio_write 和 memset 参数无效和段错误(核心已转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30078682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com