买票系统

来源:互联网 发布:淘宝投诉假货流程 编辑:程序博客网 时间:2024/05/01 21:10
  1. #include <stdio.h>  
  2.   
  3. #include <stdlib.h>  
  4.   
  5. #include <pthread.h>  
  6.   
  7. //#include <unistd.h>  
  8.   
  9. //#include <errno.h>  
  10.   
  11. #include <time.h>  
  12.   
  13.   
  14.   
  15. pthread_mutex_t mutex;  
  16.   
  17. pthread_cond_t cond1;  
  18.   
  19. pthread_cond_t cond2;  
  20.   
  21. pthread_cond_t cond3;  
  22.   
  23.   
  24.   
  25. int ticket = 10;  
  26.   
  27.   
  28.   
  29. void *sell_ticket1(void)  
  30.   
  31. {  
  32.   
  33.     while(1)  
  34.   
  35.     {  
  36.   
  37.         pthread_mutex_lock(&mutex);  
  38.   
  39.         pthread_cond_wait(&cond1,&mutex);  
  40.   
  41.         if(ticket > 0)  
  42.   
  43.         {  
  44.   
  45.             printf("sell ticket1--> chool ticket:%d\n",ticket--);  
  46.   
  47.             //sleep(1);  
  48.   
  49.             pthread_mutex_unlock(&mutex);  
  50.   
  51.         }  
  52.   
  53.         else  
  54.   
  55.         {  
  56.   
  57.             pthread_mutex_unlock(&mutex);  
  58.   
  59.             break;  
  60.   
  61.         }  
  62.   
  63.     }  
  64.   
  65.     return NULL;  
  66.   
  67. }  
  68.   
  69.   
  70.   
  71. void *sell_ticket2(void)  
  72.   
  73. {  
  74.   
  75.     while(1)  
  76.   
  77.     {  
  78.   
  79.         pthread_mutex_lock(&mutex);  
  80.   
  81.         pthread_cond_wait(&cond2,&mutex);  
  82.   
  83.         if(ticket > 0)  
  84.   
  85.         {  
  86.   
  87.             //sleep(1);  
  88.   
  89.             printf("sell ticket2--> chool ticket:%d\n",ticket--);  
  90.   
  91.             //sleep(1);  
  92.   
  93.             pthread_mutex_unlock(&mutex);  
  94.   
  95.         }  
  96.   
  97.         else  
  98.   
  99.         {  
  100.   
  101.             pthread_mutex_unlock(&mutex);  
  102.   
  103.             break;  
  104.   
  105.         }  
  106.   
  107.     }  
  108.   
  109. }  
  110.   
  111.   
  112.   
  113. void *sell_ticket3(void)  
  114.   
  115. {  
  116.   
  117.     while(1)  
  118.   
  119.     {  
  120.   
  121.         pthread_mutex_lock(&mutex);  
  122.   
  123.         pthread_cond_wait(&cond3,&mutex);  
  124.   
  125.         if(ticket > 0)  
  126.   
  127.         {  
  128.   
  129.             //sleep(1);  
  130.   
  131.             printf("sell ticket3--> chool ticket:%d\n",ticket--);  
  132.   
  133.             //sleep(1);  
  134.   
  135.             pthread_mutex_unlock(&mutex);  
  136.   
  137.          }  
  138.   
  139.         else  
  140.   
  141.         {  
  142.   
  143.             pthread_mutex_unlock(&mutex);  
  144.   
  145.             break;  
  146.   
  147.         }         
  148.   
  149.     }  
  150.   
  151.     return NULL;  
  152.   
  153. }  
  154.   
  155.   
  156.   
  157. int main()  
  158.   
  159. {  
  160.   
  161.     int i=0,flog=0,ret=0;  
  162.   
  163.     pthread_t id1,id2,id3;  
  164.   
  165.     int old_ticket = ticket;  
  166.   
  167.   
  168.   
  169.     srand(time(NULL));  
  170.   
  171.     pthread_mutex_init(&mutex,NULL);  
  172.   
  173.     pthread_cond_init(&cond1,NULL);  
  174.   
  175.     pthread_cond_init(&cond2,NULL);  
  176.   
  177.     pthread_cond_init(&cond3,NULL);  
  178.   
  179.   
  180.   
  181.     ret = pthread_create(&id1, NULL, (void*)sell_ticket1, NULL);  
  182.   
  183.     if(ret == -1)  
  184.   
  185.     {  
  186.   
  187.         printf("create_thread1 error!\n");  
  188.   
  189.         exit(-1);  
  190.   
  191.     }  
  192.   
  193.     ret = pthread_create(&id2, NULL, (void*)sell_ticket2, NULL);  
  194.   
  195.     if(ret == -1)  
  196.   
  197.     {  
  198.   
  199.         printf("create_thread2 error!\n");  
  200.   
  201.         exit(-1);  
  202.   
  203.     }  
  204.   
  205.     ret = pthread_create(&id3, NULL, (void*)sell_ticket3, NULL);  
  206.   
  207.     if(ret == -1)  
  208.   
  209.     {  
  210.   
  211.         printf("create_thread3 error!\n");  
  212.   
  213.         exit(-1);  
  214.   
  215.     }  
  216.   
  217.   
  218.   
  219.     sleep(1);  
  220.   
  221.     for(i =0; i< old_ticket; i++)  
  222.   
  223.     {  
  224.   
  225.         flog = rand() % 3;  
  226.   
  227.         //printf("%d\n",flog);  
  228.   
  229.         switch(flog)  
  230.   
  231.         {  
  232.   
  233.             case 0: pthread_cond_signal(&cond1);break;  
  234.   
  235.             case 1: pthread_cond_signal(&cond2);break;  
  236.   
  237.             case 2: pthread_cond_signal(&cond3);break;  
  238.   
  239.         }  
  240.   
  241.         sleep(1);  
  242.   
  243.     }  
  244.   
  245.     pthread_cond_signal(&cond1);  
  246.   
  247.     sleep(1);  
  248.   
  249.     pthread_cond_signal(&cond2);  
  250.   
  251.     sleep(1);  
  252.   
  253.     pthread_cond_signal(&cond3);  
  254.   
  255.     sleep(1);  
  256.   
  257.     ret = pthread_join(id1, NULL);  
  258.   
  259.     if(ret == -1)  
  260.   
  261.     {  
  262.   
  263.         printf("join_thread1 error!\n");  
  264.   
  265.         exit(-1);  
  266.   
  267.     }  
  268.   
  269.     printf("id1 exit!\n");  
  270.   
  271.     ret = pthread_join(id2, NULL);  
  272.   
  273.     if(ret == -1)  
  274.   
  275.     {  
  276.   
  277.         printf("join_thread2 error!\n");  
  278.   
  279.         exit(-1);  
  280.   
  281.     }  
  282.   
  283.     printf("id2 exit!\n");  
  284.   
  285.     ret = pthread_join(id3, NULL);  
  286.   
  287.     if(ret == -1)  
  288.   
  289.     {  
  290.   
  291.         printf("join_thread3 error!\n");  
  292.   
  293.         exit(-1);  
  294.   
  295.     }  
  296.   
  297.     printf("id3 exit!\n");  
  298.   
  299.       
  300.   
  301.     return 0;  
  302.   
  303. }
0 0
原创粉丝点击