rendered paste body/****************************//* Subway.c *//*Ubahn Simulations Programm*//* V 1.0 21.5.05 *//****************************/#include <stdio.h>#include <stdlib.h>#include <pthread.h>#include <string.h>#include <time.h>#include <unistd.h>#define LINE_LEN 12pthread_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; fprintf(stdout, "Subway %d: goin to work\n", index); 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] += line[index].speed; fprintf(stdout, "Subway %lf: workin\n", 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); } 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++) { if(pthread_create(&th[i], NULL, &subwaytohell, NULL) != 0) { fprintf (stderr, "Error creating Thread %d ...\n",i+1); return EXIT_FAILURE; } counter++; } /*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: %lf", line[i].speed); line[i].speed = line[i].speed/3.6;fprintf(stdout, "speed: %lf", line[i].speed); line[i].time = line[i].distance / line[i].speed; } thread_start();return (0);}