This code is a simple example of how threads can be used to speed up a * program in a multi-core system. Unfortunately it doesn't function properly. * The function run on thread creation wasn't finished, and thus the program * doesn't produce any results. Finish the function calculate_sum so that the * sum is correctly calculated. #Solve it in C language. #include #include #include #include #define NUMTHREADS 2 struct thread_data { int *valuelist; int num_values; int sum; }; void *calculate_sum(void *); int main(void) { int values[] = {1, 3, 5, 7, 9, 2, 6, 7, 0, 10, 11, 12, 13, 14, 15, 40, 50, 60, 61, 62}; int sum = 0; int *iter = values; pthread_t tids[NUMTHREADS]; /* Calculate how much data each thread will work on */ size_t num_values; num_values = sizeof(values) / sizeof(*values); int num_values_per_thread = num_values / NUMTHREADS; /** Standard C doesn't have variable length arrays. To allocate a dynamic * array, calloc can be used. This is different than dynamically creating a * list with multiple malloc calls. */ struct thread_data *d = calloc(NUMTHREADS, sizeof(struct thread_data)); for (int i = 0; i < NUMTHREADS; i++) { /* Using array notation changes -> into . */ d[i].sum = 0; d[i].num_values = num_values_per_thread; d[i].valuelist = calloc(num_values_per_thread, sizeof(int)); for (int j = 0; j < num_values_per_thread; j++) { fprintf(stderr, "assigning %d to thread %d \n", *iter, i); d[i].valuelist[j] = *iter; iter++; } fprintf(stderr, "\n"); } for (int i = 0; i < NUMTHREADS; i++) { pthread_create(&tids[i], NULL, calculate_sum, (void *)&d[i]); } for (int i = 0; i < NUMTHREADS; i++) { pthread_join(tids[i], NULL); sum += d[i].sum; } printf("Sum is: %d\n", sum); } void *calculate_sum(void *param) { struct thread_data *localdata = (struct thread_data *)param; int *iter = localdata->valuelist; /** Hint: The localdata pointer also has fields num_values and sum. */ pthread_exit(NULL); }

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter17: Linked Lists
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

This code is a simple example of how threads can be used to speed up a * program in a multi-core system. Unfortunately it doesn't function properly. * The function run on thread creation wasn't finished, and thus the program * doesn't produce any results. Finish the function calculate_sum so that the * sum is correctly calculated.

#Solve it in C language.

#include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #define NUMTHREADS 2 struct thread_data { int *valuelist; int num_values; int sum; }; void *calculate_sum(void *); int main(void) { int values[] = {1, 3, 5, 7, 9, 2, 6, 7, 0, 10, 11, 12, 13, 14, 15, 40, 50, 60, 61, 62}; int sum = 0; int *iter = values; pthread_t tids[NUMTHREADS]; /* Calculate how much data each thread will work on */ size_t num_values; num_values = sizeof(values) / sizeof(*values); int num_values_per_thread = num_values / NUMTHREADS; /** Standard C doesn't have variable length arrays. To allocate a dynamic * array, calloc can be used. This is different than dynamically creating a * list with multiple malloc calls. */ struct thread_data *d = calloc(NUMTHREADS, sizeof(struct thread_data)); for (int i = 0; i < NUMTHREADS; i++) { /* Using array notation changes -> into . */ d[i].sum = 0; d[i].num_values = num_values_per_thread; d[i].valuelist = calloc(num_values_per_thread, sizeof(int)); for (int j = 0; j < num_values_per_thread; j++) { fprintf(stderr, "assigning %d to thread %d \n", *iter, i); d[i].valuelist[j] = *iter; iter++; } fprintf(stderr, "\n"); } for (int i = 0; i < NUMTHREADS; i++) { pthread_create(&tids[i], NULL, calculate_sum, (void *)&d[i]); } for (int i = 0; i < NUMTHREADS; i++) { pthread_join(tids[i], NULL); sum += d[i].sum; } printf("Sum is: %d\n", sum); } void *calculate_sum(void *param) { struct thread_data *localdata = (struct thread_data *)param; int *iter = localdata->valuelist; /** Hint: The localdata pointer also has fields num_values and sum. */ pthread_exit(NULL); }

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#define NUMTHREADS 2
struct thread_data {
int "valuelist;
int num_values;
int sum;
};
void *calculate_sum(void *);
int main(void) {
int values[] = {1, 3, 5, 7, 9, 2, 6, 7,
в, 10,
11, 12, 13, 14, 15, 40, 50, 68, 61, 62};
%3D
int sum - 0;
int *iter - values;
pthread_t tids[NUMTHREADS];
/* Calculate how much data each thread will work on */
size_t num_values;
num_values = sizeof (values) / sizeof (*values);
int num_values_per_thread = num_values / NUMTHREADS;
/** Standard C doesn't have variable length arrays. To allocate a dynamic
* array, calloc can be used. This is different than dynamically creating a
* list with multiple malloc calls.
*/
struct thread_data *d = calloc(NUMTHREADS, sizeof(struct thread_data));
for (int i - 0; i < NUMTHREADS; i++) {
/* Using array notation changes -> into . /
d[i].sum = 0;
d[1].num_values - num_values_per_thread;
d[i].valuelist - calloc(num_values_per_thread, sizeof(int));
for (int j = 0; j< num values_per_thread; j++) {
fprintf(stderr, "assigning Xd to thread %d \n", *iter, 1);
d[i].valuelist[j] - *iter;
iter++;
%3D
fprintf(stderr, "In");
}
for (int i - 0; i < NUMTHREADS; i++) {
pthread_create(&tids[i], NULL, calculate_sum, (void *)&d[i]);
}
for (int i - 0; i < NUMTHREADSs; i++) {
pthread_join(tids[i], NULL);
sum += d[i].sum;
}
printf("Sum is: %d\n", sum);
void *calculate sum(void *param) {
struct thread_data *localdata = (struct thread_data *)param;
int *iter - localdata->valuelist;
/** Hint: The localdata pointer also has fields num_values and sum. /
pthread_exit(NULL);
Transcribed Image Text:#include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #define NUMTHREADS 2 struct thread_data { int "valuelist; int num_values; int sum; }; void *calculate_sum(void *); int main(void) { int values[] = {1, 3, 5, 7, 9, 2, 6, 7, в, 10, 11, 12, 13, 14, 15, 40, 50, 68, 61, 62}; %3D int sum - 0; int *iter - values; pthread_t tids[NUMTHREADS]; /* Calculate how much data each thread will work on */ size_t num_values; num_values = sizeof (values) / sizeof (*values); int num_values_per_thread = num_values / NUMTHREADS; /** Standard C doesn't have variable length arrays. To allocate a dynamic * array, calloc can be used. This is different than dynamically creating a * list with multiple malloc calls. */ struct thread_data *d = calloc(NUMTHREADS, sizeof(struct thread_data)); for (int i - 0; i < NUMTHREADS; i++) { /* Using array notation changes -> into . / d[i].sum = 0; d[1].num_values - num_values_per_thread; d[i].valuelist - calloc(num_values_per_thread, sizeof(int)); for (int j = 0; j< num values_per_thread; j++) { fprintf(stderr, "assigning Xd to thread %d \n", *iter, 1); d[i].valuelist[j] - *iter; iter++; %3D fprintf(stderr, "In"); } for (int i - 0; i < NUMTHREADS; i++) { pthread_create(&tids[i], NULL, calculate_sum, (void *)&d[i]); } for (int i - 0; i < NUMTHREADSs; i++) { pthread_join(tids[i], NULL); sum += d[i].sum; } printf("Sum is: %d\n", sum); void *calculate sum(void *param) { struct thread_data *localdata = (struct thread_data *)param; int *iter - localdata->valuelist; /** Hint: The localdata pointer also has fields num_values and sum. / pthread_exit(NULL);
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Avoiding deadlock
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning