/****************************/ /* Subway.c */ /*Ubahn Simulations Programm*/ /* von Philipp Klenkhart */ /* V 1.0 21.5.05 */ /****************************/ #include #include #include #include #include #include #define LINE_LEN 255 pthread_cond_t cond; pthread_mutex_t mutex; typedef struct timetable{ double distance; double speed; double time; }timetable_t; timetable_t line[4]; double position[4]; int stopit = 0; int counter; /*Funktion zum starten der Threads*/ void *subwaytohell(void *arg) { int status; int index = counter; /**(int *)arg*/; counter++; fprintf(stdout, "Subway %d: goin to work: %ld\n", index+1, pthread_self()); fprintf(stdout, "speed before mutex %lf: workin\n", line[index].speed); while(position[index] <= 500) { if(stopit == 0) { status = pthread_mutex_lock(&mutex); if (status != 0) { fprintf (stdout, "Error on mutex_unlock with Thread%ld\n", pthread_self()); exit (EXIT_FAILURE); } position[index] = position[index] + line[index].speed; fprintf(stdout, "Subway %lf: workin %ld\n", line[index].speed, pthread_self()); status = pthread_mutex_unlock(&mutex); if (status != 0) { fprintf (stdout, "Error on mutex_unlock with Thread%ld\n", pthread_self()); exit (EXIT_FAILURE); } sleep(1); } else { fprintf(stdout, "Subway %d: waitin\n", index); pthread_cond_wait(&cond, &mutex); } } if(position[index] >= 500 && position[index] < line[index].distance) { fprintf(stdout, "Subway %d: reached critical section\n", index); stopit = 1; while(position [index] <= 700) { status = pthread_mutex_lock(&mutex); if (status != 0) { fprintf (stdout, "Error on mutex_unlock with Thread%ld\n", pthread_self()); exit (EXIT_FAILURE); } position[index] += line[index].speed; status = pthread_mutex_unlock(&mutex); if (status != 0) { fprintf (stdout, "Error on mutex_unlock with Thread%ld\n", pthread_self()); exit (EXIT_FAILURE); } sleep(1); } stopit = 0; pthread_cond_signal(&cond); } while(position[index] <= line[index].distance) { pthread_mutex_lock(&mutex); position[index] += line[index].speed; pthread_mutex_unlock(&mutex); sleep(1); } if(position[index] == line[index].distance) { fprintf(stdout, "Destination Reached\n"); pthread_exit((void *)0);line[index].time = line[index].distance / line[index].speed; } return (0); } int thread_start() { pthread_t th[4]; int i; counter=0; for (i = 0; i < 4; i++) {fprintf(stdout, "i: %d\n",i); if(pthread_create(&th[i], NULL, subwaytohell, NULL) != 0) { fprintf (stderr, "Error creating Thread %d ...\n",i+1); return EXIT_FAILURE; } } /*Es wird für jeden Thread gewartet bis er beendet hat*/ for( i=0; i <= 3; i++) { pthread_join(th[i], NULL); } fprintf(stdout, "All Subways reached their Destinatons!\n"); return (0); } int main(int argc, char *argv[]) { FILE *fp; char buffer[LINE_LEN]; int i; if(argc < 1){ /*usage();*/ return EXIT_FAILURE; } if( (fp=fopen(argv[1], "r")) == NULL){ fprintf(stdout, "File couldn't be opened!\n"); return EXIT_FAILURE; } fprintf(stdout, "reading file\n"); for(i = 0; i < 3; i++) { if(fgets(buffer, LINE_LEN -1, fp) == NULL) { fprintf(stderr, "%s: invalid file layout %s\n", argv[0], argv[1]); return EXIT_FAILURE; } sscanf(buffer,"%lf %lf", &line[i].distance, &line[i].speed); fprintf(stdout, "speed read: %lf\n", line[i].speed); line[i].speed = line[i].speed/3.6; fprintf(stdout, "speed math: %lf\n", line[i].speed); line[i].time = line[i].distance / line[i].speed; } thread_start(); return (0); }