Question: #define _ GNU _ SOURCE / / for Ubuntu #include #include #include #include #include #include #include #include / / Pointers to arrays must be in

#define _GNU_SOURCE //for Ubuntu
#include
#include
#include
#include
#include
#include
#include
#include
// Pointers to arrays must be in global scope,
// so that all child threads can access them.
int *arr1;
int *arr2;
int *res;
int add_rows(void *args)
{
// The argument received by add_rows is void*, it must be typecast.
int *ids =(int*) args;
/* ########## PART IV ################################################## */
// Each thread must calculate res = arr1+ arr2 for ids[0]
// up to ids[1](inclusive). Write code to do this below.
/* ##################################################################### */
/* PART IV - YOUR CODE HERE */
/* PART IV - YOUR CODE ENDS */
free(args);
exit(0); // Exit (terminate child)
}
int main(int argc, char *argv[])
{
const int N_THREADS =4; // Number of threads to create
const int ARR_SIZE =16; // Size of arrays being added
// Number of rows for each thread to handle
const int N_ROWS = ARR_SIZE / N_THREADS;
const int STACK_SIZE =16384;
/* ########## PART I ################################################### */
// Dynamically allocate arr1, arr2, and res. They must contain
// ARR_SIZE integers each. Use calloc().
// Fill arrays arr1 and arr2 with random integers between 0 and 10
// You can google (or use ChatGPT) to find how to do this.
/* ##################################################################### */
/* PART I - YOUR CODE HERE */
/* PART I - YOUR CODE ENDS */
char *stack[N_THREADS]; // Stack memory for each thread
char *stackTop[N_THREADS]; // Pointer to stack top for each thread
/* ########## PART II ################################################## */
// Allocate N_THREADS stacks, and assign N_THREADS stackTop pointers.
// N_THREADS is set to four, but your code below should work no matter
// how what the value of N_THREADS is. Refer to previous clone() example
// to see how to do this.
/* ##################################################################### */
/* PART II - YOUR CODE HERE */
/* PART II - YOUR CODE ENDS */
/* ########## PART III ################################################# */
// Spawn N_THREADS child threads using clone(). stackTop[i], allocated
// in part II, is the stack for the ith thread.
// The fourth argument to clone, instead of being NULL as in previous
// examples, will be an array of two integers. This fourth argument is
// what gets passed to add_rows() as an argument.
// These integers represent the lower and upper indices that the ith
// thread will calculate. For example, if there are 4 threads and 16
// elements in the arrays, the the first thread will calculate res[0-3],
// the second will calculate res[4-7], the third res[8-11], and the fourth
// res[12-15]. You must spawn the threads in such a way that this happens.
// It might be helpful to look at the add_rows function above, first.
/* ##################################################################### */
/* PART III - YOUR CODE HERE */
/* PART III - YOUR CODE ENDS */
// Parent keeps going here. Put it to sleep for one second to
// make sure all children finish first.
sleep(1);
// Here we print all three arrays so that we can verify the results.
for (int i =0; i < ARR_SIZE; i++)
printf("%3d ", arr1[i]);
printf("
");
for (int i =0; i < ARR_SIZE; i++)
printf("%3d ", arr2[i]);
printf("
");
for (int i =0; i < ARR_SIZE; i++)
printf("%3d ", res[i]);
printf("
");
free(arr1);
free(arr2);
free(res);
return 0;
}
could you show how part 3 would be done using clone() function instead of p threads?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!