/* thread2.c */ #include #include #include #include #include #define BUF 255 #define MAX_X 1024 static pthread_cond_t cond; pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER; typedef struct { long a; long n; long x[MAX_X]; long erg; long sum; }data_t; data_t input; void *multi(void *arg); void *add(void *arg); void *multi(void *arg) { int status; long temp; fprintf(stdout, "Thread multi gestartet: %ld\n", pthread_self()); status = pthread_mutex_lock(&mutex); fprintf(stdout, "Daten multi gelockt\n"); if (status != 0) { fprintf (stdout, "Fehler bei lock in Thread:%ld\n",pthread_self()); exit (EXIT_FAILURE); } temp = (long)arg; input.erg = input.a * temp; fprintf(stdout, "Thread multi temp = %ld\n", temp); fprintf(stdout, "Thread multi erg = %ld\n", input.erg); fprintf(stdout, "Thread multi sendet condition\n"); pthread_cond_signal(&cond); status = pthread_mutex_unlock(&mutex); fprintf(stdout, "Daten multi unlocked\n"); if (status != 0) { fprintf (stdout, "Error on mutex_unlock with Thread%ld\n", pthread_self()); exit (EXIT_FAILURE); } fprintf(stdout, "Thread multi beendet: %ld\n", pthread_self()); pthread_exit ((void *) 0); } void *add(void *arg) { int status; fprintf (stdout, "Thread add : %ld wartet auf die Condition Variable\n", pthread_self ()); pthread_cond_wait (&cond, &mutex); fprintf (stdout, "Thread add: %ld gestartet ...\n", pthread_self ()); status = pthread_mutex_lock(&mutex); fprintf(stdout, "Daten add gelockt\n"); if (status != 0) { fprintf (stdout, "Fehler bei lock in Thread:%ld\n",pthread_self()); exit (EXIT_FAILURE); } input.sum = input.sum + input.erg; status = pthread_mutex_unlock(&mutex); fprintf(stdout, "Daten add unlocked\n"); if (status != 0) { fprintf (stdout, "Error on mutex_unlock with Thread%ld\n", pthread_self()); exit (EXIT_FAILURE); } fprintf(stdout, "Thread multi beendet\n"); pthread_exit ((void *) 0); } int thread_start(long max_threads) { pthread_t th[max_threads+1]; int i; fprintf(stdout, "input.n = %ld", max_threads); for (i = 0; i < max_threads; i++) { if(pthread_create(&th[i], NULL, &multi, &input.x[i]) != 0) { fprintf (stderr, "Error creating Thread %d ...\n",i+1); return EXIT_FAILURE; } if(pthread_create(&th[i+1], NULL, &add, NULL) != 0) { fprintf (stderr, "Error creating Thread %d ...!\n", i+2); return EXIT_FAILURE; } for (i=0; i < max_threads; i++) { pthread_join(th[i], NULL); pthread_join(th[i+1], NULL); fprintf(stdout, "pthread_join %d", i); } } return (0); } int main (int argc, char *argv[]) { FILE *fp; char *token = NULL; char delim[] = " "; char string[BUF]; int i=0; long temp; input.erg = 0; input.sum = 0; if(argc < 2) { fprintf(stderr, "Usage: %s file\n", argv[0]); return (EXIT_FAILURE); } if( (fp = fopen(argv[1], "r") ) == NULL) { fprintf(stderr, "%s: File '%s' couldn't be opened!\n", argv[0], argv[1]); return (EXIT_FAILURE); } if( fgets(string, BUF, fp) != NULL) { token = strtok(string, delim); input.a = strtol(token, NULL, 10); token = strtok(NULL, delim); input.n = strtol(token, NULL, 10); } while(fgets(string, BUF, fp) != NULL) { temp = strtol(string, NULL, 10); input.x[i] = temp; fprintf(stdout, "input.x[%d] = %ld\n", i, input.x[i]); i++; } if (i < input.n) { fprintf(stderr, "%s: Not enough entries for x in file!", argv[0]); return EXIT_FAILURE; } thread_start(input.n); fprintf(stdout, "a = %ld, n = %ld, Summe = %ld\n", input.a, input.n, input.sum); fclose(fp); return (0); }