Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use fork() and write a program in C that find the parent and child for the follo

ID: 3816709 • Letter: U

Question

Use fork() and write a program in C that find the parent and child for the following case:

Let’s assume that there are two actions you would like to take. In that case, you can give one to the parent process and give the other to the child process. How to tell the difference between the parent and china process? This is the first thing you have to figure out.
Now the two actions, one action to find the largest element and the other actions could be to find the smallest element. So create an array of 1000 and populate it with random integers before the fork call. Do two actions, with the parent finding the largest element and the child processing finding the smallest element, and then each prints the result out. Even better, do that before the fork so you know the result is correct or not. Event better, do these recursively with one or two functions.
Here is the part you need two copies of the same array. Replace every element of the parent with the largest element and print out the total; replace every element of the child process with the smallest element and print out the total. Yes, I know you can calculate the total, but here we are asking you to do the actual replacing to prove a point.

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>


int large(int list[], int size, int largest)
{
if (size == 1)
return largest;

if (size > -1)
{
if (list[size] > largest)
{
largest = list[size];
}
return(largest = large(list, size - 1, largest));
}
else
{
return largest;
}
}
int small(int array[], int index, int len)
{
int min;

if(index >= len-2)
{
if(array[index] < array[index + 1])
return array[index];
else
return array[index + 1];
}

min = small(array, index + 1, len);

if(array[index] < min)
return array[index];
else
return min;
}

int main() {
int pid = fork();
int arra1[1000];
int arra2[1000];
int i;
for ( i = 0; i < 1000; ++i)
{
   int r=rand()%1100+1;
   arra1[i]=r;
   arra2[i]=r;
  
}
printf("Smallest number before fork is %d ",small(arra2, 0, 1000) );
printf("Largest number before fork is %d ",large(arra1, 1000, arra1[0]));
if ((pid=fork() )== 0)
{
  
int smal = arra2[0];
smal = small(arra2, 0, 1000);
printf("Smallest number is during fork %d ", smal);
for ( i = 0; i < 1000; ++i)
   {
  
   arra1[i]=smal;//change all numbers to smallest

   }
}
else {
  
int largest = arra1[0];
largest = large(arra1, 1000, largest);
printf("Largest number is during fork%d ", largest);
for ( i = 0; i < 1000; ++i)
   {
  
   arra2[i]=largest; //change all numbers to largest

   }
}

return 0;
}

============================================================

Output:

akshay@akshay-Inspiron-3537:~/Chegg$ gcc fork.c
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Smallest number before fork is 2
Largest number before fork is 1100
Largest number is during fork1100
Smallest number is during fork 2
Smallest number before fork is 2
Largest number before fork is 1100
Largest number is during fork1100

================================================================

Output will be in any order due to child and parent processes