Question

In: Computer Science

using C thank you Must submit as MS Word file with a screenshot of the 3...

using C thank you

Must submit as MS Word file with a screenshot of the 3 outputs. Run your program 3 times.

the output must be in a screenshot not typed for the each time you run the program. thank you

Modify the code below to implement the program that will sum up 1000 numbers using 5 threads.

1st thread will sum up numbers from 1-200

2nd thread will sum up numbers from 201 - 400

...

5th thread will sum up numbers from 801 - 1000

Make main thread wait for other threads to finish execution and sum up all the results.

Display the total to the user.

#include <pthread.h>

#include <stdio.h>

#include <stdlib.h>


#define N_OF_THREADS 4
void * print(void *tid){

printf("Hello %d\n", tid); pthread_exit(NULL);
}
int main(){

pthread_t threads[N_OF_THREADS];

int status, i; //

printf("Main. Creating thread %d\n", i);

for (int i=0; i<N_OF_THREADS; i++) {    

printf("Main. Creating thread %d\n", i);

status = pthread_create(&threads[i], NULL, print, (void*)i);

if(status){

printf("Error %d\n", status);

exit(-1);

}

}

exit(NULL);

}

Solutions

Expert Solution


#include <stdio.h> 
#include<stdlib.h>
#include<pthread.h> 

#define MAX 1000     // size of array 

#define MAX_THREAD 5    // maximum number of threads 
int a[1000] = {[0 ... 999] = 2};    // initialized array elements to 2: for say as an example
int sum_of_partial[5] = { 0 };     //  to contain partial sums of array parts
int part = 0;     //  to identify parts of the array to compute
  
void* sum_of_array(void* arg) 
{ 
  
    // Each thread computes sum of 1/5th of array as mentioned in the question
    int thread_part = part++; 
  
    for (int i = thread_part * (MAX / 5); i < (thread_part + 1) * (MAX / 5); i++) 
        sum_of_partial[thread_part] += a[i]; 
} 
  
// Main program
int main() 
{ 
  
    pthread_t threads[MAX_THREAD]; 
  
    // Creating all the 5 threads 
    for (int i = 0; i < MAX_THREAD; i++) 
    {
        pthread_create(&threads[i], NULL, sum_of_array, (void*)NULL); 
    }
    // joining all the 5 threads (wait for all 5 threads) 
    for (int i = 0; i < MAX_THREAD; i++) 
    {
        pthread_join(threads[i], NULL); 
    }
    // adding the sum of all 5 parts from 5 threads
    int total_sum = 0; 
    for (int i = 0; i < MAX_THREAD; i++) 
        total_sum += sum_of_partial[i]; 
  
    printf("The final sum is: %d", total_sum); 
  
    return 0; 
} 

Related Solutions

. You must use Excel (submit either a pdf, word or Excel file only). . You...
. You must use Excel (submit either a pdf, word or Excel file only). . You must identify the 5 steps (you must address each in detail). Problem: Use the given data to complete a t-test using Excel. Question: Is there a difference in group means between the number of words spelled correctly for two groups of fourth graders? Group Assignment Score 1 3 1 4 1 10 2 14 2 7 2 8 2 10 2 15 2 9...
Directly copy the source code and paste into the Word file. Screenshot of running result must...
Directly copy the source code and paste into the Word file. Screenshot of running result must be presented. 1. (20 points) Write the “Hello, world!” program. 2. (30 points) Write a program to calculate the sum from -5 to10. Use the for loop to do the calculation. 3. (20 points) Write a complete C++ program that asks the user to enter the necessary information about the cylinder, calculate the volume in a function (named as calculate_vol, using reference to pass...
Please follow the instructions and solve it by C++. Thank you! What to Submit Submit the...
Please follow the instructions and solve it by C++. Thank you! What to Submit Submit the following: 1) Your .cpp file of the solution. 2) For what n value (approximately) does your computer stop producing output? Why is this? Enter your answer in the 'Comments' field when you submit the file.   So far in our study of recursion we identified a few common recursive number sequences, such as the Fibonacci numbers. Number sequences like this are not recursive algorithms themselves...
Using JavaScript You must submit a file, called indentation.js indentation.js: program to indent The following program...
Using JavaScript You must submit a file, called indentation.js indentation.js: program to indent The following program is difficult to follow because of its indentation: print ( "binary"); var n = Math.pow (2, 31) - 1; var bits = 0; while (n> 0) { bits + = n & 1; n >> = 1; } print ("Number of bits at 1:" + bits); for (var i = 1; i <= bits; i ++) { var str = ""; if (i% 3...
Using JavaScript You must submit a file called, called pass.js pass.js: Validator of passwords Your task...
Using JavaScript You must submit a file called, called pass.js pass.js: Validator of passwords Your task this week will be to program a valid password validator. A password is safe only if it satisfies the following constraints: - It has from 5 to 10 characters (inclusive) - It contains at least one lowercase letter, at least one capital letter and at least one number - The first character is different from the last Once the password is entered and validated,...
Please in C++ thank you! Please also include the screenshot of the output. I have included...
Please in C++ thank you! Please also include the screenshot of the output. I have included everything that's needed for this. Pls and thank you! Write a simple class and use it in a vector. Begin by writing a Student class. The public section has the following methods: Student::Student() Constructor. Initializes all data elements: name to empty string(s), numeric variables to 0. bool Student::ReadData(istream& in) Data input. The istream should already be open. Reads the following data, in this order:...
Instruction: Answer in a Word file and submit. Show the formula and all corresponding numbers you...
Instruction: Answer in a Word file and submit. Show the formula and all corresponding numbers you use in the formula. Do NOT use spreadsheet and submit. 1. (3 points)     You deposit your $10,000 in a new savings account that earn 4 % annually. You don’t take the interest out and keep it in the savings account so that you can earn interest on interest (compounding!). What would be the value of the $10,000 after 40 years? (Show your calculation) 2....
PLEASE UPLOAD EXCEL FILE OR A SCREENSHOT SHOWING FORMULAS Must be submitted in excel with any...
PLEASE UPLOAD EXCEL FILE OR A SCREENSHOT SHOWING FORMULAS Must be submitted in excel with any math done with formulas within cells. The Jackpots Dreams Cards Company is a card manufacturer. All direct materials and conversion costs are applied to all of Proball Trading Cards’ products at an even rate throughout the manufacturing process. For August, the beginning work in process inventory for the Finishing Department was 9,600 units that averaged 65 percent complete. Ending work in process inventory was...
PLEASE UPLOAD EXCEL FILE OR A SCREENSHOT SHOWING FORMULAS Must be submitted in excel with any...
PLEASE UPLOAD EXCEL FILE OR A SCREENSHOT SHOWING FORMULAS Must be submitted in excel with any math done with formulas within cells. Electric Car Co (ECC). currently manufactures two different types of fully electronic cars in 2021. Type 8Y is a large SUV whereas Type 8W will be the fastest car in the world with 10 rocket thrusters. ECC has decided to use ABC costing instead of traditional costing. The 2021 budget to manufacture these new types include manufacturing overhead...
Using SQL Developer ONLY! Lab 5 1. Create a lab report file (MS Word compatible) and...
Using SQL Developer ONLY! Lab 5 1. Create a lab report file (MS Word compatible) and name it as “IT4153_Lab 5_ Your D2L ID”. a. Create two tables: Employee: empID (PK), empFname, empLname, deptID(FK) and Department: deptID(PK), deptName, chairID chairID is empID from Employee table b. Insert at least 3 rows in the Department table and at least 6 rows in the Employee table. c. Create trigger on update of chairID that enforces the following business rules • One employee...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT