Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <stdio.h>
- #include <stdlib.h>
- int fibCount; // Size of fibonacci sequence
- int fibSum; // Fib values
- pthread_attr_t attr;
- int fibSeq(int x){
- if (x <= 1) {
- return 1;
- }
- return fibSeq(x-1) + fibSeq(x-2);
- }
- void *runs(void *param)
- {
- fibSum = fibSeq((int)param);
- pthread_exit(0);
- }
- int main(int argc, const char * argv[]) {
- //Validates argc
- if(argc != 2){
- return -1;
- }
- //Validates argv
- if (atoi(argv[1]) < 1)
- {
- printf("%d must be>1\n", atoi(argv[1]));
- return -1;
- }
- fibCount = atoi(argv[1]);
- pthread_attr_init(&attr);
- for(int i=1;i<=fibCount;i++){
- pthread_t thread;
- pthread_create(&thread,&attr,runs,(void*)i);
- pthread_join(thread,NULL);
- if(i < fibCount){
- printf("%d, ", fibSum);
- } else {
- printf("%d\n", fibSum);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement