Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <pthread.h>
  5. #include <sys/types.h>
  6.  
  7. #define SENTINEL -1
  8.  
  9. struct node {
  10. int element;
  11. struct node *next;
  12. };
  13.  
  14. struct linkedlist {
  15. struct node *head;
  16. int length;
  17. pthread_mutex_t lock;
  18. };
  19.  
  20. void init(struct linkedlist *list){
  21. list->length = 0;
  22. list->head = NULL;
  23. pthread_mutex_init(&list->lock, NULL);
  24. }
  25.  
  26. void clear(struct linkedlist *list){
  27. pthread_mutex_destroy(&list->lock);
  28. for(int i = 0; i < list->length; i++)
  29. remove(0);
  30. }
  31.  
  32. void push(struct linkedlist *list, int element){
  33. struct node *node = malloc(sizeof(struct node));
  34. node->element = element;
  35. node->next = NULL;
  36. if(list->head == NULL){
  37. list->head = node;
  38. } else {
  39. struct node *last = list->head;
  40. while(last->next != NULL){
  41. last = last->next;
  42. }
  43. last->next = node;
  44. }
  45. ++list->length;
  46. }
  47.  
  48. int remove_at(struct linkedlist *list, int index){
  49. int retval;
  50. if(index == 0){
  51. retval = list->head->element;
  52. struct node* n = list->head->next;
  53. free(list->head);
  54. list->head = n;
  55. } else {
  56. struct node* n = list->head;
  57. for(int i = 0; i < index-1; i++)
  58. n = n->next;
  59. struct node *to_be_removed = n->next;
  60. retval = to_be_removed->element;
  61. n->next = to_be_removed->next;
  62. free(to_be_removed);
  63. }
  64. --list->length;
  65. return retval;
  66. }
  67.  
  68. void *erstellen(void *arg){
  69. struct linkedlist *list = (struct linkedlist *)arg;
  70. for(int aufgabe = 0; aufgabe < 1000; aufgabe++){
  71. // Schläft eine zufällige Zeit im Bereich [0,1000) us. Simuliert die zum Erstellen einer Aufgabe notwendige Zeit.
  72. usleep(rand()%1000);
  73. printf("Thread #%d erstellt Aufgabe #%d\n", (int)pthread_self(), aufgabe);
  74. pthread_mutex_lock(&list->lock);
  75. push(list, aufgabe);
  76. pthread_mutex_unlock(&list->lock);
  77. }
  78. // Signalisiert, dass alle Aufgaben erstellt worden sind.
  79. push(list, SENTINEL);
  80. return NULL;
  81. }
  82.  
  83. void *verifizieren(void *arg){
  84. struct linkedlist *list = (struct linkedlist *)arg;
  85. while(1){
  86. pthread_mutex_lock(&list->lock);
  87. if(list->length > 0){
  88. int aufgabe = remove_at(list, 0);
  89. pthread_mutex_unlock(&list->lock);
  90. // SENTINEL bedeutet, dass wir ans Ende der Aufgabenliste angekommen sind und, dass alle Aufgaben verifiziert worden sind.
  91. if(aufgabe == SENTINEL){
  92. // Pusht den SENTINEL Wert zurück in die Liste.
  93. push(list, SENTINEL);
  94. return NULL;
  95. }
  96. // Schläft eine zufällige Zeit im Bereich [0,1000) us. Simuliert die zum Verifizieren einer Aufgabe notwendige Zeit.
  97. usleep(rand()%1000);
  98. printf("Thread #%d verifiziert Aufgabe #%d\n", (int)pthread_self(), aufgabe);
  99. }
  100. else{
  101. pthread_mutex_unlock(&list->lock);
  102. }
  103. }
  104. }
  105.  
  106.  
  107. int main(){
  108. struct linkedlist list;
  109. init(&list);
  110.  
  111. pthread_mutex_init(&list.lock, NULL);
  112.  
  113. pthread_t dawid, hiwi1, hiwi2;
  114. pthread_create(&hiwi1, NULL, verifizieren, &list);
  115. pthread_create(&hiwi2, NULL, verifizieren, &list);
  116. pthread_create(&dawid, NULL, erstellen, &list);
  117.  
  118. pthread_join(dawid, NULL);
  119. pthread_join(hiwi1, NULL);
  120. pthread_join(hiwi2, NULL);
  121.  
  122. // Das Programm funktioniert korrekt, falls am Ende nur der SENTINEL Wert in der Liste zu finden ist.
  123. printf("List length: %d\n", list.length);
  124. printf("Last element == SENTINEL: %s\n", remove_at(&list, 0) == SENTINEL ? "yes" : "no");
  125. clear(&list);
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement