unix 互斥锁与信号量应用例子
admin
2023-02-04 20:00:06
0次

 



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

 

附件:http://down.51cto.com/data/2362161

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...