Advertisement
Guest User

Untitled

a guest
May 25th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. #include "aufgabe2.h"
  2. #include <pthread.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <errno.h>
  7. #include <ctype.h>
  8. #include <stdlib.h>
  9.  
  10. #define ANZAHL_TEAMS 4
  11. #define ANZAHL_RUNDEN 10
  12.  
  13.  
  14. /* Diesen Code nicht modifizieren */
  15. struct Team teams[] = {{.name = "The Runtime Exceptions", .runtime = 1, .staffelstab = 1, .hand = 1},
  16. {.name = "Ueberholverbot", .runtime = 2, .staffelstab = 2, .hand = 2},
  17. {.name = "WiSo nur", .runtime = 5, .staffelstab = 3, .hand = 3},
  18. {.name = "Die laufenden Kopplungen", .runtime = 3, .staffelstab = 4, .hand = 4}};
  19.  
  20. /* Sockel zum Halten des Staffelstabs */
  21. staffelstab_t sockel;
  22.  
  23. /* Ab hier euren Code einfuegen */
  24.  
  25. #define handle_error_en(en, msg) \
  26. do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
  27.  
  28. #define handle_error(msg) \
  29. do { perror(msg); exit(EXIT_FAILURE); } while (0)
  30.  
  31.  
  32. #define true 1
  33. #define false 0
  34.  
  35. typedef unsigned short bool;
  36. typedef bool Lock;
  37. Lock lock;
  38.  
  39. void aquire( Lock* lock )
  40. {
  41. while( *lock ) { printf( "warte auf lock...\n" ); }
  42.  
  43. *lock = true;
  44. }
  45.  
  46. void release( Lock* lock )
  47. {
  48. *lock = false;
  49. }
  50.  
  51. struct thread_info_t
  52. {
  53. pthread_t tid;
  54. int tnum;
  55. struct Team* team;
  56. };
  57.  
  58. static void* CreateThread( void* arg )
  59. {
  60. struct thread_info_t* threadinfo = arg; /* muss ja an die teamdaten kommen ;) */
  61. struct Team* tinfo = threadinfo->team;
  62.  
  63. printf( "\nTeamName: %s\n", tinfo->name );
  64.  
  65. int i;
  66. for( i = 1; i < ANZAHL_RUNDEN; i++ )
  67. {
  68. if( i > 1 )
  69. sleep( 1 ); /* zum sockel laufen */
  70.  
  71. if( tinfo->hand == LEER )
  72. {
  73. if( sockel == LEER )
  74. {
  75. printf( "Team '%s' disqualifiziert, FML...\n", tinfo->name );
  76.  
  77. return NULL;
  78. }
  79. else
  80. {
  81. tinfo->hand = sockel;
  82. sockel = LEER;
  83. }
  84. }
  85.  
  86. sleep( tinfo->runtime ); /* runde laufen */
  87.  
  88. if( i < ANZAHL_RUNDEN ) /* stab abstellen */
  89. {
  90. aquire( &sockel );
  91.  
  92. sockel = tinfo->hand;
  93. tinfo->hand = LEER;
  94.  
  95. release( &sockel );
  96. }
  97.  
  98. printf( "Team '%s' hat die %d. Runde beendet.\n", tinfo->name, i );
  99. }
  100.  
  101. printf( "Team '%s' ist fertig; hat (hand): %d, sollte haben (staffelstab): %d\n\n", tinfo->name, tinfo->hand, tinfo->staffelstab );
  102.  
  103. return NULL; /* wird ja nicht benötigt */
  104. }
  105.  
  106. int main(void)
  107. {
  108. /* Zu Beginn "leeren" wir mal den Sockel */
  109. sockel = LEER;
  110.  
  111. int ret;
  112. ret = 0;
  113.  
  114. struct thread_info_t* tinfo;
  115. pthread_attr_t attr;
  116. void* out;
  117.  
  118. ret = pthread_attr_init( &attr );
  119.  
  120. if( ret != 0 )
  121. handle_error_en( ret, "pthread_attr_init" );
  122.  
  123. tinfo = calloc( ANZAHL_TEAMS, sizeof( struct thread_info_t ) );
  124.  
  125. int i, j;
  126. for( i = 0; i < ANZAHL_TEAMS; i++ )
  127. {
  128. tinfo[i].tnum = i + 1; /* threadnumber verpasen, nicht wichtig aber ich habs gern falls mans doch mal braucht */
  129. tinfo[i].team = &teams[i]; /* team zuweisen */
  130.  
  131. ret = pthread_create( &tinfo[i].tid, &attr, &CreateThread, &tinfo[i] );
  132.  
  133. if( ret != 0 )
  134. handle_error_en( ret, "pthread_create" );
  135. }
  136.  
  137. ret = pthread_attr_destroy( &attr );
  138.  
  139. if( ret != 0 )
  140. handle_error_en( ret, "pthread_attr_destroy" );
  141.  
  142. for( j = 0; j < ANZAHL_TEAMS; j++ )
  143. {
  144. ret = pthread_join( tinfo[j].tid, &out );
  145.  
  146. if( ret != 0 )
  147. handle_error_en( ret, "pthread_join" );
  148.  
  149. printf( "Im thread mit id %d; return: %s\n", tinfo[j].tid, (char*)out );
  150.  
  151. free( out );
  152. }
  153.  
  154. free( tinfo ); /* cleanup */
  155. return 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement