In: Computer Science
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);
}
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