Advertisement
LinKin

offline2

Jun 9th, 2012
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<pthread.h>
  4. #include<semaphore.h>
  5. #include<unistd.h>
  6. #include<queue>
  7. using namespace std;
  8.  
  9. #define MAX 20
  10. #define MAX_B 3
  11.  
  12. pthread_t Booth_T[MAX_B+7];
  13. pthread_t HeadC_T;
  14.  
  15. queue<long> Booth_Q[MAX_B+7];
  16. queue<long> HeadC_Q;
  17.  
  18. pthread_mutex_t Booth_LK[MAX_B+7];
  19. pthread_mutex_t HeadC_LK;
  20.  
  21. sem_t Booth_SM[MAX_B+7];
  22. sem_t Customer_SM[MAX+7];
  23. sem_t Empty;
  24. sem_t Full;
  25.  
  26. void* Customer_Func( void *Arg )
  27. {
  28.     long Nm = *((long*)Arg);
  29.     long b = rand()%MAX_B;
  30.  
  31.     printf("Customer %ld: Has come to booth %ld\n",Nm,b );
  32.     sem_init( &Customer_SM[Nm],0,0 );
  33.     pthread_mutex_lock( &Booth_LK[b] );
  34.         Booth_Q[b].push( Nm );
  35.         sem_post( &Booth_SM[b] );
  36.     pthread_mutex_unlock( &Booth_LK[b] );
  37.     sem_wait( &Customer_SM[Nm] );
  38.     printf("Customer %ld: Gone home\n",Nm );
  39. }
  40.  
  41.  
  42.  
  43. void* Booth_Func( void *Arg )
  44. {
  45.     long b = *((long*)Arg);
  46.     while(1){
  47.  
  48.         sem_wait( &Booth_SM[b] );
  49.  
  50.         pthread_mutex_lock( &Booth_LK[b] );
  51.             long Nm = Booth_Q[b].front();
  52.             Booth_Q[b].pop();
  53.             printf("Customer %ld: In booth %ld cashier has collected the money and signed the receipt\n",Nm,b );
  54.             sleep(1);
  55.         pthread_mutex_unlock( &Booth_LK[b] );
  56.  
  57.         sem_wait( &Empty );
  58.         pthread_mutex_lock( &HeadC_LK );
  59.             HeadC_Q.push( Nm );
  60.             printf("Customer %ld: In booth %ld cashier has passed the receipt to the head cashier\n",Nm,b );
  61.             sem_post( &Full );
  62.         pthread_mutex_unlock( &HeadC_LK );
  63.     }
  64.  
  65. }
  66.  
  67.  
  68. void* HeadC_Func( void* Arg ){
  69.  
  70.     while(1){
  71.         sem_wait( &Full );
  72.         pthread_mutex_lock( &HeadC_LK );
  73.             long Nm = HeadC_Q.front();
  74.             HeadC_Q.pop();
  75.             printf("customer %ld: Head Cashier has received the receipt , given initial and passed back to customer\n",Nm );
  76.         pthread_mutex_unlock( &HeadC_LK );
  77.             sem_post( &Empty );
  78.             sem_post( &Customer_SM[Nm] );
  79.        
  80.         sleep(1);
  81.     }
  82. }
  83.  
  84.  
  85. int main(void)
  86. {
  87.     long i;
  88.  
  89.     for( i=0;i<MAX_B;i++ ){
  90.         long *p = new long;
  91.         *p = i;
  92.         pthread_create( &Booth_T[i],NULL,Booth_Func,(void*)p );
  93.         sem_init( &Booth_SM[i],0,0 );
  94.         pthread_mutex_init( &Booth_LK[i],0 );
  95.     }
  96.     pthread_create( &HeadC_T,NULL,HeadC_Func,NULL );
  97.     sem_init( &Empty,0,5 );
  98.     sem_init( &Full,0,0 );
  99.  
  100.     long Ct = 1;
  101.     while( Ct<=MAX ){
  102.         long *p = new long;
  103.         *p = Ct;
  104.         pthread_create( new pthread_t,NULL,Customer_Func,(void*)p );
  105.         Ct++;
  106.     }
  107.  
  108.     for( i=0;i<MAX_B;i++ ){
  109.         pthread_join( Booth_T[i],NULL );
  110.     }
  111.     pthread_join( HeadC_T,NULL );
  112.  
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement