免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1310 | 回复: 1
打印 上一主题 下一主题

共享内存出错 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-02-17 12:17 |只看该作者 |倒序浏览
  1. #include <errno.h>
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <sys/shm.h>
  7. #include <sys/stat.h>
  8. #include <sys/wait.h>
  9. #include <stdlib.h>

  10. int main(int argc, char *argv[])
  11. {
  12.         int childpid;
  13.         int id;
  14.         int i;
  15.         int buf[10];
  16.         char *ptr;
  17.         int totalbytes = 0;

  18.         if((childpid = fork ())==-1)
  19.         {
  20.                 perror("fork");
  21.                 exit(EXIT_FAILURE);
  22.         }
  23.        
  24.         if(childpid==0)
  25.         {
  26.                 if ((id = shmget((key_t)12345,50*sizeof(char), IPC_CREAT)) == -1)
  27.                 {
  28.                         perror("Failed to create shared memory segment");
  29.                         exit(EXIT_FAILURE);
  30.                 }
  31.                 if ((ptr = (char *)shmat(id, NULL, 0)) == NULL)
  32.                 {
  33.                         if (shmctl(id, IPC_RMID, NULL) == -1)
  34.                                 perror("Failed to  remove memory segment");
  35.                         exit(EXIT_FAILURE);
  36.                 }
  37.                 for(i=0;argv[1][i]!='\0';i++)
  38.                 {
  39.                         *ptr=argv[1][i];
  40.                         ptr++;
  41.                 }
  42.                 printf("this is child.\nwrite argv[1] to shm.\nyou input charater count is %d\n",i);
  43.                 exit(EXIT_SUCCESS);
  44.         }
  45.        
  46.         else
  47.         {
  48.                 wait(NULL);
  49.                 if ((id = shmget((key_t)12345, 50*sizeof(char), IPC_CREAT)) == -1)
  50.                 {
  51.                         perror("Failed to create shared memory segment");
  52.                         exit(EXIT_FAILURE);
  53.                 }
  54.                 if ((ptr = (char *)shmat(id, NULL, 0)) == NULL)
  55.                 {
  56.                         perror("shmat");
  57.                         if (shmctl(id, IPC_RMID, NULL) == -1)
  58.                                  perror("Failed to  remove memory segment");
  59.                         exit(EXIT_FAILURE);
  60.                 }
  61.                 printf("this is parent.\ninput charater is %s\n",ptr);
  62.                
  63.                 if (shmctl(id, IPC_RMID, NULL) == -1)
  64.                 {
  65.                         perror("Failed to  remove memory segment");
  66.                         exit(EXIT_FAILURE);
  67.                 }
  68.                 exit(EXIT_SUCCESS);
  69.         }
  70. }
复制代码
以root身份可以很好地执行,但以其它身份就出错。不知为什么会这样?

论坛徽章:
0
2 [报告]
发表于 2010-02-17 12:28 |只看该作者
f:\Screenshot.jpg回复 1# whoisliang
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP