1. Homepage
  2. Exam
  3. [2021] CMPSC311 Introduction to Systems Programming - Q11 Parallel Sum

[2021] CMPSC311 Introduction to Systems Programming - Q11 Parallel Sum

This question has been solved
Engage in a Conversation

Question 11 CourseNana.COM


CourseNana.COM

The following program declares a global integer array called x with 1000 elements. It then launches two threads to sum the elements of the array in parallel. The first thread adds the first 500 elements of x and the second thread adds the second 500elements of the array. After they return, the main function adds the results returned from both threads and prints out the result. Please specify what goes into the blanks numbered [1] through [8]. CourseNana.COM

  CourseNana.COM

int x[1000]; CourseNana.COM

typedef struct { CourseNana.COM

int *array; CourseNana.COM

int size; CourseNana.COM

} arg_t; CourseNana.COM

  CourseNana.COM

void *sum_array(void *a) { CourseNana.COM

arg_t *arg = a; CourseNana.COM

int *sum = __[1]__; CourseNana.COM

*sum = 0; CourseNana.COM

  CourseNana.COM

for (int i = 0; i < arg->size; ++i) CourseNana.COM

*sum += __[2]__; CourseNana.COM

  CourseNana.COM

return sum; CourseNana.COM

} CourseNana.COM

int main(void) { CourseNana.COM

pthread_t t1, t2; CourseNana.COM

arg_t arg1 = {__[3]__, 500}, arg2 = {x + 500, __[4]__}; CourseNana.COM

pthread_create(&t1, NULL, sum_array, __[5]__); CourseNana.COM

pthread_create(&t2, NULL, __[6]__, &arg2); CourseNana.COM

  CourseNana.COM

int *sum1, *sum2; CourseNana.COM

pthread_join(t1, __[7]__); CourseNana.COM

pthread_join(t2, __[8]__); CourseNana.COM

  CourseNana.COM

int sum = *sum1 + *sum2; CourseNana.COM

printf("sum is = %d\n", sum); CourseNana.COM

} CourseNana.COM


CourseNana.COM

Get the Solution to This Question

WeChat (微信) WeChat (微信)
Whatsapp WhatsApp
Pennsylvania State University代写,PSU代写,Introduction to Systems Programming代写,CMPSC311代写,Pennsylvania State University代编,PSU代编,Introduction to Systems Programming代编,CMPSC311代编,Pennsylvania State University代考,PSU代考,Introduction to Systems Programming代考,CMPSC311代考,Pennsylvania State Universityhelp,PSUhelp,Introduction to Systems Programminghelp,CMPSC311help,Pennsylvania State University作业代写,PSU作业代写,Introduction to Systems Programming作业代写,CMPSC311作业代写,Pennsylvania State University编程代写,PSU编程代写,Introduction to Systems Programming编程代写,CMPSC311编程代写,Pennsylvania State Universityprogramming help,PSUprogramming help,Introduction to Systems Programmingprogramming help,CMPSC311programming help,Pennsylvania State Universityassignment help,PSUassignment help,Introduction to Systems Programmingassignment help,CMPSC311assignment help,Pennsylvania State Universitysolution,PSUsolution,Introduction to Systems Programmingsolution,CMPSC311solution,