SHARE
TWEET

Zad4

LazySenpai Nov 24th, 2019 130 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <math.h>
  6.  
  7. typedef struct argument{
  8.     double n;
  9.     double k;
  10. }arg;
  11.  
  12. double newton(double n, double k) {
  13.     if(k == 0 || k == n)
  14.         return 1;
  15.     else
  16.         return newton(n-1, k-1) + newton(n-1, k);
  17. }
  18.  
  19. void* newton_use(void* _arg) {
  20.     arg *input = (arg*) _arg;
  21.     double *output = malloc(sizeof(double));
  22.     *output = newton(input->n,input->k);
  23.     pthread_exit((void*) output);
  24. }
  25.  
  26. void* czekaj() {
  27.     char czekaj[6]={'c','z','e','k','a','j'};
  28.     for(int i=0; i<6; i++) {
  29.         printf("%c ",czekaj[i]);
  30.         fflush(stdout);
  31.         usleep(300000);
  32.     }
  33. }
  34.  
  35. int main(int argc, char* argv[]) {
  36.     arg input;
  37.     double *result;
  38.     input.n=atoi(argv[1]);
  39.     input.k=atoi(argv[2]);
  40.     pthread_t newton_thread;
  41.     pthread_t czekaj_thread;
  42.     pthread_create(&newton_thread,NULL,newton_use, &input);
  43.     pthread_create(&czekaj_thread,NULL,czekaj, NULL);
  44.     pthread_join(czekaj_thread, NULL);
  45.     pthread_join(newton_thread, (void**)&result);
  46.  
  47.     printf("\n Wynik = %f", *result);
  48.     return 0;
  49. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top