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

Please find the detailed explanation of each steps as inline comment. Also see an example at the end of the program. I hope this explanation helps you to understand the flow properly.

Please provide your feedback

Thanks and Happy learning!


#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[])

{
    //Declare the pthread variables which is used to uniquely
    //identify a thread.Thai will be used later while creating
        //the threads using pthread_create
    pthread_t tid1, tid2;
        
    //Declare the required attribute for the thread
    pthread_attr_t attr;

    //Below two lines will be reading the input(int) and storing
        //in the integer array a[] and b[] respectively declared in 
        //the global section 
    readData(a);
    readData(b);

        //Below line initializes the attributes required for a thread.
        //Here the attribute is initialized with default values.
    pthread_attr_init(&attr);
        
        //Below two linws create two threads with id tid1 and tid2 respectively.
        //1. Here the first argument(ie tid1) is to hold the id of the new thread created.
        //2. Second argument (ie attr) will be used to initialise the attributes of the new thread .
        //3. Third argument(ie runner1) is the function that will be get invoked by the thread once it is created.
        //4. Fourth argument(ie argv[1]) is the argument that will be passed to the function mentioned in step#3
        //   (ie function runner1) when it is incoked by the thread. That means when the thread invokes the function
        //    runner1, it will be as if its is calling runner1(argv[1])
    pthread_create(&tid1, &attr, runner1, argv[1]);
    pthread_create(&tid2, &attr, runner2, argv[1]);

        //Below two statements will make the parent thread(ie the thread which is executing the main()
        //function) to wait for the two threads (with id tid1 and tid2) until it is completed. This means
        //that the main thread will be waiting until the execution of functions runner1() and runner2()
        //is completed.
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);

        //Print the sum of result calculated in the runner1() and runner2()
    printf("result = %d\n", res1 + res2);

}

void *runner1(void *param)

{
    //Here the program converts the argument passed to the
        //function into integer using atoi() function call.
    int i, upper = atoi(param);

        //Below loop executes 'upper'(calculated above using atoi()) number of times
        //and adds up the numbers in the input array a[100] (which is read in the main() function
        //readData()).
        //
        //In simple terms the below loop adds up the first 'upper' number of numbers in the
        //a[100] array and stores the result in the res1 variable.
    res1 = 0;
    for (i = 0; i < upper; i++)
        res1 += a[i];

    //Exit the thread function
    pthread_exit(0);

}

void *runner2(void *param)

{
    //Here the program converts the argument passed to the
        //function into integer using atoi() function call.
    int i, upper = atoi(param);

    res2 = 0;

    //The below loop adds up the first 'upper' number of numbers in the
        //b[100] array and stores the result in the res2 variable.
    for (i = 0; i < upper; i++)
        res2 += b[i];

    //Exit the thread function
    pthread_exit(0);

}

For example, suppose
The input array a[100] contains numbers {1,2,3,4}
The input array b[100] contains numbers {5,6,7,8}
And if argv[1](ie the first command line argument passed while executing the program) is 3

Then runner1() function(invoked by first thread tid1) will calculate rs1 as the sum of first 3 numbers from a[100]
ie rs1 = 1 + 2 + 3 that means rs1 = 6

Then runner2() function (invoked by second thread tid2) will calculate rs2 as the sum of first 3 numbers from b[100]
ie rs2 = 5 + 6 + 7 that means rs1 = 18

And in the main() function, the main thread will wait until the above two functions are finished, then it will print the
result as res1 + res2, that means result will be 6 + 18 = 24

Related Solutions

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);...
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);...
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);...
Write a C or C++ program that uses pthreads to compute the number of values that...
Write a C or C++ program that uses pthreads to compute the number of values that are evenly divisible by 97 between a specified range of values (INCLUSIVE). The program should accept 3 command-line arguments: low value high value number of threads to create to perform the computation -Specifics for program order of input: 0 9000000000 2 this means 0 to 9 Billion (INCLUSIVE); 2 threads alarm(90); this should be the first executable line of the program to make sure...
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 =...
Write a multithreaded program that tests your solution to HW#1. You will create several threads –...
Write a multithreaded program that tests your solution to HW#1. You will create several threads – for example, 100 – and each thread will request a pid, sleep for a random period of time, and then release the pid. (Sleeping for a random period approximates the typical pid usage in which a pid is assigned to a new process, the process executes and terminates, and the pid is released on the process’ termination). On UNIX and Linux systems, sleeping is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT