daily pastebin goal
55%
SHARE
TWEET

Untitled

a guest Jan 16th, 2018 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<stdio.h>
  2. #include<pthread.h>
  3. #include<semaphore.h>
  4. #include<stdlib.h>
  5. #include<time.h>
  6. #include <unistd.h>
  7. #define madhesia_bufferit 5
  8.  
  9. struct buff_struct
  10. {
  11.   int buffer[madhesia_bufferit];
  12.   int slota_eLire;
  13.   int slota_eZene;
  14.  
  15.   sem_t empty; //numeron sa slota jane te lira
  16.   sem_t full; // numeron sa slota jane te zena;
  17.   pthread_mutex_t mutex_lock; // siguron perjashtimin reciprok
  18.  
  19. }Bufferi;
  20.  
  21. void *Prodhuesi()
  22. {
  23.   srand(time(NULL));
  24.   while(1)
  25.   {
  26.  
  27.       int item_iProdhuar = rand()%101;//gjeneron te dhenen(nje numer)
  28.  
  29.       /*nqs nuk ka slote te lire duhet te presim per kete
  30.         sfhrytezojme semaforin empty */
  31.       sem_wait(&Bufferi.empty);
  32.  
  33.       /* nqs bufferi eshte i zene nga ndonje proces tjeter presim
  34.          duke thirrur semaforin binar mutex */
  35.       pthread_mutex_lock(&Bufferi.mutex_lock);
  36.        
  37.       /* nqs dy hapat e mesiperm kalohen athere jemi te lire te fusim te dhena ne buffer */
  38.       Bufferi.buffer[Bufferi.slota_eLire]=item_iProdhuar;
  39.  
  40.       //while(Bufferi.slota_eLire < madhesia_bufferit) Bufferi.slota_eLire++;
  41.       Bufferi.slota_eLire = (Bufferi.slota_eLire+1)%madhesia_bufferit;
  42.  
  43.  
  44.    
  45.  
  46.       /* Lirohet bufferi per perdorim nga konsumuesi */
  47.       pthread_mutex_unlock(&Bufferi.mutex_lock);
  48.       for(int j = 0; j < madhesia_bufferit; j++)
  49.         {
  50.             printf("| %d", Bufferi.buffer[j]);
  51.            
  52.         }
  53.       printf("\n");
  54.        
  55.       sleep(2);
  56.       /* Automatikisht rritet numri i variablit 'full' */
  57.       sem_post(&Bufferi.full);
  58.   }
  59.  
  60. }
  61.  
  62. void *Konsumuesi()
  63. {
  64.    int counter=1;
  65.   while(1)
  66.   {
  67.       int item_iLarguar;
  68.  
  69.       /* presim derisa prodhuesi te prodhoj ndonje item ,
  70.          per kete sfhrytezojme semaforin full duke ia bere check if full > 0  */
  71.       sem_wait(&Bufferi.full);
  72.  
  73.       /* nqs bufferi eshte i zene nga ndonje proces tjeter presim
  74.          duke thirrur semaforin binar mutex */
  75.       pthread_mutex_lock(&Bufferi.mutex_lock);
  76.        
  77.       /* nqs dy hapat e mesiperm kalohen athere jemi te lire te largojme te dhena ne buffer */
  78.       item_iLarguar = Bufferi.buffer[Bufferi.slota_eZene];
  79.  
  80.       //while(Bufferi.slota_eZene < madhesia_bufferit) Bufferi.slota_eZene++;
  81.       Bufferi.slota_eZene = (Bufferi.slota_eZene+1)%madhesia_bufferit;
  82.  
  83.  
  84.  
  85.       /* Lirohet bufferi per perdorim nga prodhuesi */
  86.       pthread_mutex_unlock(&Bufferi.mutex_lock);
  87.  
  88.       for(int j = 0; j< madhesia_bufferit; j++)
  89.         {
  90.             Bufferi.buffer[(counter - 1) % 5] = 0;
  91.             //printf("| %d haha", );
  92.         }
  93.         printf("\n\n\n");
  94.     counter++;
  95.       /* Automatikisht rritet numri i variablit 'empty' */
  96.       sem_post(&Bufferi.empty);
  97.   }
  98.  
  99. }
  100.  
  101. void main()
  102. {
  103.     pthread_t t1,t2;//t1 si prodhues,t2 si konsumues dmth threda per qito dyja
  104.    
  105.     sem_init(&Bufferi.empty,0,madhesia_bufferit);//0 dmth po pajtohet mu bo shared
  106.     sem_init(&Bufferi.full,0,0);
  107.     pthread_mutex_init(&Bufferi.mutex_lock, NULL);
  108.  
  109.    
  110.     Bufferi.slota_eLire=0;
  111.     Bufferi.slota_eZene=0;
  112.  
  113.     pthread_create(&t1,NULL,Prodhuesi,NULL);//qiky thread po kriojet per prodhuesin
  114.    
  115.     pthread_create(&t2,NULL,Konsumuesi,NULL);//qiky thread po kriojet per prodhuesin
  116.    
  117.     pthread_join(t1,NULL);
  118.     pthread_join(t2,NULL);
  119.    
  120.  
  121.  
  122.  
  123.  
  124.  
  125. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top