Created
May 3, 2017 15:06
-
-
Save morsoinferno/4c39ef611baa41288670b3fb3b576cbd to your computer and use it in GitHub Desktop.
codigo_control_3.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <pthread.h> | |
#define MAX_THREADS 5 | |
int i, sum; | |
enum {THREAD_FAIL, THREAD_SUCCESS}; | |
void *runner(void *param){ | |
int limit = atoi(param); | |
int i, check; | |
sum=0; check=0; | |
if (limit > 0){ | |
for (i = 1; i <= limit; i++){ | |
sum += i; | |
check += i; | |
} | |
} | |
if (check == sum) pthread_exit((void *) THREAD_SUCCESS); | |
else pthread_exit((void *) THREAD_FAIL); | |
} | |
char* statusToString (int status){ | |
return status == THREAD_SUCCESS ? "success!":"fail"; | |
} | |
int main(int argc, char *argv[]) { | |
pthread_t threads[MAX_THREADS]; | |
int status; | |
for (i=0;i<MAX_THREADS;i++) | |
pthread_create(&threads[i], NULL, (void*)runner, (void *) argv[1]); | |
for (i=0;i<MAX_THREADS;i++){ | |
pthread_join(threads[i], (void **)&status); | |
printf("sum = %d, status = %s\n", sum, statusToString(status)); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment