Question

In: Computer Science

The following program uses Pthreads to create two threads. They do some work for the process...

The following program uses Pthreads to create two threads. They do some work for the process and then exit. The process then outputs a result.

Assume all supporting libraries and other functions have been included.

=> Use the answer text field to describe what work (operations) the threads are doing, and what kind of result (what is it?) is output by the process.

#include <pthread.h>

#include <stdio.h>

#include <stdlib.h>

int res1, res2, a[100], b[100];

void *runner1(void *param);

void *runner2(void *param);

void readData(int []);


int main(int argc, char *argv[])

{

  pthread_t tid1, tid2;

  pthread_attr_t attr;


  readData(a);

  readData(b);

  pthread_attr_init(&attr);

  pthread_create(&tid1, &attr, runner1, argv[1]);

  pthread_create(&tid2, &attr, runner2, argv[1]);

  pthread_join(tid1,NULL);

  pthread_join(tid2,NULL);

  printf("result = %d\n", res1+res2);

}

void *runner1(void *param)

{

  int i, upper = atoi(param);

  res1 = 0;

  for (i = 0; i < upper; i++)

    res1 += a[i];

  pthread_exit(0);

}

void *runner2(void *param)

{

  int i, upper = atoi(param);

  res2 = 0;

  for (i = 0; i < upper; i++)

    res2 += b[i];

  pthread_exit(0);

}

Solutions

Expert Solution

#Could you please leave a THUMBS Up for my work...

I have made few modifications to your code. Please keep in mind these points.

First, readData() function is not defined, so for running the code, I have initialised array a[0...99] with all 1's, i.e. a[0]=1, a[1]=1.... a[99]=1, similarly, array b[0..99] is also initialised with 1's inside main() function
The range of values allowed for the the first argument passed, i.e, argv[1], should be in beween 0-100. Reason is, this value is used to iterate through values of array a[] and b[], since size of both array is 100, passing value more than 100 will lead to fetching garbage value.
Now, moving to the actual question of your's:

Ques1: Describe what work (operations) the threads are doing?

Thread 1 computes the sum of values of array a[] indexing from 0 to argv[1]. Here, argv[1] is converted to integer value using atoi() function. The sum is stored in res1.
Similarly, Thread 2 computes the sum of values of array b[] indexing from 0 to argv[1].​The sum is stored in res2.
The purpose of using thread, here, is to compute the sum parallelly for both the arrays, instead of computing it linearly one-by-one. This clearly make the program run faster.
​​​​​​​

Ques2: What kind of result (what is it?) is output by the process?

Once res1 and res2 are computed by thread1 and thread2 respectively, as explained above.
In main() function, both values are added, and printed as output.
The print() statement will only run after both the thread exits.
In the code, pthread_join() enables the main(),ie, parent process, to wait for the thread to terminate.
To support the explanation, I am attaching a working code and the output. Please go through it and ask if you face any issue.

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

int res1, res2, a[100], b[100];
void *runner1(void *param);
void *runner2(void *param);
void readData(int []);

int main(int argc, char *argv[]){

argv[1] = "57"; // Remove this statement if passing value while running the code

pthread_t tid1, tid2;
pthread_attr_t attr;

int i=0;

// instead of readData(a), I have initialised array a[] here
for(i=0; i<100; i++)
a[i] = 1;

// readData(b), I have initialised array b[] here
for(i=0; i<100; i++)
b[i] = 1;


pthread_attr_init(&attr);
pthread_create(&tid1, &attr, runner1, argv[1]);
pthread_create(&tid2, &attr, runner2, argv[1]);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);

printf("result = %d\n", res1+res2);
}

void *runner1(void *param){

int i, upper = atoi(param);
res1 = 0;

for (i = 0; i < upper; i++)
res1 += a[i];

pthread_exit(0);
}

void *runner2(void *param){

int i, upper = atoi(param);
res2 = 0;

for (i = 0; i < upper; i++)
res2 += b[i];

pthread_exit(0);
}

Output :

​​​​​​​


Related Solutions

DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread prints all capital letters 'A' to'Z'. The other thread prints all odd numbers from 1 to 21.
Create a program in java with the following information: Design a program that uses an array...
Create a program in java with the following information: Design a program that uses an array with specified values to display the following: The lowest number in the array The highest number in the array The total of the numbers in the array The average of the numbers in the array Initialize an array with these specific 20 numbers: 26 45 56 12 78 74 39 22 5 90 87 32 28 11 93 62 79 53 22 51 example...
Objective: Create a program that uses the methods in the String and Math classes to do...
Objective: Create a program that uses the methods in the String and Math classes to do the following: Ask the user to enter two nouns Tell the user how many letters the nouns are from eachother in the dictionary. Tell the user the last letter of each of the nouns Tell the user the position of the letter "e" in each of the nouns, -1 if no "e" exists in the word. For full credit, you must use quotations around...
Create a simple C++ application that will exhibit concurrencyconcepts. Your application should create two threads...
Create a simple C++ application that will exhibit concurrency concepts. Your application should create two threads that will act as counters. One thread should count up to 20. Once thread one reaches 20, then a second thread should be used to count down to 0. For your created code, provide a detailed analysis of appropriate concepts that could impact your application. Specifically, address:Performance issues with concurrencyVulnerabilities exhibited with use of stringsSecurity of the data types exhibited.
write C program to create 4 threads for summing the numbers between 1 and 40 where...
write C program to create 4 threads for summing the numbers between 1 and 40 where the first thread computes the sum between 1 and 10; the second thread computes the sum between 11 and 20; the third thread computes the sum between 21 and 30; and the fourth thread compute the sum between 31 and 40. The output should be similar to the following. The sum from 1 to 10 = 55 The sum from 11 to 20 =...
Create a java program that will do the following: Create a method called getInt.Allow the user...
Create a java program that will do the following: Create a method called getInt.Allow the user to enter up to 20 student names,and for each student 3 quiz scores (in the range 0-100). Once input is done, display each student’s name, their three quiz scores, and their quiz score average, one student per line. The output table does not need to line up perfectly in columns.Use dialog boxes for all input and output.Use the method to input the three scores.Parameter...
Create a Java application that will exhibit concurrency concepts. Your application should create two threads that will act as counters.
JAVACreate a Java application that will exhibit concurrency concepts. Your application should create two threads that will act as counters. One thread should count up to 20. Once thread one reaches 20, then a second thread should be used to count down to 0.
Create a C++ application that will exhibit concurrency concepts. Your application should create two threads that will act as counters.
C++Create a C++ application that will exhibit concurrency concepts. Your application should create two threads that will act as counters. One thread should count up to 20. Once thread one reaches 20, then a second thread should be used to count down to 0.
Create a C++ application that will exhibit concurrency concepts. Your application should create two threads that will act as counters.
C++ application:Create a C++ application that will exhibit concurrency concepts. Your application should create two threads that will act as counters. One thread should count up to 20. Once thread one reaches 20, then a second thread should be used to count down to 0.For your created code, provide a written analysis of appropriate concepts that could impact your application.Please Specifically, Address: Performance Issues with Concurrency Vulnerabilities Exhibited with use of Strings and Security of the Data Types used.
C++ Vectors. Create a program do the following in the program: 1. declare an vector without...
C++ Vectors. Create a program do the following in the program: 1. declare an vector without specifying the size 2. use push_back to add random integers between 100 and 999 to the vector 3. write a function that returns the smallest, largest, and average of the numbers in the vector display the smallest, largest, and average of the numbers in the vector
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT