// arr[0] = 1, arr[1] = 2, arr[2] = 3, arr[3] = 4, arr[4] = 5, arr[5] = 6, arr[6] =7, arr[7] = 8.
// count = 8 which is the length of the array in this case
// value = 6 which is the value that is being searched
int get_first(int arr[], int count)
{
int half = count / 2;
int *firstHalf = malloc(half * sizeof(int));
memcpy(firstHalf, arr, half * sizeof(int));
return firstHalf;
}
int get_second(int arr[], int count)
{
int half = count / 2;
int *secondHalf = malloc(half * sizeof(int));
memcpy(secondHalf, arr + half, half * sizeof(int));
return secondHalf;
}
pid_t pid;
pid = fork();
if (pid == 0)
{
int half = count / 2;
int *result = get_first(arr, count);
while (sizeof(result) != 1)
{
half = half / 2 ;
int *result = get_first(result, half);
}
if (result[0] == value[0])
{
return the index of value
}
}
else
{
int half = count / 2;
int *result1 = get_second(arr, count);
while (sizeof(result1) != 1)
{
half = half / 2 ;
int *result1 = get_second(result1, half);
}
if (result1[0] == value[0])
{
return the index of value
}
}
I am new to c and I am really not good at fork(). I need to write a program that uses multiple processes to keep splitting an array into two equal parts. When the array only has a single element, the process will compare the single element to the integer being searched for.
For example, to split an array 5, 6, 7, 8. P1 creates two children, P2 which is assigned to search 5, 6, and P3 which is assigned to search 7, 8. P2 creates two children, P4 which is assigned to search 5, and P5 which is assigned to search 6. P3 creates two children P6 which is assigned to search 7, and P7 which is assigned to search 8.
I wrote two functions to split the array and they are both working fine. The first function gets the first part of the array and the second function gets the second part of the array (I did not write the indexing part because I have not got to there yet). But I am struggling with the fork(). I think the way I am using fork() does not help me to get the right answer, because it does not get to every element in the array.
My question is how to change the fork() in order to allow it to compare every element in the array with the value that I am searching for. Can someone help me to fix it? Thanks in advance.