In: Computer Science
Write a C code to let the main thread create N child threads, where each created thread will randomly generate an integer between 0 to 10 and put it into a global array variable. After that, the main thread will calculate the sum of all the generated integers. N should be input as a command line argument.
Complete the following C code by filling all “???”s in the code sketch. NOTE: when you compile the code, you need to add the parameter “-lpthread” to “gcc”, e.g., “gcc YourCode.c -lpthread”
// include the header file that allows us to use pthread
#include <???>
#include <stdio.h>
// include the header file that allows us to use dynamic memory management
#include <???>
// define a pointer to point to an array to hold the randomly generated integers
int *a;
// define the function used to create a thread
???runner(???param);
int main(int argc, char *argv[])
{
if (argc != 2)
{
fprintf(stderr, "usage: %s <integer value>\n", argv[0]);
return -1;
}
int N = atoi(argv[1]);
if (N<=0)
{
fprintf(stderr, "%d must be > 0\n", N);
return -1;
}
// define an array to hold the threads to be created
??? workers[N];
// define a set of thread attributes for creating threads
??? attr;
// define a variable later used for “for” loop
int i;
// use dynamic memory management to create an array to hold the integers
a = ??? ;
// seed the random number generator
srandom((unsigned)time(NULL));
// initialize the default thread attributes
???
// use “for” loop to create the threads, where the index of the created thread in workers
// array should be passed to the thread as the parameter
???
// use “for” loop to wait for all the threads to exit
???
// calculate the sum
int sum = 0;
for (i=0; i<N; i++) sum += a[i];
printf("sum = %d\n", sum);
// free the dynamically created array
???
}
// The created thread will start its execution from this function
??? runner(??? param)
{
// get the index of the thread in workers array and put it to “thread_index”
int thread_index = ??? ;
// randomly generate an integer between 0 to 10
a[thread_index] = random()%11;
// print the information on the screen
printf("Thread %d generates integer %d ...\n", thread_index, a[thread_index]);
// terminate the thread
???
}
#include <pthread.h>
#include <stdio.h>
// include the header file that allows us to use dynamic memory
management
#include <stdlib.h>
// define a pointer to point to an array to hold the randomly
generated integers
int *a;
// define the function used to create a thread
void * runner(void* param);
int main(int argc, char *argv[])
{
if (argc != 2)
{
fprintf(stderr, "usage: %s <integer value>\n",
argv[0]);
return -1;
}
int N = atoi(argv[1]);
if (N <= 0)
{
fprintf(stderr, "%d must be > 0\n", N);
return -1;
}
// define an array to hold the threads to be
created
pthread_t workers[N];
void* rval;
// define a set of thread attributes for creating
threads
pthread_attr_t attr;
// define a variable later used for “for” loop
int i;
// use dynamic memory management to create an array to
hold the integers
a = (int*)calloc(N, sizeof(int)); ;
// seed the random number generator
srandom((unsigned)time(NULL));
// initialize the default thread attributes
pthread_attr_init(&attr);
// use “for” loop to create the threads, where the index of
the created thread in workers
// array should be passed to the thread as the parameter
for(i=0;i<N;i++) {
pthread_create(&workers[i],&attr,runner,i);
}
// use “for” loop to wait for all the threads to
exit
for(i = 0; i < N; i++)
pthread_join(workers[i],&rval);
// calculate the sum
int sum = 0;
for (i=0; i<N; i++) sum += a[i];
printf("sum = %d\n", sum);
// free the dynamically created array
free(a);
}
// The created thread will start its execution from this
function
void* runner(void* param)
{
// get the index of the thread in workers array and put it to
“thread_index”
int thread_index = (int *)param; ;
// randomly generate an integer between 0 to 10
a[thread_index] = random()%11;
// print the information on the screen
printf("Thread %d generates integer %d ...\n", thread_index,
a[thread_index]);
// terminate the thread
pthread_exit(NULL);
}
OUTPUT :
For N = 10
