-
-
Save evscott/970401fb4518bd21575680317c270d48 to your computer and use it in GitHub Desktop.
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 <time.h> | |
#include <unistd.h> | |
#include <pthread.h> | |
const int CW_TIME = 10000000; | |
// Threaded task | |
void *childsWork(void *param) { | |
int *args = (int*)param; | |
int ID = args[0]; | |
// Simulate the random length of time a process could take | |
int length = rand() % CW_TIME; | |
int l = length; | |
while (--l > 0); | |
printf("{ Child: %d, Thread ID: %u, Task length: %d }\n", ID, pthread_self(), length); | |
} | |
int main(int argc, char **argv) { | |
int err; | |
srand(time(NULL)); | |
// Get stdin | |
int numThreads; | |
char c; | |
if (err = sscanf(argv[1], "%d %c", &numThreads, &c) != 1) { | |
printf("Failed to provide integer argument, err code %d\n", err); | |
return -1; | |
} | |
// Run threads | |
int i; | |
pthread_t *thread_id = (pthread_t *)malloc(numThreads * sizeof(pthread_t)); | |
int *index = (int *)malloc(numThreads * sizeof(int)); | |
for (i = 0; i < numThreads; i++) { | |
index[i] = i; | |
if (err = (pthread_create(&thread_id[i], NULL, childsWork, &index[i]) == 1)) { | |
printf("Error creating thread, err code: %d\n", err); | |
return -1; | |
} | |
} | |
for (i = 0; i < numThreads; i++) { | |
if (err = pthread_join(thread_id[i], NULL) == 1) { | |
printf("Error joining thread, err code: %d\n", err); | |
return -1; | |
} | |
} | |
free(thread_id); | |
free(index); | |
return 0; | |
} |
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 <time.h> | |
#include <unistd.h> | |
#include <pthread.h> | |
pthread_barrier_t barrier; | |
// Threaded task | |
void *childsWork(void *param) { | |
int *args = (int*)param; | |
int ID = args[0]; | |
printf("p1 -- { Child: %d, TID: %u }\n", ID, pthread_self()); | |
pthread_barrier_wait(&barrier); | |
printf("{ Child: %d, TID: %u } -- 2\n", ID, pthread_self()); | |
} | |
int main(int argc, char **argv) { | |
int err; | |
// Get stdin | |
int numThreads; | |
char c; | |
if (err = sscanf(argv[1], "%d %c", &numThreads, &c) != 1) { | |
printf("Failed to provide integer argument, err code: %d\n", err); | |
return -1; | |
} | |
pthread_barrier_init(&barrier, NULL, numThreads); | |
// Run threads | |
int i; | |
pthread_t *thread_id = (pthread_t *)malloc(numThreads *sizeof(pthread_t)); | |
int *index = (int *)malloc(numThreads * sizeof(int)); | |
for (i = 0; i < numThreads; i++) { | |
index[i] = i; | |
if (err = pthread_create(&thread_id[i], NULL, childsWork, &index[i]) == 1) { | |
printf("Error creating thread, err code: %d\n", err); | |
return -1; | |
} | |
} | |
for (i = 0; i < numThreads; i++) { | |
if (err = pthread_join(thread_id[i], NULL) == 1) { | |
printf("Error joining thread, err code: %d\n", err); | |
return -1; | |
} | |
} | |
pthread_barrier_destroy(&barrier); | |
free(thread_id); | |
free(index); | |
return 0; | |
} |
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 <time.h> | |
#include <unistd.h> | |
#include <pthread.h> | |
pthread_barrier_t barrier; | |
int A[] = {1, 1}; | |
// Threaded task 1 | |
void *backend(void *unused) { | |
for (int i = 0; i < 10; i++) { | |
pthread_barrier_wait(&barrier); | |
pthread_barrier_wait(&barrier); | |
// Calculate the next two numbers of the fibonacci sequence | |
int x = A[0] + A[1], y = A[1] + x;; | |
A[0] = x; | |
A[1] = y; | |
} | |
} | |
// Threaded task 2 | |
void *frontend(void *unused) { | |
for (int i = 0; i < 10; i++) { | |
pthread_barrier_wait(&barrier); | |
printf("{ %d, %d }\n", A[0], A[1]); | |
pthread_barrier_wait(&barrier); | |
} | |
} | |
int main(int argc, char **argv) { | |
int err; | |
pthread_barrier_init(&barrier, NULL, 2); | |
// Run threads | |
pthread_t *backend_tid = (pthread_t *) malloc(sizeof(pthread_t)); | |
pthread_t *frontend_tid = (pthread_t *) malloc(sizeof(pthread_t)); | |
int null; | |
if ( err = pthread_create(backend_tid, NULL, backend, &null) == 1) { | |
printf("Error creating thread, err code: %d\n", err); | |
return -1; | |
} | |
if ( err = pthread_create(frontend_tid, NULL, frontend, &null) == 1) { | |
printf("Error creating thread, err code: %d\n", err); | |
return -1; | |
} | |
if (err = pthread_join(*backend_tid, NULL) == 1) { | |
printf("Error joining thread, err code: %d %s\n", err); | |
return -1; | |
} | |
if (err = pthread_join(*frontend_tid, NULL) == 1) { | |
printf("Error joining thread, err code: %d\n", err); | |
return -1; | |
} | |
pthread_barrier_destroy(&barrier); | |
free(backend_tid); | |
free(frontend_tid); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment