[text] test

Viewer

  1. /*
  2.  * Finden Sie in Task3.c den Deadlock.
  3.  * Beheben Sie den Deadlock.
  4.  * Ändern Sie das Programm so ab, dass es kompilierbar und ausführbar ist.
  5.  */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <pthread.h>
  9.  
  10. struct List{
  11.         int data;
  12.         struct List* next;
  13. };
  14.  
  15. int popFirst(struct List** _list) {
  16.         if(*_list == NULL || *_list == (struct List*)-1)
  17.                 return 0;
  18.  
  19.         int val = (*_list)->data;
  20.         struct List* t = (*_list)->next;
  21.         free(*_list);
  22.         *_list = t;
  23.         return val;
  24. }
  25.  
  26. void put(struct List** _list, int _data) {
  27.         if(*_list == NULL || *_list == (struct List*)-1) {
  28.                 *_list = (struct List*)malloc(sizeof(struct List));
  29.                 (*_list)->data = _data;
  30.                 (*_list)->next = NULL;
  31.         } else {
  32.                 put(&((*_list)->next), _data);
  33.         }
  34. }
  35.  
  36.  
  37. struct List* MaterialQueue = (struct List*)-1;
  38. int ElementCount = 0;
  39. pthread_mutex_t QueueLock, CountLock;
  40.  
  41. void* producerFnc(void* in) {
  42.         srand(1234);
  43.         int counter = 0;
  44.         while( 1 && counter < 100) {
  45.                 pthread_mutex_lock(&QueueLock);
  46.                 pthread_mutex_lock(&CountLock);
  47.                 put(&MaterialQueue, rand());
  48.                 ElementCount++;
  49.                 counter++;
  50.                 pthread_mutex_unlock(&CountLock);
  51.                 pthread_mutex_unlock(&QueueLock);
  52.         }
  53.         return 0;
  54. }
  55.  
  56. void* workerFnc(void* _in) {
  57.         int id = ((int*)_in)[0];
  58.         int element;
  59.         long sum = 0;
  60.         while( 1 && MaterialQueue != NULL) {
  61.                 pthread_mutex_lock( &QueueLock );
  62.                 pthread_mutex_lock( &CountLock );
  63.                 if(ElementCount > 0) {
  64.                         element =  popFirst(&MaterialQueue);
  65.                         ElementCount--;
  66.                 }
  67.                 pthread_mutex_unlock( &CountLock );
  68.                 pthread_mutex_unlock( &QueueLock );
  69.                 printf("%d: %d\n", id, element);
  70.                 usleep(5);
  71.         }
  72.         return 0;
  73. }
  74.  
  75. int main(int argc, char** argv) {
  76.         pthread_t producer;
  77.         pthread_t worker[15];
  78.        
  79.         pthread_create( &producer, 0, producerFnc, 0 );
  80.         for( int i = 0; i < 15; i++ ) {
  81.                 pthread_create( &worker[i], 0, workerFnc, &i);
  82.         }
  83.  
  84.         pthread_join( producer, 0 );
  85.         for( int i = 0; i < 15; i++ ) {
  86.                 pthread_join( worker[i], 0 );
  87.         }
  88.         return 0;
  89. }
  90.  
  91.  

Editor

You can edit this paste and save as new:


File Description
  • test
  • Paste Code
  • 01 Mar-2021
  • 1.93 Kb
You can Share it: