Advertisement
szmelu

systemy2

May 15th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <stdio.h>
  5. #include <pthread.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <cmath>
  9.  
  10. using namespace std;
  11.  
  12. double tab[3];
  13. void* sinus(void* arg)
  14. {
  15. double *y = (double*) arg;
  16. double x = *y;
  17. double ret = sin(x);
  18. tab[0]=ret;
  19. sleep(pthread_self());
  20. cout<<"sinx = "<<ret<<endl;
  21. pthread_exit(0);
  22. return 0;
  23. }
  24. void* ln(void* arg)
  25. {
  26. double *y = (double*) arg;
  27. double x = *y;
  28. double ret = log(x);
  29. tab[1]=ret;
  30. sleep(pthread_self());
  31. cout<<"lnx = "<<ret<<endl;
  32. pthread_exit(0);
  33. return 0;
  34. }
  35. void* pierw(void* arg)
  36. {
  37. double *y = (double*) arg;
  38. double x = *y;
  39. double ret = sqrt(x);
  40. sleep(pthread_self());
  41. tab[2]=ret;
  42. cout<<"pierwx = "<<ret<<endl;
  43. pthread_exit(0);
  44. return 0;
  45. }
  46.  
  47. int main(int argc, char **argv)
  48. {
  49. pthread_t id[3];
  50. double suma=0;
  51.  
  52. double x=0;
  53.  
  54. if(argc < 2 || (x=atof(argv[1])) <= 0)
  55. {
  56. cout<<"Blad"<<endl;
  57. return 0;
  58. }
  59. pthread_create(&id[0], NULL, sinus, &x);
  60. pthread_create(&id[1], NULL, ln, &x);
  61. pthread_create(&id[2], NULL, pierw, &x);
  62. pthread_join(id[0], NULL);
  63. pthread_join(id[1], NULL);
  64. pthread_join(id[2], NULL);
  65. suma = tab[0] + tab[1] + tab[2];
  66. cout<<"suma: "<<suma<<endl;
  67. return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement