Question: Hello can you please include screenshots of this program, input code as well as the output. thank you!!! Create, compile, and execute a C program

Hello can you please include screenshots of this program, input code as well as the output. thank you!!!
Create, compile, and execute a C program to demonstrate the basic Pthreads API for constructing a multithreaded program that calculates the summation of a nonnegative integer in a separate thread. When this program begins, a single thread of control begins in main(). After some initialization, main() creates a second thread that begins control in the runner() function. Refer Fig 4.11 Multithreaded C program using the Pthreads API. You need to add #include to include the standard library to avoid implicit declaration of function atoi warning.
Note: Verify if all underscores are present. All pthread functions begin with pthread
To compile a program called multithread.c:
gcc multithread.c -pthread -o multithread
The sample program calculates the summation of the integer parameter provided on the command line. To compute summation of numbers 1 to 100, execute:
./multithread 100
Add an additional pthread_create line to create a new thread that calls an average() function to calculate the average of the same numbers.
Figure 4.11
#include
#include
#include
int sum; /* this data is shared by the thread(s)*/
void *runner(void *param); /* threads call this function */
int main(int argc, char *argv[])
{
pthread t tid; /* the thread identifier */
pthread attr t attr; /* set of thread attributes */
/* set the default attributes of the thread */
pthread attr init(&attr);
/* create the thread */
pthread create(&tid, &attr, runner, argv[1]);
/* wait for the thread to exit */
pthread join(tid,NULL);
printf("sum =%dn",sum);
}
/* The thread will execute in this function */
void *runner(void *param)
{
int i, upper = atoi(param);
sum =0;
for (i =1; i <= upper; i++)
sum += i;
pthread exit(0);
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!