Question: #define _ GNU _ SOURCE / / for Ubuntu #include #include #include #include #include #include #include #include / / Pointers to arrays must be in
#define GNUSOURCE 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 arr;
int arr;
int res;
int addrowsvoid args
The argument received by addrows is void it must be typecast.
int ids int args;
########## PART IV ##################################################
Each thread must calculate res arr arr for ids
up to idsinclusive Write code to do this below.
#####################################################################
PART IV YOUR CODE HERE
PART IV YOUR CODE ENDS
freeargs;
exit; Exit terminate child
int mainint argc, char argv
const int NTHREADS ; Number of threads to create
const int ARRSIZE ; Size of arrays being added
Number of rows for each thread to handle
const int NROWS ARRSIZE NTHREADS;
const int STACKSIZE ;
########## PART I ###################################################
Dynamically allocate arr arr and res. They must contain
ARRSIZE integers each. Use calloc
Fill arrays arr and arr with random integers between and
You can google or use ChatGPT to find how to do this.
#####################################################################
PART I YOUR CODE HERE
PART I YOUR CODE ENDS
char stackNTHREADS; Stack memory for each thread
char stackTopNTHREADS; Pointer to stack top for each thread
########## PART II ##################################################
Allocate NTHREADS stacks, and assign NTHREADS stackTop pointers.
NTHREADS is set to four, but your code below should work no matter
how what the value of NTHREADS 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 NTHREADS child threads using clone stackTopi 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 addrows as an argument.
These integers represent the lower and upper indices that the ith
thread will calculate. For example, if there are threads and
elements in the arrays, the the first thread will calculate res
the second will calculate res the third res and the fourth
res You must spawn the threads in such a way that this happens.
It might be helpful to look at the addrows 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;
Here we print all three arrays so that we can verify the results.
for int i ; i ARRSIZE; i
printfd arri;
printf
;
for int i ; i ARRSIZE; i
printfd arri;
printf
;
for int i ; i ARRSIZE; i
printfd resi;
printf
;
freearr;
freearr;
freeres;
return ;
could you show how part 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
