zoukankan      html  css  js  c++  java
  • 火车票售票系统

      1 #include<stdio.h>
      2 #include<pthread.h>
      3 #include<string.h>
      4 #include<stdlib.h>
      5 
      6 typedef struct tag {
      7     int ticketcount;
      8     pthread_mutex_t lock;
      9     pthread_cond_t cond1,cond2;    //cond1 putter 补票 cond2 seller 
     10 }NODE,*pNODE;
     11 
     12 
     13 void* sale(void* arg)
     14 {
     15     pNODE p = (pNODE)arg;
     16 
     17     while(1){
     18         pthread_mutex_lock(&p->lock);
     19         while(p->ticketcount <= 0)    //not if  用if卖票会有负值
     20         {
     21             pthread_cond_signal(&p->cond2);
     22 
     23             pthread_cond_wait(&p->cond1,&p->lock);
     24         }
     25         printf("%d sell %d
    ",getpid(),p->ticketcount);
     26         p->ticketcount--;
     27         pthread_mutex_unlock(&p->lock);        
     28     }
     29 }
     30 
     31 void* putter(void* arg)
     32 {
     33     pNODE p = (pNODE)arg;
     34     while(1){
     35         pthread_mutex_lock(&p->lock);
     36         while(p->ticketcount > 0){
     37             pthread_cond_wait(&p->cond2,&p->lock);
     38             pthread_cond_signal(&p->cond1);
     39         }
     40         p->ticketcount += 10;
     41         printf("tickets on
    ");
     42         sleep(1);
     43         pthread_mutex_unlock(&p->lock);
     44     }
     45 
     46 }
     47 
     48 
     49 int main(int argc,char* argv[])//m seller ,n putter 通过命令行传入m个售票员,n个补票员
     50 {
     51     if(argc!=3)
     52     {
     53         printf("wrong argv!
    ");
     54         exit(1);
     55     }
     56     int m = atoi(argv[1]);
     57     int n = atoi(argv[2]);
     58     pthread_t arr[m+n];
     59     memset(arr,0,sizeof(arr));
     60     NODE anode;
     61     memset(&anode,0,sizeof(anode));
         //初始化锁
    62 if(pthread_mutex_init(&anode.lock, NULL)!=0) 63 { 64 printf("mutex_init fail! "); 65 exit(1); 66 } 67 anode.ticketcount = 0; 68 //初始化条件变量 69 if(pthread_cond_init(&anode.cond1,NULL)) 70 { 71 printf("cond_init fail! "); 72 exit(1); 73 } 74 75 if(pthread_cond_init(&anode.cond2,NULL)) 76 { 77 printf("cond_init fail! "); 78 exit(1); 79 } 80 81 int index = 0; 82 //创建m个售票线程 83 while(m){ 84 if(pthread_create(&arr[index],NULL,sale,(void*)&anode)!=0) 85 { 86 printf("pthread_create sale fail! "); 87 exit(1); 88 } 89 m--; 90 index++; 91 }
        // 创建n个补票线程
    92 while(n){ 93 if(pthread_create(&arr[index],NULL,putter,(void*)&anode)!=0){ 94 printf("pthread_create putter fail "); 95 exit(1); 96 } 97 n--; 98 index++; 99 } 100 101 index--; 102   //由于程序是死循环,下面不会执行 103 while(index) 104 { 105 pthread_join(arr[index],NULL); 106 index--; 107 } 108 109 110 111 pthread_cond_destroy(&anode.cond1); 112 pthread_cond_destroy(&anode.cond2); 113 return 0; 114 }
  • 相关阅读:
    动态表格
    Palindrome Number
    String to Integer (atoi) ???
    Reverse Integer
    Two Sum
    Path Sum
    Minimum Depth of Binary Tree
    Plus One
    Maximum Depth of Binary Tree
    Symmetric Tree
  • 原文地址:https://www.cnblogs.com/luolizhi/p/4531869.html
Copyright © 2011-2022 走看看