无法定义共享内存对象的大小

问题描述 投票:1回答:1

您好,我正在尝试使用POSIX函数创建共享内存对象,但出现了奇怪的错误。

//创建共享内存

if( (shmid = shm_open("/OS",   O_CREAT ,0700)) == -1){
    printf("Error creating memory\n");
    exit(1);
}
printf("shmid: %d\n", shmid);

if (ftruncate(shmid, sizeof(int)) == -1){
    printf("Error defining size\n");
    exit(1);
}

您可以想象,它一直在打印“错误定义尺寸”。shmid输出的值为3,是有效值。但是ftruncate()函数由于错误而返回-1 ...设置为errno的值为22,正如我在互联网上看到的那样,这是由于“无效参数”引起的,但是我不明白为什么。 ?

c unix posix shared-memory
1个回答
0
投票

在Linux系统上,errno22值为EINVAL。除了显示数字值,您还应该使用perrorstrerror(errno)来获取诸如“无效参数”之类的文本错误消息。

使用

if( (shmid = shm_open("/OS", O_RDWR | O_CREAT ,0700)) == -1){

ftruncate列表中的https://pubs.opengroup.org/onlinepubs/7908799/xsh/ftruncate.html的文档

[EBADF] or [EINVAL] The fildes argument is not a file descriptor open for writing.

[EINVAL] The fildes argument references a file that was opened without write permission.

https://linux.die.net/man/2/ftruncate状态下的Linux手册页>

EBADF or EINVAL fd is not open for writing.

© www.soinside.com 2019 - 2024. All rights reserved.