日期:2014-05-16  浏览次数:20766 次

关于共享内存的问题:是否只有超级用户可以读写?
我用普通用户运行程序,生成一段共享内存,但是这段共享内存却无法读与写,如果切换到root下面执行此程序,又是正常的,用ipcs查看,能看到此共享内存(登录用户创建,奇怪的是只有在root下面用ipcs才可以看到shmid)
/*
  g++   TestShm1.cpp     -o   TestShm1.out   -lrt

*/

#include   <iostream>
#include   <fstream>
#include   <sstream>    
//#include   <sstream>
#include   <string>
#include   <sys/time.h>
#include <sys/stat.h>


#include   <sys/ipc.h>
#include   <sys/shm.h>
#include   <sys/types.h>
#include   <unistd.h>

using   namespace   std;


typedef   struct{
                char   name[4];
                int   age;
}   people;
main(int   argc,   char**   argv)
{
int   shm_id,i;
key_t   key;
people   *p_map;
char*   name   =   "/usr/admin/c1 ";
key   =   ftok(name,0);
if(key   ==   -1)
perror( "ftok   error ");
shm_id   =   shmget(key,4096,IPC_CREAT);                
if(shm_id   ==   -1)
{
perror( "shmget   error ");
return   1;
}
cout   < <   "shm_id= " < <   shm_id   < <   endl;
p_map   =   (people*)shmat(shm_id,NULL,0);
for(i   =   0;i <10;i++)
{
printf(   "name:%s\n ",(*(p_map+i)).name   );
printf(   "age   %d\n ",(*(p_map+i)).age   );
}
if(shmdt(p_map)   ==   -1)
perror( "   detach   error   ");
return   0;
}




------解决方案--------------------
结吧:-)