linux多进程多线程互斥同步例子

187 篇文章 1 订阅
168 篇文章 0 订阅
[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4.   
  5. #include <sys/shm.h>  
  6. #include <pthread.h>  
  7. #include <errno.h>  
  8.   
  9. #define DEBUG 1  
  10. #define SHARE_KEY 0x1234  
  11. #define THREAD_NUM 4  
  12.   
  13. typedef struct{  
  14.     pthread_mutex_t lock;  
  15.     pthread_cond_t cond;  
  16.     char msg[180];  
  17.     int num;  
  18. }Share_Stuff;  
  19.   
  20. static Share_Stuff* stuff[THREAD_NUM];  
  21.   
  22. void* threadB(void *prm);  
  23.   
  24.   
  25. int main(int argc,char** argv)  
  26. {  
  27.     void *share_addr=NULL;  
  28.     pthread_t tid[THREAD_NUM];  
  29.     int shmid = -1;  
  30.     int ret=0;  
  31.     int i=0;  
  32.   
  33. #if DEBUG  
  34.     printf("______%s______%s______\n",__DATE__,__TIME__);  
  35. #endif  
  36.   
  37. //share menory//
  38.     shmid = shmget(SHARE_KEY,sizeof(Share_Stuff)*THREAD_NUM, 0666|IPC_CREAT);  
  39.     if(shmid == -1){  
  40.         printf("Create share memory fail! - %s\n",strerror(errno));  
  41.     }  
  42.   
  43.     share_addr=(void*)shmat(shmid,(void*)0,0);  
  44.     if(share_addr < 0){  
  45.         printf("Get share memory address error! - %s\n",strerror(errno));     
  46.     }  
  47.   
  48. //get the addr  
  49. #if DEBUG  
  50.     printf("share_addr:%x\n",(unsigned int)share_addr);  
  51. #endif  
  52.   
  53.     for(;i<THREAD_NUM;i++){  
  54.         stuff[i]=(Share_Stuff*)share_addr+i;  
  55.         stuff[i]->num=i;  
  56. #if DEBUG  
  57.         printf("stuff's addr:%x\n",(unsigned int)stuff[i]);  
  58. #endif  
  59.     }  
  60.   
  61.     i=0;  
  62. //create_thread//
  63.     pthread_condattr_t cond_attr;  
  64.     pthread_condattr_init(&cond_attr);  
  65.     pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED);  
  66.   
  67.     pthread_mutexattr_t mutex_attr;  
  68.     pthread_mutexattr_init(&mutex_attr);  
  69.     pthread_mutexattr_setpshared(&mutex_attr, PTHREAD_PROCESS_SHARED);  
  70.       
  71.     for(;i<THREAD_NUM;i++){  
  72.         pthread_cond_init(&stuff[i]->cond, &cond_attr);  
  73.         pthread_mutex_init(&stuff[i]->lock, &mutex_attr);  
  74.         ret=pthread_create(&tid[i],0,threadB,(void*)stuff[i]);  
  75.         if(ret!=0){  
  76.             printf("ThreadA%d create error!\n",i);  
  77.         }  
  78.     }  
  79.   
  80.     pthread_condattr_destroy(&cond_attr);  
  81.     pthread_mutexattr_destroy(&mutex_attr);  
  82.   
  83.     i=0;  
  84. wait_for_done
  85.     for(;i<THREAD_NUM;i++){  
  86.         ret=pthread_join(tid[i],NULL);  
  87.     }  
  88.   
  89. #if DEBUG  
  90.     printf("all thread done!\n");  
  91. #endif  
  92.   
  93.     return 0;  
  94. }  
  95.   
  96.   
  97. void* threadB(void *prm)  
  98. {  
  99.     Share_Stuff *stuff;  
  100.       
  101. #if DEBUG  
  102.     printf("thread's prm:%x\n",(unsigned int)prm);  
  103. #endif  
  104.   
  105.     stuff=(Share_Stuff *)prm;  
  106.     while(1){  
  107.         sleep(1);  
  108.         //printf("pthread_cond_wait\n");  
  109.         pthread_cond_wait(&stuff->cond,&stuff->lock);  
  110.         printf("message:%s",stuff->msg);  
  111. /*******************************************************/  
  112.         sleep(3);     
  113.         //printf("pthread_mutex_lock\n");  
  114.         pthread_mutex_lock(&stuff->lock);  
  115.             sprintf(stuff->msg,"threadA--%d\n",stuff->num);  
  116.         //  printf("pthread_cond_signal\n");  
  117.             pthread_cond_signal(&stuff->cond);  
  118.         //printf("pthread_mutex_unlock\n");  
  119.         pthread_mutex_unlock(&stuff->lock);  
  120.     }  
  121. }  


进程2

[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4.   
  5. #include <sys/shm.h>  
  6. #include <pthread.h>  
  7. #include <errno.h>  
  8.   
  9. #define DEBUG 1  
  10. #define SHARE_KEY 0x1234  
  11. #define THREAD_NUM 4  
  12.   
  13. typedef struct{  
  14.     pthread_mutex_t lock;  
  15.     pthread_cond_t cond;  
  16.     char msg[180];  
  17.     int num;  
  18. }Share_Stuff;  
  19.   
  20. static Share_Stuff* stuff[THREAD_NUM];  
  21.   
  22. void* threadA(void *prm);  
  23.   
  24.   
  25. int main(int argc,char** argv)  
  26. {  
  27.     void *share_addr=NULL;  
  28.     pthread_t tid[THREAD_NUM];  
  29.     int shmid = -1;  
  30.     int ret=0;  
  31.     int i=0;  
  32.   
  33. #if DEBUG  
  34.     printf("______%s______%s______\n",__DATE__,__TIME__);  
  35. #endif  
  36.   
  37. /share menory//
  38.     shmid = shmget(SHARE_KEY,sizeof(Share_Stuff)*THREAD_NUM, 0666|IPC_CREAT);  
  39.     if(shmid == -1){  
  40.         printf("Create share memory fail! - %s\n",strerror(errno));  
  41.     }  
  42.   
  43.     share_addr=(void*)shmat(shmid,(void*)0,0);  
  44.     if(share_addr < 0){  
  45.         printf("Get share memory address error! - %s\n",strerror(errno));     
  46.     }  
  47.   
  48. get the addr
  49. #if DEBUG  
  50.     printf("share_addr:%x\n",(unsigned int)share_addr);  
  51. #endif  
  52.   
  53.     for(;i<THREAD_NUM;i++){  
  54.         stuff[i]=(Share_Stuff*)share_addr+i;  
  55.         stuff[i]->num=i;  
  56. #if DEBUG  
  57.         printf("stuff's addr:%x\n",(unsigned int)stuff[i]);  
  58. #endif  
  59.     }  
  60.   
  61.     i=0;  
  62. //create_thread/// 
  63.     //pthread_condattr_t cond_attr;  
  64.     //pthread_condattr_init(&cond_attr);  
  65.     //pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED);  
  66.   
  67.     //pthread_mutexattr_t mutex_attr;  
  68.     //pthread_mutexattr_init(&mutex_attr);  
  69.     //pthread_mutexattr_setpshared(&mutex_attr, PTHREAD_PROCESS_SHARED);  
  70.       
  71.     for(;i<THREAD_NUM;i++){  
  72.         //pthread_cond_init(&stuff[i]->cond, &cond_attr);  
  73.         //pthread_mutex_init(&stuff[i]->lock, &mutex_attr);  
  74.         ret=pthread_create(&tid[i],0,threadA,(void*)stuff[i]);  
  75.         if(ret!=0){  
  76.             printf("ThreadA%d create error!\n",i);  
  77.         }  
  78.     }  
  79.   
  80.     //pthread_condattr_destroy(&cond_attr);  
  81.     //pthread_mutexattr_destroy(&mutex_attr);  
  82.   
  83.     i=0;  
  84. //wait_for_done/  
  85.     for(;i<THREAD_NUM;i++){  
  86.         ret=pthread_join(tid[i],NULL);  
  87.     }  
  88.   
  89. #if DEBUG  
  90.     printf("all thread done!\n");  
  91. #endif  
  92.   
  93.     return 0;  
  94. }  
  95.   
  96.   
  97. void* threadA(void *prm)  
  98. {  
  99.     Share_Stuff *stuff;  
  100.       
  101. #if DEBUG  
  102.     printf("thread's prm:%x\n",(unsigned int)prm);  
  103. #endif  
  104.   
  105.     stuff=(Share_Stuff *)prm;  
  106.     while(1){  
  107.         sleep(3);  
  108.         //printf("pthread_mutex_lock\n");  
  109.         pthread_mutex_lock(&stuff->lock);  
  110.             sprintf(stuff->msg,"threadB--%d\n",stuff->num);  
  111.         //  printf("pthread_cond_signal\n");  
  112.             pthread_cond_signal(&stuff->cond);     
  113.         //printf("pthread_mutex_unlock\n");  
  114.         pthread_mutex_unlock(&stuff->lock);  
  115. /*******************************************************/  
  116.         sleep(1);  
  117.         //printf("pthread_cond_wait\n");  
  118.         pthread_cond_wait(&stuff->cond,&stuff->lock);  
  119.         printf("message:%s",stuff->msg);  
  120.               
  121.     }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值