Sumana_19

Untitled

Nov 25th, 2021
531
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <pthread.h>
  5.  
  6. int printSum(void *x)
  7. {
  8.     int n = *((int *)x);
  9.     int sum = 0;
  10.  
  11.     for (int i = 1; i <= n; i++)
  12.     {
  13.         sum += i;
  14.     }
  15.  
  16.     return sum;
  17. }
  18.  
  19. int printPrime(void *x)
  20. {
  21.     int n = *((int *)x);
  22.     int count = 0;
  23.  
  24.     printf("Print n received %d\n", n);
  25.  
  26.     if (n >= 2)
  27.     {
  28.         printf("Prime number found: 2\n");
  29.         count++;
  30.     }
  31.  
  32.     for (int i = 3; i < n; i++)
  33.     {
  34.         int flag = 0;
  35.         for (int j = 2; j < i / 2; j++)
  36.         {
  37.             if (i % j == 0)
  38.             {
  39.                 flag = 1;
  40.                 break;
  41.             }
  42.         }
  43.         if (flag == 0)
  44.         {
  45.             printf("Prime number found: %d\n", i);
  46.             count++;
  47.         }
  48.  
  49.         return count;
  50.     }
  51. }
  52.  
  53. int main()
  54. {
  55.     printf("Enter the range:");
  56.     int n;
  57.     int rt_p1, rt_p2;
  58.     scanf("%d", &n);
  59.  
  60.     pthread_t ptSum, ptPrime;
  61.  
  62.     pthread_create(&ptSum, NULL, (void *)&printSum, (void *)&n);
  63.     pthread_create(&ptPrime, NULL, (void *)&printPrime, (void *)&n);
  64.     pthread_join(ptSum, (void **)&rt_p1);
  65.     printf("Main thread: Value returned from printSum %d\n", rt_p1);
  66.     pthread_join(ptPrime, (void **)&rt_p2);
  67.     printf("Main thread: Value returned from printPrime %d\n", rt_p2);
  68.  
  69.     return 0;
  70. }
RAW Paste Data