Advertisement
STANAANDREY

bin src multi

Oct 28th, 2023
822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4.  
  5. #define NUM_THREADS 4 // Number of threads to use
  6. #define ARRAY_SIZE 20 // Size of the sorted array
  7.  
  8. // Shared data structure to hold thread arguments
  9. struct ThreadArgs {
  10.     int* arr;
  11.     int target;
  12.     int start;
  13.     int end;
  14.     int result;
  15. };
  16.  
  17. // Function to perform binary search on a sorted array
  18. void* binarySearch(void* arg) {
  19.     struct ThreadArgs* threadArgs = (struct ThreadArgs*)arg;
  20.     int* arr = threadArgs->arr;
  21.     int target = threadArgs->target;
  22.     int start = threadArgs->start;
  23.     int end = threadArgs->end;
  24.  
  25.     while (start <= end) {
  26.         int mid = start + (end - start) / 2;
  27.  
  28.         if (arr[mid] == target) {
  29.             threadArgs->result = mid;
  30.             return NULL;
  31.         } else if (arr[mid] < target) {
  32.             start = mid + 1;
  33.         } else {
  34.             end = mid - 1;
  35.         }
  36.     }
  37.  
  38.     // If the target is not found, set the result to -1
  39.     threadArgs->result = -1;
  40.     return NULL;
  41. }
  42.  
  43. int main() {
  44.     int arr[ARRAY_SIZE] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39};
  45.     int target = 23;
  46.  
  47.     // Initialize the threads and their arguments
  48.     pthread_t threads[NUM_THREADS];
  49.     struct ThreadArgs threadArgs[NUM_THREADS];
  50.  
  51.     int step = ARRAY_SIZE / NUM_THREADS;
  52.     int remaining = ARRAY_SIZE % NUM_THREADS;
  53.     int start = 0;
  54.  
  55.     // Create threads and assign the binary search tasks
  56.     for (int i = 0; i < NUM_THREADS; i++) {
  57.         int end = start + step - 1;
  58.         if (i < remaining) {
  59.             end++;
  60.         }
  61.  
  62.         threadArgs[i].arr = arr;
  63.         threadArgs[i].target = target;
  64.         threadArgs[i].start = start;
  65.         threadArgs[i].end = end;
  66.  
  67.         pthread_create(&threads[i], NULL, binarySearch, (void*)&threadArgs[i]);
  68.  
  69.         start = end + 1;
  70.     }
  71.  
  72.     // Wait for all threads to finish
  73.     for (int i = 0; i < NUM_THREADS; i++) {
  74.         pthread_join(threads[i], NULL);
  75.     }
  76.  
  77.     // Find the first thread that has found the target
  78.     int result = -1;
  79.     for (int i = 0; i < NUM_THREADS; i++) {
  80.         if (threadArgs[i].result != -1) {
  81.             result = threadArgs[i].result;
  82.             break;
  83.         }
  84.     }
  85.  
  86.     if (result != -1) {
  87.         printf("Target %d found at index %d.\n", target, result);
  88.     } else {
  89.         printf("Target %d not found in the array.\n", target);
  90.     }
  91.  
  92.     return 0;
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement