Question

In: Computer Science

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

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 <pthread.h>

#include <stdio.h>

#include <stdlib.h>
#define N_OF_THREADS 4

int start_num = 0;

int sum[4] ={0};

void * print(void *tid){

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

void *sum_of_num(void* arg){

int thread_num = start_num++

for(int i=(thread_num*200)+1;i<=(thread_num+1)*200;i++){

     sum[start_num]+=i;

}

}


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, sum_of_num, (void*)i);

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

        pthread_join(threads[i], NULL);

int tsum = 0;

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

    tsum += sum[i];

printf("Total is : %d",tsum);

if(status){

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

exit(-1);

}

}

exit(NULL);

}


Related Solutions

Program in java 1- Implement an algorithms to calculate the sum of all numbers in an...
Program in java 1- Implement an algorithms to calculate the sum of all numbers in an array.   2- Implement an algorithm to determine if an array has all unique integer values. 3- Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]). Return the running sum of nums. Example 1: Input: nums = [1,2,3,4] Output: [1,3,6,10] Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
Write a C code program to implement the comparisons of three integer numbers, using only conditional...
Write a C code program to implement the comparisons of three integer numbers, using only conditional operators (no if statements). Please ask the user to input random three integers. Then display the minimum, middle, and maximum number in one line. Also, please calculate and display the sum and the average value (save two digits after decimal point) of these three integers. Please write the comments line by line.
modify the code below to create a GUI program that accepts a String from the user...
modify the code below to create a GUI program that accepts a String from the user in a TextField and reports whether or not there are repeated characters in it. Thus your program is a client of the class which you created in question 1 above. N.B. most of the modification needs to occur in the constructor and the actionPerformed() methods. So you should spend time working out exactly what these two methods are doing, so that you can make...
5. Modify the program for Line Numbers from L09: In-Class Assignment. Mainly, you are changing the...
5. Modify the program for Line Numbers from L09: In-Class Assignment. Mainly, you are changing the problem from using arrays to ArrayLists. There are some other modifications as well, so read the instructions carefully. The program should do the following: –Ask the user for how many lines of text they wish to enter –Declare and initialize an **ArrayList** of Strings to hold the user’s input –Use a do-while loop to prompt for and read the Strings (lines of text) from...
write a java code program using loops to compute the sum of the 30 terms of...
write a java code program using loops to compute the sum of the 30 terms of the series below. Find sum of all integers between 100 and 200 which are divisible by 9 or 13 Write a program to find the sum of the first 8 terms of the series 1 + 11 + 111 + 1111 + . . . Output: Term Ratio Sum
"4. (Modify) Modify Program 7.14 so that the user inputs the initial set of numbers when...
"4. (Modify) Modify Program 7.14 so that the user inputs the initial set of numbers when the program runs. Have the program request the number of initial numbers to be entered." //C++ Program 7.14 as follows #include #include #include #include using namespace std; int main() { const int NUMELS = 4; int n[] = {136, 122, 109, 146}; int i; vector partnums(n, n + NUMELS); cout << "\nThe vector initially has the size of " << int(partnums.size()) << ",\n and...
1. Write MIPS assembly code to sum “n” positive integers. Forexample, n=5, read 5 numbers...
1. Write MIPS assembly code to sum “n” positive integers. For example, n=5, read 5 numbers in memory (“.data” section) and add them together. 2. Write MIPS assembly code to calculate N-factorial. Read n from the memory. (Hint: use “.data” to define the content in a memory location)
Write a program that generates all prime numbers between 2 and 1000, using the Sieve of...
Write a program that generates all prime numbers between 2 and 1000, using the Sieve of Eratosthenes method. You can find many articles that describe the method for finding primes in this manner on the Internet. Display all the prime values. This program should be in assembly language.
Any idea why the code below prints the numbers up and down instead of sideways? They...
Any idea why the code below prints the numbers up and down instead of sideways? They should print like this: 299 297 275 273 264    260 253 247 242 234    231 221 220 209 208    Found 15 integers totaling 3733 Mine prints like this: 299 297 275 273 264 260 253 247 242 234 231 221 220 209 208 Number of multiples are: 15 The sum of multiples are: 3733 public static void main(String[] args) { //start...
def sum_gt_avg(num_list): Implement a function that returns the sum of the numbers in num_list that have...
def sum_gt_avg(num_list): Implement a function that returns the sum of the numbers in num_list that have a value greater than the average value in the list. • Parameters: num_list is a list of numbers (mixed integers and floats) • Examples: sum_gt_avg([1,2,3,4,5]) → 9 # 4+5 sum_gt_avg([1,2,3,-4,5]) → 10 # 2+3+5 sum_gt_avg([-1,-2,-3,-4,-5]) → -3 # -1-2 in python
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT