Question

In: Computer Science

please use linux or unix to complete, and include pictures of the output. Modify the code...

please use linux or unix to complete, and include pictures of the output.

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

Look at my code and in case of indentation issues check the screenshots.

---------main.c---------------

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

#define N_OF_THREADS 5

struct range{   //create a structure to pass as parameter to the thread
   int tid;   //The structure contains thread id, the and the range of numbers to compute sum
   int start;
   int end;
};

int sum_values[N_OF_THREADS] = {0};   //create sum array with all values as 0, each thread will write its output to sum array at thread index


void *computeSum(void *param)
{
   struct range *ptr = (struct range *) param;   //get the structure parameter as pointer
   int i;
   for(i = ptr->start; i <= ptr->end; i++)       //compute the sum from start to end
       sum_values[ptr->tid] = sum_values[ptr->tid] + i;   //update the sum array at the thread index

   //print threaad output
   printf("\nThread %d computed sum from %d to %d, SUM = %d", ptr->tid, ptr->start, ptr->end, sum_values[ptr->tid]);
   pthread_exit(NULL);
}


int main()
{
   pthread_t threads[N_OF_THREADS];   //create thread ids as array

   int status, i;   //
   for (i = 0; i < N_OF_THREADS; i++)   //loop for number of threads
   {
       printf("\nMain. Creating thread %d", i);
       struct range *r = malloc(sizeof(struct range));   //create a structure object pointer
       r->tid = i;                       //update the thread id
       if(i == 0){                       //update the range for according to the thread id
           r->start = 1;
           r->end = 200;
       }  
       else if(i == 1){
           r->start = 201;
           r->end = 400;
       }  
       else if(i == 2){
           r->start = 401;
           r->end = 600;
       }  
       else if(i == 3){
           r->start = 601;
           r->end = 800;
       }  
       else if(i == 4){
           r->start = 801;
           r->end = 1000;
       }  
       status = pthread_create(&threads[i], NULL, computeSum, r); //start the thread, pass structure parameter
       if(status){
           printf("Error %d\n", status);
           exit(-1);
       }
   }
  
   for (i = 0; i < N_OF_THREADS; i++){
       pthread_join(threads[i], NULL);   //main waits for all the threads to exit
   }

   int total = 0;
   for (i = 0; i < N_OF_THREADS; i++){
       total = total + sum_values[i];   //main adds the sum of the values computed by the threads
   }  
   printf("\n\nThe sum of numbers from 1 to 1000 is %d\n", total); //print total
   exit(0);
}

----------Screenshots--------------

----------Output--------------

---------Updated answer-------------------------

----------------------------------------

Do comment if you need any clarification.
Please let me know if you are facing any issues.
Please Rate the answer if it helped.

Thankyou


Related Solutions

Unix / Linux 31. Given the first two commands, what is the expected output for the...
Unix / Linux 31. Given the first two commands, what is the expected output for the third command? (single quotes) $ echo * a b c $ x=* $ echo '$x' 32. Given the first two commands, what is the expected output for the third command? (Single quotes inside of double quotes) $ echo * a b c $ x=* $ echo "'$x'" 33. Given the first two commands, what is the expected output for the third command? (double quotes...
PLEASE USE LINUX/UNIX 1.Use either pico, vi, or cat to create the following file and name...
PLEASE USE LINUX/UNIX 1.Use either pico, vi, or cat to create the following file and name it as “mysedfile”: Name Class1 Class2 Class3 Tom 92 94 88 Nancy 91 85 95 Lisa 99 77 96 Jerry 84 98 90 2. Please use sed command(s) to complete the following tasks. display Tom’s record. display Lisa’s record. display both Tom’s and Lisa’s records. remove the blank line(s) from “mysedfile.” replace all the digits with *.
Tower of Hanoi problem complete the problems please use this code #pragma once #include <iostream> #include...
Tower of Hanoi problem complete the problems please use this code #pragma once #include <iostream> #include <stack> #include <string> #include <vector> using namespace std; class TowersOfHanoi { friend ostream& operator<<(ostream& sink, const TowersOfHanoi& towers); public: //constructor TowersOfHanoi(); //custom methods unsigned move(unsigned new_n_disks = 6, ostream& new_sink = cout); private: //custom methods void move_aux(ostream& sink, unsigned n_disks, unsigned srce, unsigned dest, unsigned aux); //temporary variable unsigned _n_moves; //data const unsigned _n_towers; stack<unsigned>* _tower; }; #include "TowersOfHanoi.h" // ostream& operator<<(ostream& sink, const...
modify code to write the output as an HTML table to a file in the output...
modify code to write the output as an HTML table to a file in the output directory. The file that is saying to work at : SOURCE CODE IN PERL: print "Enter principal amount: "; $P=; while($P<=0) { print "Principal must be positive. Try again: "; $P=; } print "Enter number of times interest is applied in a year: "; $n=; while($n!=12 && $n!=4 && $n!=2 && $n!=1) { print "It must be 12, 4, 2 or 1. Try again:...
Use R to complete the following questions. You should include your R code, output and plots...
Use R to complete the following questions. You should include your R code, output and plots in your answer. 1. Two methods of generating a standard normal random variable are: a. Take the sum of 5 uniform (0,1) random numbers and scale to have mean 0 and standard deviation 1. (Use the properties of the uniform distribution to determine the required transformation). b. Generate a standard uniform and then apply inverse cdf function to obtain a normal random variate (Hint:...
Use R to complete the following questions. You should include your R code, output and plots...
Use R to complete the following questions. You should include your R code, output and plots in your answer. 1. Two methods of generating a standard normal random variable are: a. Take the sum of 5 uniform (0,1) random numbers and scale to have mean 0 and standard deviation 1. (Use the properties of the uniform distribution to determine the required transformation). b. Generate a standard uniform and then apply inverse cdf function to obtain a normal random variate (Hint:...
please write in c using linux or unix Write a program that will simulate non -...
please write in c using linux or unix Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally 10 jobs) and...
Please write in C using linux or unix. Write a program that will simulate non -...
Please write in C using linux or unix. Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally 10 jobs) and...
Unix/Linux Turn in the following commands and any output from the commands. 1) ll to show the original 3 files
Unix/LinuxTurn in the following commands and any output from the commands.1) ll to show the original 3 files2) run the tar command to stuff three files3) ll to show the 'tar archive'4) mkdir newdir to create a new directory to unstuff the 'tar archive'
Modify the object provided in the code below to include a variety of overloaded operators in...
Modify the object provided in the code below to include a variety of overloaded operators in C++. Your object should have the following member variables: 1. float *arr a. A dynamic float array that constitutes the data in your myArray object. 2. int size a. The size of the array that the myArray object holds Modify the provided code to include a variety of overloaded operators Operators to overload: 1. bool operator!=(myArray& obj2) a. Tests to see if the calling...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT