Advertisement
Karim_Gabr

Untitled

Mar 31st, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6.  
  7. #define ARRAY_SIZE 5
  8.  
  9. pthread_t THREAD_ID[5];
  10.  
  11. struct StructMax
  12. {
  13.     int ARRAY[ARRAY_SIZE];
  14.     int LEFT;
  15.     int RIGHT;
  16. };
  17.  
  18.  
  19.  
  20. static int i = 0;
  21. static int FLAG_FUNCTION_CALLS = 0;
  22. static int FLAG_THREAD_ID = 0;
  23.  
  24. void *MAX_VALUE(struct StructMax *S_M)
  25. {
  26.     int ARRAY[ARRAY_SIZE];
  27.     ARRAY = S_M->ARRAY;
  28.     int *LEFT,*RIGHT;
  29.     &LEFT = S_M->LEFT;
  30.     &RIGHT = S_M->RIGHT;
  31.     int MID;
  32.  
  33.  
  34.     int ERR;
  35.     FLAG_FUNCTION_CALLS++;
  36.  
  37.     int ACTIVE_THREAD_ID = pthread_self();
  38.  
  39.  
  40.     if(&LEFT == &RIGHT)
  41.     {
  42.         if(FLAG_THREAD_ID <= 4)
  43.         {
  44.             printf("THREAD[%d] roger that and moving out...",ACTIVE_THREAD_ID);
  45.             pthread_exit(&ARRAY[&LEFT]);
  46.         }
  47.         else
  48.         {
  49.             return (void *) ARRAY[S_M->LEFT];
  50.         }
  51.  
  52.     }
  53.     else
  54.     {
  55.         MID = (&LEFT + &RIGHT) / 2;
  56.         void *U;
  57.         if(FLAG_FUNCTION_CALLS <= 4)
  58.         {
  59.             FLAG_THREAD_ID++;
  60.             ERR = pthread_create(&(THREAD_ID[FLAG_THREAD_ID]), NULL, &MAX_VALUE(S_M), NULL);
  61.             if (ERR != 0)
  62.                 fprintf(stderr, "\nFAILED CREATE THREAD!");
  63.             else
  64.                 printf("\n Thread created successfully\n");
  65.             pthread_join(&THREAD_ID[FLAG_THREAD_ID],&U);
  66.         }
  67.  
  68.         void *V;
  69.         if(FLAG_FUNCTION_CALLS <= 4)
  70.         {
  71.             FLAG_THREAD_ID++;
  72.             ERR = pthread_create(&(THREAD_ID[FLAG_THREAD_ID]), NULL, &MAX_VALUE(S_M), NULL);
  73.             if (ERR != 0)
  74.                 fprintf(stderr, "\nFAILED CREATE THREAD!");
  75.             else
  76.                 printf("\n Thread created successfully\n");
  77.             pthread_join(&THREAD_ID[FLAG_THREAD_ID],&V);
  78.         }
  79.  
  80.         if(FLAG_THREAD_ID <= 4)
  81.         {
  82.             printf("THREAD[%d] roger that and moving out...",ACTIVE_THREAD_ID);
  83.             pthread_exit(U > V ? &U : &V);
  84.         }
  85.         else
  86.         {
  87.             return (U > V ? (void *)U : (void *)V);
  88.         }
  89.     }
  90. }
  91.  
  92. int main()
  93. {
  94.     int N,i,ERR;
  95.     void *MAXIMUM_VALUE;
  96.  
  97.     struct StructMax MY_STRUCT;
  98.  
  99.     int BUFFER[ARRAY_SIZE];
  100.     printf("\nEnter Array values : \n");
  101.     for(i = 0; i < N; i++)
  102.         scanf("%d", &BUFFER[i]);
  103.  
  104.     int *LEFT, *RIGHT;
  105.     MY_STRUCT->ARRAY = BUFFER;
  106.     MY_STRUCT->LEFT = 0;
  107.     MY_STRUCT->RIGHT = ARRAY_SIZE-1;
  108.  
  109.     ERR = pthread_create(&(THREAD_ID[0]), NULL, &MAX_VALUE(MY_STRUCT), NULL);
  110.     if (ERR != 0)
  111.         fprintf(stderr, "\nFAILED CREATE THREAD!");
  112.     else
  113.         printf("\n Thread created successfully\n");
  114.  
  115.     pthread_join(&THREAD_ID[4],&MAXIMUM_VALUE);
  116.     printf("\n%d\nNumber of time the recursive function is called: %d", (int)MAXIMUM_VALUE,FLAG_FUNCTION_CALLS);
  117.  
  118.     return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement