Advertisement
arczi316

Untitled

Nov 21st, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.22 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <pthread.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. #define SIZE 9
  7.  
  8. struct Symbol
  9. {
  10.     int n, k;
  11. };
  12.  
  13. unsigned long long newton(int n, int long k)
  14. {
  15.     if (k == 0 || k == n)
  16.         return 1;
  17.  
  18.     if (k > 0 && k < n)
  19.         return newton(n - 1, k - 1) + newton(n - 1, k);
  20.  
  21.     return 0;
  22. }
  23.  
  24. void *funkcja1(void *args) {
  25.     struct Symbol *s = (struct Symbol*) args;
  26.     unsigned long long wynik = newton(s->n, s->k);
  27.     pthread_exit((void *)wynik);
  28. }
  29.  
  30. void *funkcja2(void *args) {
  31.     char *imie = (char *)args;
  32.     int i = 0;
  33.     while (1)
  34.     {
  35.         printf("%c", imie[i]);
  36.         fflush(stdout);
  37.  
  38.         if (i >= SIZE)
  39.             i = 0;
  40.         else
  41.             i++;
  42.        
  43.         usleep(1000 * 300);
  44.     }
  45.     phtread_exit(NULL);
  46. }
  47.  
  48. int main()
  49. {
  50.     int n, k;
  51.     const char *imie = "Arkadiusz";
  52.     printf("Podaj n: ");
  53.     scanf("%d", &n);
  54.     printf("Podaj k: ");
  55.     scanf("%d", &k);
  56.  
  57.     struct Symbol *sym = (struct Symbol *)malloc(sizeof(struct Symbol));
  58.     sym->n = n;
  59.     sym->k = k;
  60.  
  61.     pthread_t thread1, thread2;
  62.     void *wynik;
  63.    
  64.     pthread_create(&thread1, NULL, funkcja1, (void *)sym);
  65.     pthread_create(&thread2, NULL, funkcja2, (void *)imie);
  66.  
  67.     pthread_join(thread1, &wynik);
  68.     pthread_cancel(thread2);
  69.  
  70.     printf("\nWynik: %llu\n", (unsigned long long)wynik);
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement