Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <string.h>
- #include <unistd.h>
- #define ARRAY_SIZE 5
- pthread_t THREAD_ID[5];
- struct StructMax
- {
- int ARRAY[ARRAY_SIZE];
- int LEFT;
- int RIGHT;
- };
- static int i = 0;
- static int FLAG_FUNCTION_CALLS = 0;
- static int FLAG_THREAD_ID = 0;
- void *MAX_VALUE(struct StructMax *S_M)
- {
- int ARRAY[ARRAY_SIZE];
- ARRAY = S_M->ARRAY;
- int *LEFT,*RIGHT;
- &LEFT = S_M->LEFT;
- &RIGHT = S_M->RIGHT;
- int MID;
- int ERR;
- FLAG_FUNCTION_CALLS++;
- int ACTIVE_THREAD_ID = pthread_self();
- if(&LEFT == &RIGHT)
- {
- if(FLAG_THREAD_ID <= 4)
- {
- printf("THREAD[%d] roger that and moving out...",ACTIVE_THREAD_ID);
- pthread_exit(&ARRAY[&LEFT]);
- }
- else
- {
- return (void *) ARRAY[S_M->LEFT];
- }
- }
- else
- {
- MID = (&LEFT + &RIGHT) / 2;
- void *U;
- if(FLAG_FUNCTION_CALLS <= 4)
- {
- FLAG_THREAD_ID++;
- ERR = pthread_create(&(THREAD_ID[FLAG_THREAD_ID]), NULL, &MAX_VALUE(S_M), NULL);
- if (ERR != 0)
- fprintf(stderr, "\nFAILED CREATE THREAD!");
- else
- printf("\n Thread created successfully\n");
- pthread_join(&THREAD_ID[FLAG_THREAD_ID],&U);
- }
- void *V;
- if(FLAG_FUNCTION_CALLS <= 4)
- {
- FLAG_THREAD_ID++;
- ERR = pthread_create(&(THREAD_ID[FLAG_THREAD_ID]), NULL, &MAX_VALUE(S_M), NULL);
- if (ERR != 0)
- fprintf(stderr, "\nFAILED CREATE THREAD!");
- else
- printf("\n Thread created successfully\n");
- pthread_join(&THREAD_ID[FLAG_THREAD_ID],&V);
- }
- if(FLAG_THREAD_ID <= 4)
- {
- printf("THREAD[%d] roger that and moving out...",ACTIVE_THREAD_ID);
- pthread_exit(U > V ? &U : &V);
- }
- else
- {
- return (U > V ? (void *)U : (void *)V);
- }
- }
- }
- int main()
- {
- int N,i,ERR;
- void *MAXIMUM_VALUE;
- struct StructMax MY_STRUCT;
- int BUFFER[ARRAY_SIZE];
- printf("\nEnter Array values : \n");
- for(i = 0; i < N; i++)
- scanf("%d", &BUFFER[i]);
- int *LEFT, *RIGHT;
- MY_STRUCT->ARRAY = BUFFER;
- MY_STRUCT->LEFT = 0;
- MY_STRUCT->RIGHT = ARRAY_SIZE-1;
- ERR = pthread_create(&(THREAD_ID[0]), NULL, &MAX_VALUE(MY_STRUCT), NULL);
- if (ERR != 0)
- fprintf(stderr, "\nFAILED CREATE THREAD!");
- else
- printf("\n Thread created successfully\n");
- pthread_join(&THREAD_ID[4],&MAXIMUM_VALUE);
- printf("\n%d\nNumber of time the recursive function is called: %d", (int)MAXIMUM_VALUE,FLAG_FUNCTION_CALLS);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement