|
@@ -1,35 +1,86 @@
|
|
| 1 |
// Copyright 2021 Jeisson Hidalgo <jeisson.hidalgo@ucr.ac.cr> CC-BY 4.0
|
| 2 |
|
|
|
|
|
|
|
| 3 |
#include <pthread.h>
|
|
|
|
| 4 |
#include <stdio.h>
|
| 5 |
#include <stdlib.h>
|
| 6 |
-
|
| 7 |
|
| 8 |
/**
|
| 9 |
* @brief ...
|
| 10 |
*/
|
| 11 |
void* greet(void* data);
|
|
|
|
| 12 |
|
| 13 |
-
// procedure main
|
| 14 |
-
int main(
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
if (error == EXIT_SUCCESS) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
// print "Hello from main thread"
|
| 20 |
-
// usleep(1); // indeterminism
|
| 21 |
printf("Hello from main thread\n");
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
} else {
|
| 24 |
-
fprintf(stderr, "Error: could not
|
|
|
|
|
|
|
| 25 |
}
|
|
|
|
| 26 |
return error;
|
| 27 |
-
}
|
| 28 |
|
| 29 |
// procedure greet:
|
| 30 |
void* greet(void* data) {
|
| 31 |
-
(
|
|
|
|
| 32 |
// print "Hello from secondary thread"
|
| 33 |
-
printf("Hello from secondary thread\n");
|
| 34 |
return NULL;
|
| 35 |
} // end procedure
|
| 1 |
// Copyright 2021 Jeisson Hidalgo <jeisson.hidalgo@ucr.ac.cr> CC-BY 4.0
|
| 2 |
|
| 3 |
+
#include <assert.h>
|
| 4 |
+
#include <inttypes.h>
|
| 5 |
#include <pthread.h>
|
| 6 |
+
#include <stdint.h>
|
| 7 |
#include <stdio.h>
|
| 8 |
#include <stdlib.h>
|
| 9 |
+
#include <unistd.h>
|
| 10 |
|
| 11 |
/**
|
| 12 |
* @brief ...
|
| 13 |
*/
|
| 14 |
void* greet(void* data);
|
| 15 |
+
int create_threads(uint64_t thread_count);
|
| 16 |
|
| 17 |
+
// procedure main(argc, argv[])
|
| 18 |
+
int main(int argc, char* argv[]) {
|
| 19 |
+
#if 0
|
| 20 |
+
for (int index = 0; index < argc; ++index) {
|
| 21 |
+
printf("argv[%d] = '%s'\n", index, argv[index]);
|
| 22 |
+
}
|
| 23 |
+
return 0;
|
| 24 |
+
#endif
|
| 25 |
+
|
| 26 |
+
int error = EXIT_SUCCESS;
|
| 27 |
+
// create thread_count as result of converting argv[1] to integer
|
| 28 |
+
// thread_count := integer(argv[1])
|
| 29 |
+
uint64_t thread_count = sysconf(_SC_NPROCESSORS_ONLN);
|
| 30 |
+
if (argc == 2) {
|
| 31 |
+
if (sscanf(argv[1], "%" SCNu64, &thread_count) == 1) {
|
| 32 |
+
} else {
|
| 33 |
+
fprintf(stderr, "Error: invalid thread count\n");
|
| 34 |
+
return 11;
|
| 35 |
+
}
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
error = create_threads(thread_count);
|
| 39 |
+
return error;
|
| 40 |
+
} // end procedure
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
int create_threads(uint64_t thread_count) {
|
| 44 |
+
int error = EXIT_SUCCESS;
|
| 45 |
+
// for thread_number := 0 to thread_count do
|
| 46 |
+
pthread_t* threads = (pthread_t*) malloc(thread_count * sizeof(pthread_t));
|
| 47 |
+
if (threads) {
|
| 48 |
+
for (uint64_t thread_number = 0; thread_number < thread_count
|
| 49 |
+
; ++thread_number) {
|
| 50 |
+
// create_thread(greet, thread_number)
|
| 51 |
+
error = pthread_create(&threads[thread_number], /*attr*/ NULL, greet
|
| 52 |
+
, /*arg*/ (void*) thread_number);
|
| 53 |
if (error == EXIT_SUCCESS) {
|
| 54 |
+
} else {
|
| 55 |
+
fprintf(stderr, "Error: could not create secondary thread\n");
|
| 56 |
+
error = 21;
|
| 57 |
+
break;
|
| 58 |
+
}
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
// print "Hello from main thread"
|
|
|
|
| 62 |
printf("Hello from main thread\n");
|
| 63 |
+
|
| 64 |
+
for (uint64_t thread_number = 0; thread_number < thread_count
|
| 65 |
+
; ++thread_number) {
|
| 66 |
+
pthread_join(threads[thread_number], /*value_ptr*/ NULL);
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
free(threads);
|
| 70 |
} else {
|
| 71 |
+
fprintf(stderr, "Error: could not allocate %" PRIu64 " threads\n"
|
| 72 |
+
, thread_count);
|
| 73 |
+
error = 22;
|
| 74 |
}
|
| 75 |
+
|
| 76 |
return error;
|
| 77 |
+
}
|
| 78 |
|
| 79 |
// procedure greet:
|
| 80 |
void* greet(void* data) {
|
| 81 |
+
// assert(data);
|
| 82 |
+
const uint64_t rank = (uint64_t) data;
|
| 83 |
// print "Hello from secondary thread"
|
| 84 |
+
printf("Hello from secondary thread %" PRIu64 "\n", rank);
|
| 85 |
return NULL;
|
| 86 |
} // end procedure
|