Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "aufgabe2.h"
- #include <pthread.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <string.h>
- #include <errno.h>
- #include <ctype.h>
- #include <stdlib.h>
- #define ANZAHL_TEAMS 4
- #define ANZAHL_RUNDEN 10
- /* Diesen Code nicht modifizieren */
- struct Team teams[] = {{.name = "The Runtime Exceptions", .runtime = 1, .staffelstab = 1, .hand = 1},
- {.name = "Ueberholverbot", .runtime = 2, .staffelstab = 2, .hand = 2},
- {.name = "WiSo nur", .runtime = 5, .staffelstab = 3, .hand = 3},
- {.name = "Die laufenden Kopplungen", .runtime = 3, .staffelstab = 4, .hand = 4}};
- /* Sockel zum Halten des Staffelstabs */
- staffelstab_t sockel;
- /* Ab hier euren Code einfuegen */
- #define handle_error_en(en, msg) \
- do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
- #define handle_error(msg) \
- do { perror(msg); exit(EXIT_FAILURE); } while (0)
- #define true 1
- #define false 0
- typedef unsigned short bool;
- typedef bool Lock;
- Lock lock;
- void aquire( Lock* lock )
- {
- while( *lock ) { printf( "warte auf lock...\n" ); }
- *lock = true;
- }
- void release( Lock* lock )
- {
- *lock = false;
- }
- struct thread_info_t
- {
- pthread_t tid;
- int tnum;
- struct Team* team;
- };
- static void* CreateThread( void* arg )
- {
- struct thread_info_t* threadinfo = arg; /* muss ja an die teamdaten kommen ;) */
- struct Team* tinfo = threadinfo->team;
- printf( "\nTeamName: %s\n", tinfo->name );
- int i;
- for( i = 1; i < ANZAHL_RUNDEN; i++ )
- {
- if( i > 1 )
- sleep( 1 ); /* zum sockel laufen */
- if( tinfo->hand == LEER )
- {
- if( sockel == LEER )
- {
- printf( "Team '%s' disqualifiziert, FML...\n", tinfo->name );
- return NULL;
- }
- else
- {
- tinfo->hand = sockel;
- sockel = LEER;
- }
- }
- sleep( tinfo->runtime ); /* runde laufen */
- if( i < ANZAHL_RUNDEN ) /* stab abstellen */
- {
- aquire( &sockel );
- sockel = tinfo->hand;
- tinfo->hand = LEER;
- release( &sockel );
- }
- printf( "Team '%s' hat die %d. Runde beendet.\n", tinfo->name, i );
- }
- printf( "Team '%s' ist fertig; hat (hand): %d, sollte haben (staffelstab): %d\n\n", tinfo->name, tinfo->hand, tinfo->staffelstab );
- return NULL; /* wird ja nicht benötigt */
- }
- int main(void)
- {
- /* Zu Beginn "leeren" wir mal den Sockel */
- sockel = LEER;
- int ret;
- ret = 0;
- struct thread_info_t* tinfo;
- pthread_attr_t attr;
- void* out;
- ret = pthread_attr_init( &attr );
- if( ret != 0 )
- handle_error_en( ret, "pthread_attr_init" );
- tinfo = calloc( ANZAHL_TEAMS, sizeof( struct thread_info_t ) );
- int i, j;
- for( i = 0; i < ANZAHL_TEAMS; i++ )
- {
- tinfo[i].tnum = i + 1; /* threadnumber verpasen, nicht wichtig aber ich habs gern falls mans doch mal braucht */
- tinfo[i].team = &teams[i]; /* team zuweisen */
- ret = pthread_create( &tinfo[i].tid, &attr, &CreateThread, &tinfo[i] );
- if( ret != 0 )
- handle_error_en( ret, "pthread_create" );
- }
- ret = pthread_attr_destroy( &attr );
- if( ret != 0 )
- handle_error_en( ret, "pthread_attr_destroy" );
- for( j = 0; j < ANZAHL_TEAMS; j++ )
- {
- ret = pthread_join( tinfo[j].tid, &out );
- if( ret != 0 )
- handle_error_en( ret, "pthread_join" );
- printf( "Im thread mit id %d; return: %s\n", tinfo[j].tid, (char*)out );
- free( out );
- }
- free( tinfo ); /* cleanup */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement