Chinaunix首页 | 论坛 | 博客
  • 博客访问: 94336
  • 博文数量: 24
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 171
  • 用 户 组: 普通用户
  • 注册时间: 2014-10-24 12:12
个人简介

好好学习天天向上

文章分类

全部博文(24)

文章存档

2018年(1)

2016年(7)

2015年(6)

2014年(10)

我的朋友

分类: LINUX

2014-11-17 14:02:51

以下代码段是在nvram读写时用的lock和unlock函数及实现,只要在get和set nvram之前和之后加上_nvram_lock()和_nvram_unlock(),就能实现当前进程读写nvram时不被其他进程打断。
参考的博文:http://blog.csdn.net/xiajun07061225/article/details/8475738
参考文件:Linux信号量学习.pdf

  1. #include <sys/ipc.h>
  2. #include <sys/sem.h>//包含信号量定义的头文件
  3. #define NVRAM_SEM_KEY 0XAEEAEE

  4. //联合类型semun定义
  5. union semun{
  6.     int val;
  7.     struct semid_ds *buf;
  8.     unsigned short *array;
  9. };
  10. static int sem_id;//信号量ID


  11. //函数:设置信号量的值
  12. static int set_semvalue(void)
  13. {
  14.     union semun sem_union;
  15.     sem_union.val = 1;
  16.   
  17.     if(semctl(sem_id,0,SETVAL,sem_union))
  18.         return 0;
  19.   
  20.     return 1;
  21. }
  22.   
  23. //函数:删除信号量
  24. static void del_semvalue(void)
  25. {
  26.     union semun sem_union;
  27.   
  28.     if(semctl(sem_id,0,IPC_RMID,sem_union))
  29.         printf("NVRAMERRO:Failed to delete semaphore\n");
  30. }
  31.   
  32. //函数:信号量P操作:对信号量进行减一操作
  33. static int semaphore_p(void)
  34. {
  35.     struct sembuf sem_b;
  36.   
  37.     sem_b.sem_num = 0;//信号量编号
  38.     sem_b.sem_op = -1;//P操作
  39.     sem_b.sem_flg = SEM_UNDO;
  40.   
  41.     if(semop(sem_id,&sem_b,1) == -1)
  42.     {
  43.         printf("NVRAMERRO:semaphore_p failed\n");
  44.         return 0;
  45.     }
  46.   
  47.     return 1;
  48. }
  49.   
  50. //函数:信号量V操作:对信号量进行加一操作
  51. static int semaphore_v(void)
  52. {
  53.     struct sembuf sem_b;
  54.   
  55.     sem_b.sem_num = 0;//信号量编号
  56.     sem_b.sem_op = 1;//V操作
  57.     sem_b.sem_flg = SEM_UNDO;
  58.   
  59.     if(semop(sem_id,&sem_b,1) == -1)
  60.     {
  61.         printf("NVRAMERRO:semaphore_v failed\n");
  62.         return 0;
  63.     }
  64.   
  65.     return 1;
  66.   
  67. }


  68. static int _lock()
  69. {
  70.     //取得一个已有信号量的键
  71.     sem_id = semget((key_t)NVRAM_SEM_KEY,0,0666 );
  72.     if((sem_id<0)&&(errno==ENOENT))
  73.     {
  74.         printf("NVRAM:MUST creat semaphore\n");
  75.         //创建一个新的信号量或者是取得一个已有信号量的键
  76.         sem_id = semget((key_t)NVRAM_SEM_KEY,1,0666 | IPC_CREAT);
  77.         if(sem_id < 0)
  78.         {
  79.             printf("NVRAMERRO:failed to creat semaphore\n");
  80.             return 0;
  81.         }
  82.         if(!set_semvalue())
  83.         {
  84.             printf("NVRAMERRO:failed to initialize semaphore\n");
  85.             return 0;
  86.         }
  87.     }
  88.     else if(sem_id<0)
  89.     {
  90.         printf("NVRAMERRO:failed to get semaphore\n");
  91.         return 0;
  92.     }
  93.     if(!semaphore_p())
  94.     {
  95.         printf("NVRAMERRO:failed to lock\n");
  96.         return 0;
  97.     }
  98.     
  99.     return 1;
  100. }

  101. static int _unlock()
  102. {
  103.     if(!semaphore_v())
  104.     {
  105.         printf("NVRAMERRO:failed to unlock\n");
  106.         return 0;
  107.     }
  108.     return 1;
  109. }


  110. static int _nvram_lock()
  111. {
  112.     int i=0;

  113.     while (i++ < MAX_LOCK_WAIT) {        
  114.         if(_lock())
  115.             return 1;
  116.         else            
  117.             usleep(500000);    
  118.     }
  119.     return 0;
  120. }
  121. /*nvram file unlock*/
  122. static int _nvram_unlock()
  123. {
  124.     int i=0;

  125.     while (i++ < MAX_LOCK_WAIT) {        
  126.         if(_unlock())
  127.             return 1;
  128.         else            
  129.             usleep(500000);    
  130.     }
  131.     return 0;
  132. }

阅读(1593) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~