进程同步

 若干 个汽车 参数 1  等待 若干个窗口处理 参数2, 然后进行锁操作后对共享内存进行写入。
  1. #include<sys/sem.h>
  2. #include<sys/types.h>
  3. #include<sys/ipc.h>
  4. #include<stdio.h>
  5. #include<sys/stat.h>
  6. #include<pthread.h>
  7. #include<stdlib.h>
  8. #include <sys/shm.h>
  9. #define MAXNITEMS 1000000
  10. #define MAXNTHREADS 100
  11. #define min(a,b)((a) < (b) ? (a) : (b))
  12. #define max(a,b)((a) > (b) ? (a) : (b))
  13. int nitems;
  14. char *pmat = NULL;
  15. char tempbuff[16]; 
  16. struct{
  17.     //互斥量的类型为pthread_mutex_t
  18.     pthread_mutex_t mutex;
  19.     char buff[200][MAXNITEMS];
  20.     int nput;//计数器
  21.     int nval;//值
  22. }shared={PTHREAD_MUTEX_INITIALIZER};
  23. void *enregistrer(void *),*taiter(void *);
  24. int main(int argc,char **argv){
  25.     
  26.     int i,nthreads,count[MAXNTHREADS];
  27.     pthread_t tid_produce[MAXNITEMS],tid_consume;
  28.     int id_of_shmid;
  29.     int j;
  30.     
  31.     if(argc!=3)  {  
  32.         printf("un grave problem sur le num de argument/n");
  33.         exit (-1);
  34.         }
  35.             
  36.     //combien de voiture    
  37.     nitems = min(atoi(argv[1]),MAXNITEMS);
  38.     //combien de guichet
  39.     nthreads=min(atoi(argv[2]),MAXNTHREADS);    
  40.     //etablir une partager mémoire
  41.     id_of_shmid = shmget(0x1234, nitems*100, 0666|IPC_CREAT);   
  42.     //printf(".............%d",id_of_shmid);
  43.     
  44.     pmat = (char *)shmat(id_of_shmid, 0, 0);
  45.     
  46.     pthread_setconcurrency(nthreads+1);
  47.     for (i = 0; i < nthreads; i++) {
  48.         count[i] = 0;
  49.         pthread_create(&tid_produce[i], NULL, enregistrer, &count[i]);
  50.     }
  51.     //memoire partage pour faire incrementation du matricule
  52.     pthread_create(&tid_consume,NULL,taiter,NULL);  
  53.     for (i = 0; i < nthreads; i++) {
  54.         pthread_join(tid_produce[i], NULL);
  55.         printf("Guichet[%d] traite %d voitures. /n", i, count[i]);  
  56.     }
  57.     pthread_join(tid_consume, NULL);
  58.     
  59.     printf("---------------matricule--------------------/n");
  60.     
  61.     for (j = 0; j < nitems; j++) {      
  62.         
  63.         printf("ID de traitement: %s/n",pmat+j*100);
  64.         
  65.     }
  66.     printf("---------------------------------------------/n");
  67.     
  68.     
  69.     shmdt(pmat);    
  70.     exit(0);
  71.             
  72. }
  73. void *
  74. enregistrer(void *arg){
  75.     for ( ; ; ) {
  76.         sleep(1);
  77.         pthread_mutex_lock(&shared.mutex);
  78.         if (shared.nput >= nitems) {
  79.             pthread_mutex_unlock(&shared.mutex);
  80.             return(NULL);       
  81.         }
  82.         //表明数据是多少
  83.         sprintf(shared.buff[shared.nput],"%d",shared.nput);
  84.         //trois chiffres incrémentables,trois lettres incrementables et deux chiffres fixes
  85.         strcat(shared.buff[shared.nput],"00TP300"),
  86.         shared.nput++;
  87.         shared.nval++;
  88.         pthread_mutex_unlock(&shared.mutex);
  89.         *((int *) arg) += 1;        
  90.     }
  91. }
  92. void *
  93. taiter(void *arg){
  94.     int     i;
  95.     for (i = 0; i < nitems; i++) {      
  96.         taiter_wait(i);
  97.         
  98.         //strcat(tempbuff,shared.buff[i]);
  99.         //printf("...%s/n", tempbuff);
  100.         printf("C'est le numero de [%d] voitrue et son numero de matricule est %s/n", i,shared.buff[i]);
  101.         //printf("%s",pmat+i*1024);
  102.         memcpy(pmat+i*100, shared.buff[i], 100);
  103.     }
  104.     return(NULL);
  105. }
  106. //写入和内存分享在这里
  107. void
  108. taiter_wait(int i){
  109.     for ( ; ; ) {
  110.         pthread_mutex_lock(&shared.mutex);
  111.         if (i < shared.nput) {
  112.             pthread_mutex_unlock(&shared.mutex);
  113.             return
  114.         }
  115.         pthread_mutex_unlock(&shared.mutex);
  116.     }
  117. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值