EXTREMEXPLOIT

Producer/Consumer + Monitors

Mar 17th, 2021 (edited)
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <unistd.h>
  4.  
  5. #define NTHREADS 2
  6. #define N 25 // Capacidad de nuestro buffer.
  7.  
  8. int BUFFER[25]; // Un 1 representa posición llena y un 0 representa que está vacía.
  9. pthread_mutex_t Lock;
  10. pthread_cond_t emptySpace; // Condición que indica si tenemos algun espacio libre que llenar.
  11. pthread_cond_t thereIsItem; // Condición que indica si tenemos algun elemento que consumir.
  12. int emptyElements = N; // Número actual de posiciones llenas.
  13.  
  14. void printBuffer(){ // Esto imprime nuestro buffer al estilo Python.
  15.     printf("[");
  16.     for (int i=0; i<N; i++){
  17.         if (i == N-1) printf("%d", BUFFER[i]);
  18.         else printf("%d, ", BUFFER[i]);
  19.     }
  20.     printf("]\n");
  21. }
  22.  
  23. void* CreateItem(){ // Busca la primera posición vacía y le añade un elemento.
  24.     for (int i=0; i<N; i++){
  25.         if (BUFFER[i] == 0) {
  26.             BUFFER[i] = 1; emptyElements--;
  27.             return NULL;
  28.         }
  29.     }
  30.     write(2, "System Overload! \n", sizeof("System Overload! \n")); // Si no ha encontrado ninguna posición vacía da error.
  31.     return NULL;
  32. }
  33.  
  34. void* ConsumeItem(){ // Busca si hay alguna posición llena y la consume.
  35.     for (int i=0; i<N; i++){
  36.         if (BUFFER[i] == 1){
  37.             BUFFER[i] = 0; emptyElements++;
  38.             return NULL;
  39.         }
  40.     }
  41.     write(2, "Empty Space! \n", sizeof("Empty Space! \n")); // Si no ha encontrado ninguna posición llena da error.
  42.     return NULL;
  43. }
  44.  
  45. void* ProducerFunction(){
  46.     for (int n=0; n<4*N; n++){
  47.         for (int i=0; i<N; i++){
  48.             pthread_mutex_lock(&Lock);
  49.             while(emptyElements == 0) pthread_cond_wait(&emptySpace, &Lock); // Mientras el buffer se encuentre lleno, esperamos a que haya algun espacio.
  50.             CreateItem(); // Ahora que hay un espacio, ya podemos añadir un elemento.
  51.             pthread_cond_broadcast(&thereIsItem); // Avisamos de que ya hay un elemento.
  52.             pthread_mutex_unlock(&Lock);
  53.         }
  54.     }
  55.     return NULL;
  56. }
  57. void* ConsumerFunction(){
  58.     for (int n=0; n<4*N; n++){
  59.         for (int i=0; i<N; i++){
  60.             pthread_mutex_lock(&Lock);
  61.             while(emptyElements == N) pthread_cond_wait(&thereIsItem, &Lock); // Mientras se encuentre vacío, esperamos que se genere algún elemento.
  62.             ConsumeItem(); // Ahora que ya hay almenos un elemento, lo vamos a consumir.
  63.             pthread_cond_signal(&emptySpace); // Avisamos de que ya hay un espacio libre.
  64.             pthread_mutex_unlock(&Lock);
  65.         }
  66.     }
  67.     return NULL;
  68. }
  69.  
  70. int main() {
  71.     pthread_t threadsID[NTHREADS];
  72.     for (int i=0; i<N; i++) BUFFER[i] = 0;
  73.     pthread_mutex_init(&Lock, NULL);
  74.     pthread_cond_init(&thereIsItem, NULL); pthread_cond_init(&emptySpace, NULL);
  75.     printf("Initial Buffer ---> "); printBuffer();
  76.  
  77.     // Creamos al Producer y al Consumer.
  78.     pthread_create(&threadsID[0], NULL, ProducerFunction, NULL);
  79.     pthread_create(&threadsID[1], NULL, ConsumerFunction, NULL);
  80.  
  81.     // Esperamos que terminen los dos threads.
  82.     pthread_join(threadsID[0], NULL);
  83.     pthread_join(threadsID[1], NULL);
  84.  
  85.     printf("Final Buffer ---> "); printBuffer();
  86.     return 0;
  87. }
  88.  
Add Comment
Please, Sign In to add comment