Advertisement
szmelu

systemy

May 8th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 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.  
  13. void* sinus(void* arg)
  14. {
  15. double *y = (double*) arg;
  16. double x = *y;
  17. double ret = sin(x);
  18. sleep(pthread_self());
  19. cout<<"sinx = "<<ret<<endl;
  20. pthread_exit(0);
  21. return 0;
  22. }
  23. void* ln(void* arg)
  24. {
  25. double *y = (double*) arg;
  26. double x = *y;
  27. double ret = log(x);
  28. sleep(pthread_self());
  29. cout<<"lnx = "<<ret<<endl;
  30. pthread_exit(0);
  31. return 0;
  32. }
  33. void* pierw(void* arg)
  34. {
  35. double *y = (double*) arg;
  36. double x = *y;
  37. double ret = sqrt(x);
  38. sleep(pthread_self());
  39. cout<<"pierwx = "<<ret<<endl;
  40. pthread_exit(0);
  41. return 0;
  42. }
  43.  
  44. int main(int argc, char **argv)
  45. {
  46. pthread_t id[2];
  47. double x = atof(argv[1]);
  48. double *tab[2];
  49. double suma;
  50.  
  51. if(argc < 2 || x <= 0)
  52. {
  53. printf("Nie podano argumentu lub argument nie jest liczba dodatnia\n");
  54. return 0;
  55. }
  56. pthread_create(&id[1], NULL, sinus, &x);
  57. pthread_create(&id[2], NULL, ln, &x);
  58. pthread_create(&id[3], NULL, pierw, &x);
  59. pthread_join(id[1], (void**)&tab[0]);
  60. pthread_join(id[2], (void**)&tab[1]);
  61. pthread_join(id[3], (void**)&tab[2]);
  62. suma = *tab[0] + *tab[1] + *tab[2];
  63. cout<<"suma: "<<suma<<endl;
  64. return EXIT_SUCCESS;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement