千家信息网

unix 互斥锁与信号量应用例子

发表于:2025-02-08 作者:千家信息网编辑
千家信息网最后更新 2025年02月08日,#include#include #include #include #include #include #define SIZE 3#define STR_SIZE 20pthread_mutex_
千家信息网最后更新 2025年02月08日unix 互斥锁与信号量应用例子

  1. #include<pthread.h>
  2. #include<semaphore.h>
  3. #include<stdlib.h>
  4. #include<stdio.h>
  5. #include<string.h>
  6. #include<unistd.h>
  7. #define SIZE 3
  8. #define STR_SIZE 20
  9. pthread_mutex_t mutex; //全局变量对main 及 线程函数都可见
  10. sem_t read_sem, write_sem; //全局变量对main 及 线程函数都可见
  11. struct _buf_
  12. {
  13. char* buf[SIZE];
  14. int front, rear;
  15. }buffer; //循环队列结构体
  16. void thread(void)
  17. {
  18. while(1)
  19. {
  20. if(0 != sem_wait(&read_sem))
  21. {
  22. perror("sem_wait_0");
  23. exit(1);
  24. }
  25. if(0 != pthread_mutex_lock(&mutex))
  26. {
  27. perror("pthread_mutex_lock_0");
  28. exit(1);
  29. }
  30. fputs("output characters:\n",stdout);
  31. fputs(buffer.buf[buffer.front],stdout);
  32. buffer.front = (buffer.front + 1) % SIZE;
  33. if(0 != pthread_mutex_unlock(&mutex))
  34. {
  35. perror("pthread_mutex_lock_0");
  36. exit(1);
  37. }
  38. if(0 != sem_post(&write_sem))
  39. {
  40. perror("sem_wait_0");
  41. exit(1);
  42. }
  43. sleep(10);
  44. }
  45. }
  46. int main(void)
  47. {
  48. //初始化循环队列结构体
  49. int i;
  50. bufferbuffer.front = buffer.rear = 0;
  51. for(i=0; i < SIZE; i++)
  52. {
  53. buffer.buf[i] =(char*) malloc(STR_SIZE);
  54. }
  55. //初始化 互斥锁
  56. //存在警告 pthread_mutexattr_t mutex_attr = PTHREAD_MUTEX_INITIALIZER;
  57. if(0 != pthread_mutex_init(&mutex, NULL))
  58. {
  59. perror("pthread_mutex_init\n");
  60. exit(1);
  61. }
  62. //初始化信号量
  63. if(0 != sem_init(&read_sem, 0, 0))//linux进程间的信号共享还未能实现,参数PSHARE为0,而且还没有进行写操作没有数据可读,将参数vaule设置为0,进行阻塞
  64. {
  65. perror("sem_init_0\n");
  66. exit(1);
  67. }
  68. if(0 != sem_init(&write_sem, 0, 2))
  69. {
  70. perror("sem_init_1\n");
  71. exit(1);
  72. }
  73. //创建两个子线程
  74. pthread_t thd0,thd1;
  75. if(0 != pthread_create(&thd0,NULL,(void*)thread,NULL))
  76. {
  77. perror("pthread_create_0\n");
  78. exit(1);
  79. }
  80. if(0 != pthread_create(&thd1,NULL,(void*)thread,NULL))
  81. {
  82. perror("pthread_create_1\n");
  83. exit(1);
  84. }
  85. //进行写入字符操作
  86. while(1)
  87. {
  88. //写信号量阻塞
  89. if(0 != sem_wait(&write_sem))
  90. {
  91. perror("sem_wait_1");
  92. exit(1);
  93. }
  94. //上互斥锁所进行写入操作
  95. if(0 != pthread_mutex_lock(&mutex))
  96. {
  97. perror("pthread_mutex_lock_1");
  98. exit(1);
  99. }
  100. fputs("pls enter 20 character:\n",stdout);
  101. //读标准输入文件
  102. if(NULL == fgets(buffer.buf[buffer.rear],STR_SIZE,stdin))
  103. {
  104. perror("fgets\n");
  105. exit(1);
  106. }
  107. if(0 == strncmp("end\n",buffer.buf[buffer.rear],3))
  108. {
  109. printf("manual exit successfully\n");
  110. exit(1);
  111. }
  112. buffer.rear = (buffer.rear+1)% SIZE;
  113. //解互斥锁
  114. if(0 != pthread_mutex_unlock(&mutex))
  115. {
  116. perror("pthread_mutex_lock_1");
  117. exit(1);
  118. }
  119. if(0 != sem_post(&read_sem))
  120. {
  121. perror("sem_wait_1");
  122. exit(1);
  123. }
  124. sleep(1);
  125. }
  126. return 0;
  127. }
附件:http://down.51cto.com/data/2362161
0