#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#define BUFSZ 2048
int main()
{
int shmid,i,fd,nwrite,nread;
char *shmadd;
char buf[5];
buf[5] = '\0';
if((shmid=shmget(IPC_PRIVATE,BUFSZ,0x666))<0)
{
perror("shmget");
exit(1);
}
else
printf("created shared-memory: %d\n",shmid);
if((shmadd=shmat(shmid,0,0))<(char *)0)
{
perror("shmat");
exit(1);
}
else
printf("attached shared-memory\n");
shmadd="Hello";
if((fd = open("share",O_CREAT | O_RDWR,0666))<0)
{
perror("open");
exit(1);
}
else
printf("open success!\n");
if((nwrite=write(fd,shmadd,5))<0)
{
perror("write");
exit(1);
}
else
printf("write success!\n");
lseek( fd, 0, SEEK_SET );
if((nread=read(fd,buf,5))<0)
{
perror("read");
exit(1);
}
else
printf("read %d form file:%s\n",nread,buf);
if(close(fd) == -1)
printf("close fd fails!\n");
else
printf("close fd succeeds!\n");
if((shmdt(shmadd))<0)
{
perror("shmdt");
exit(1);
}
else
printf("deleted shared-memory\n");
exit(0);
}
以上是在Linux中演示共享内存的代码。运行结果如下:
$ ./ex2
created shared-memory: 1572887
attached shared-memory
open success!
write success!
read 5 form file:Hello
close fd succeeds!
shmdt: Invalid argument
正如你所看到的,除了shmdt()之外,一切都很顺利。为什么失败?
的此外: 的
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#define BUFSZ 2048
int main()
{
int shmid,i,fd,nwrite,nread;
char *shmadd;
char buf[5];
buf[5] = '\0';
if((shmid=shmget(IPC_PRIVATE,BUFSZ,0x666)) < 0)
{
perror("shmget");
exit(1);
}
else
printf("created shared-memory: %d\n",shmid);
if((shmadd=shmat(shmid,0,0)) < (char *)0)
{
perror("shmat");
exit(1);
}
else
printf("attached shared-memory\n");
strcpy(shmadd, "Hello");
if((fd = open("share",O_CREAT | O_RDWR,0666)) < 0)
{
perror("open");
exit(1);
}
else
printf("open success!\n");
if((nwrite=write(fd,shmadd,5)) < 0)
{
perror("write");
exit(1);
}
else
printf("write success!\n");
lseek( fd, 0, SEEK_SET );
if((nread=read(fd,buf,5)) < 0)
{
perror("read");
exit(1);
}
else
printf("read %d form file:%s\n",nread,buf);
if(close(fd) == -1)
printf("close fd fails!\n");
else
printf("close fd succeeds!\n");
if((shmdt(shmadd))<0)
{
perror("shmdt");
exit(1);
}
else
printf("deleted shared-memory\n");
exit(0);
}
根据你的回答,我改变了上面的代码。但是现在我遇到了新的错误!
$ ./ex2
created shared-memory: 2129948
attached shared-memory
Segmentation fault (core dumped)
我似乎strcpy会导致错误。但为什么呢?
答案 0 :(得分:3)
这是问题所在:
shmadd="Hello";
这会将shmadd
指针更改为指向内存中的字符串。我想你打算将字符串复制到共享内存中。要做到这一点,你会这样做:
strcpy(shmadd,"Hello");
另请注意,检查错误是错误的,应该是:
if((shmadd=shmat(shmid,0,0)) == (void *)-1) { ... error ... }
你的权限应该是八进制,而不是十六进制:
if((shmid=shmget(IPC_PRIVATE,BUFSZ,0666)) < 0)
答案 1 :(得分:1)
您在此行中使用shmadd检查错误是不正确的:
if((shmadd=shmat(shmid,0,0)) < (char *)0)
您需要根据手册页对-1进行显式比较,因为对指针的-1强制转换为无符号值(32位系统上为0xFFFFFFFF)。用这一行替换该行:
if((shmadd=shmat(shmid,0,0)) == (char *)-1)
并且您将获得权限被拒绝错误,因为您不是root。