4382|0

241

帖子

4

TA的资源

纯净的硅(初级)

楼主
 

Helper2416-44——Linux_Programing——POSIX线程同步篇1 [复制链接]

本帖最后由 yuanlai2010 于 2014-9-12 08:55 编辑

                        
POSIX线程同步篇1
用信号量进行同步

参与Helper2416开发板助学计划心得


什么是信号量

        在前面的帖子中有提到SystemV IPC机制的信号量,不过那主要时用于进程间的同步的,而现在用于线程的信号量是取自POSIX的实时扩展。

        信号量是一个特殊类型的变量,它可以被增加或减少,但对其的关键访问被保证时原子操作,即使在一个多线程程序中也时如此。这意味着如果一个程序中有两个或更多的线程试图改变一个信号量的值,系统将保证所有的操作都将依次进行。

        原子操作是指,如果两个线程企图同时给一个信号量加1,他们之间不会相互干扰,而不像如果两个程序同时对同一个文件进行读取、增加、写入操作时可能会引起冲突。

        信号量一般用来保护一段代码,使其每次只能被一个执行线程运行,要完成这个工作,可以使用二进制信号量——它只有01两种取值。有时候还需要使用到通用信号量,它具有更大的取值范围。两者实际调用的函数都是一样的,根据需求给定参数就OK了。

POSIX信号量函数
信号量函数的名字都以sem_开头,线程中使用的基本信号量函数有如下4个。

#include

intsem_init(sem_t *sem, int pshared, unsigned int value);
intsem_wait(sem_t *sem);
intsem_post(sem_t *sem);
intsem_destroy(sem_t * sem);

sem_init
函数原型:intsem_init(sem_t *sem, int pshared, unsigned int value);
函数功能:初始化信号量。
函数返回:调用成功返回0
参数说明:
第一个参数sem_t*sem,指定信号量对象。
第二个参数intpshared,控制信号量的类型(共享选项),如果其值为0,就表示这个信号量是当前进程的局部信号量,否则,这个信号量就可以在多个进程之间共享。
第三个参数unsignedint value,给定信号量一个出世的整数值。

sem_wait
函数原型:intsem_wait(sem_t *sem);
函数功能:以原子操作的方式将信号量的值减1,但它会等待直到信号量有个非零值才会开始减法操作。
函数返回:调用成功返回0
参数说明:sem_t*sem指向由sem_init初始化的信号量。

sem_post
函数原型:intsem_post(sem_t*sem);
函数功能:以原子操作的方式将信号量的值加1
函数返回:调用成功返回0
参数说明:sem_t*sem指向由sem_init初始化的信号量。

sem_destroy
函数原型:intsem_destroy(sem_t*sem);
函数功能:用完信号量后对它进行清理。
函数返回:调用成功返回0
参数说明:sem_t*sem指向由sem_init初始化的信号量。该函数清理该信号量拥有的所有资源,如果企图清理的信号量正被一些线程等待,就会收到一个错误。
        
代码实践
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <semaphore.h>

  6. void *pthread1(void *arg);
  7. void *pthread2(void *arg);

  8. sem_t thread_sem;


  9. int main()
  10. {
  11.         pthread_t thread_id1,thread_id2;
  12.         void *thread_result1, *thread_result2;
  13.         int res;
  14.        
  15.         res = sem_init(&thread_sem,0,1);
  16.         if(res != 0){
  17.                 perror("semaphore initialization failed ...\n");
  18.                 exit(EXIT_FAILURE);
  19.         }
  20.        
  21.         res = pthread_create(&thread_id1,NULL,pthread1,NULL);
  22.         if(res != 0){
  23.                 perror("pthread1 is not created ...\n");
  24.                 exit(EXIT_FAILURE);
  25.         }

  26.         res = pthread_create(&thread_id2,NULL,pthread2,NULL);
  27.         if(res != 0){
  28.                 perror("pthread2 is not created ...\n");
  29.                 exit(EXIT_FAILURE);
  30.         }

  31.         res = pthread_join(thread_id1,&thread_result1);
  32.         if(res != 0){
  33.                 perror("pthread1 join failed ...\n");
  34.                 exit(EXIT_FAILURE);
  35.         }

  36.         res = pthread_join(thread_id2,&thread_result2);
  37.         if(res != 0){
  38.                 perror("pthread2 join failed ...\n");
  39.                 exit(EXIT_FAILURE);
  40.         }
  41.        
  42.         printf("the pthread1 return %s\n",(char *)thread_result1);
  43.         printf("the pthread2 return %s\n",(char *)thread_result2);
  44.         sem_destroy(&thread_sem);
  45.         return 0;
  46. }


  47. void *pthread1(void *arg)
  48. {
  49.         int i;
  50.         for(i=0;i<10;i++){
  51.                 sem_wait(&thread_sem);
  52.                 printf("hello this is pthread1 --> %d\n",i);
  53.                 sem_post(&thread_sem);
  54.                 sleep(2);
  55.         }
  56.         pthread_exit("pthread1 finished ...\n");       
  57. }

  58. void *pthread2(void *arg)
  59. {
  60.         int i;
  61.         for(i=0;i<10;i++){
  62.                 sem_wait(&thread_sem);
  63.                 printf("hello this is pthread2 --> %d\n",i);
  64.                 sem_post(&thread_sem);
  65.                 sleep(2);       
  66.         }
  67.         pthread_exit("pthread2 finished ...\n");
  68. }
复制代码

运行结果
  1. [yuanlai@fedora pthread]$ gcc -D_REENTRANT pthread1.c -o pthread1 -lpthread
  2. [yuanlai@fedora pthread]$ ./pthread1
  3. hello this is pthread1 --> 0
  4. hello this is pthread2 --> 0
  5. hello this is pthread1 --> 1
  6. hello this is pthread2 --> 1
  7. hello this is pthread1 --> 2
  8. hello this is pthread2 --> 2
  9. hello this is pthread1 --> 3
  10. hello this is pthread2 --> 3
  11. hello this is pthread1 --> 4
  12. hello this is pthread2 --> 4
  13. hello this is pthread1 --> 5
  14. hello this is pthread2 --> 5
  15. hello this is pthread1 --> 6
  16. hello this is pthread2 --> 6
  17. hello this is pthread1 --> 7
  18. hello this is pthread2 --> 7
  19. hello this is pthread1 --> 8
  20. hello this is pthread2 --> 8
  21. hello this is pthread1 --> 9
  22. hello this is pthread2 --> 9
  23. the pthread1 return pthread1 finished ...

  24. the pthread2 return pthread2 finished ...

  25. [yuanlai@fedora pthread]$
复制代码


从运行结果来看,是按照同时运行的规律来打印出信息的,通过信号量来确保同一时刻只有一个线程在打印信息,但是再回头看上一篇的实践代码的时候,发现,即使没用信号量,打印出来的结果还是正常的,就算不使用sleep调用也是正常的,结果显示,虽然我的笔记本是双核四线程的CPU,但是thread1和thread2还是和运行在单核单线程处理器上一样。还得继续去探究这里面的奥秘。

论坛IDyuanlai2010
发表时间:2014-09-12

点赞 关注

回复
举报
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

 
机器人开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表