Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int fibCount; // Size of fibonacci sequence
  6. int fibSum; // Fib values
  7. pthread_attr_t attr;
  8.  
  9. int fibSeq(int x){
  10. if (x <= 1) {
  11. return 1;
  12. }
  13. return fibSeq(x-1) + fibSeq(x-2);
  14. }
  15.  
  16. void *runs(void *param)
  17. {
  18. fibSum = fibSeq((int)param);
  19. pthread_exit(0);
  20. }
  21.  
  22. int main(int argc, const char * argv[]) {
  23.  
  24. //Validates argc
  25. if(argc != 2){
  26. return -1;
  27. }
  28. //Validates argv
  29. if (atoi(argv[1]) < 1)
  30. {
  31. printf("%d must be>1\n", atoi(argv[1]));
  32. return -1;
  33. }
  34. fibCount = atoi(argv[1]);
  35. pthread_attr_init(&attr);
  36. for(int i=1;i<=fibCount;i++){
  37. pthread_t thread;
  38. pthread_create(&thread,&attr,runs,(void*)i);
  39. pthread_join(thread,NULL);
  40. if(i < fibCount){
  41. printf("%d, ", fibSum);
  42. } else {
  43. printf("%d\n", fibSum);
  44. }
  45. }
  46. return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement