免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1786 | 回复: 1

[通信方式] 进程之间的互斥访问!! [复制链接]

论坛徽章:
1
辰龙
日期:2013-11-28 20:23:14
发表于 2014-04-14 21:17 |显示全部楼层
现有两个进程,它们之间要同时编程某一个文件,如何做到正确的进程之间的互斥呢?
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <error.h>
  4. #include <pthread.h>
  5. #include <sys/mman.h>
  6. #include <fcntl.h>
  7. #include <string.h>

  8. int main(int argc,char* argv[])
  9. {
  10.         int fd;
  11.         char dir[]="/simple.shm";
  12.         pthread_mutex_t* lock;
  13.         pthread_mutexattr_t mutexAttr;

  14.         fd = shm_open(dir,O_RDWR,0777);

  15.         /*not create yet*/
  16.         if(fd == -1)
  17.         {
  18.                 printf("Shared memory open<%s> failed!\n",dir);
  19.                 close(fd);
  20.                 fd = shm_open(dir,O_CREAT|O_TRUNC|O_RDWR,0777);
  21.                 if( -1 == fd )
  22.                     {
  23.                         printf("Shared memory create failed\n");
  24.                         return -1;
  25.                     }
  26.                     if(ftruncate(fd,sizeof(pthread_mutex_t)))
  27.                     {
  28.                         printf("Shared ftruncate failed\n");
  29.                         return -1;
  30.                     }
  31.                 lock = mmap(NULL,sizeof(pthread_mutex_t),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
  32.                 if(lock == MAP_FAILED)
  33.                 {
  34.                         printf("Shared MAP_FAILED\n");
  35.                 }
  36.                 close(fd);
  37.         }
  38.         else
  39.         {       
  40.                 lock = mmap(NULL,sizeof(pthread_mutex_t),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
  41.                 close(fd);
  42.         }

  43.         printf("Shared memory open<%s> sucess in process 1!\n",dir);

  44.         pthread_mutexattr_init(&mutexAttr);
  45.         pthread_mutexattr_setpshared(&mutexAttr,PTHREAD_PROCESS_SHARED);
  46.         pthread_mutex_init(lock,&mutexAttr);
  47.              /**以上是设置共享内存的pthread_mutex_t**/

  48.              /*sys.log在当前目录初始不存在*/
  49.         int syslog_fd;
  50.         int i=0;
  51.         while(i<5)
  52.         {
  53.                 pthread_mutex_lock(lock);
  54.                 /*create sys.log*/
  55.                 syslog_fd = open("sys.log",O_CREAT|O_EXCL,0777);/*没有则创建*/
  56.                 if(-1 == syslog_fd)
  57.                 {
  58.                         printf("You never see this!\n");
  59.                         pthread_mutex_unlock(lock);
  60.                         return -1;
  61.                 }
  62.                 printf("open one time in process 1!.\n");
  63.                 sleep(2);   /*我发现如果再这个点 ,启另外一个同样的进程B,会导致B打印You never see this! 我想在这个点锁应该是lock的啊 B进程为什么会进入open那里呢?*/

  64.                 remove("sys.log"); /*然后删除*/
  65.                 pthread_mutex_unlock(lock);
  66.                 sleep(2); /*如果在这个点 启同样的进程B,是没有问题的?*/
  67.                 i++;
  68.         }
  69.         return 0;
  70. }
复制代码
那么进程之间的互斥,找lock和unlock之间 sleep怎么会是这种情况呢?求赐教?

论坛徽章:
1
辰龙
日期:2013-11-28 20:23:14
发表于 2014-04-14 21:24 |显示全部楼层
i know  ...
lock和mutexAttr初始化了多次...
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP