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 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 to execute:
multithread
Add an additional pthreadcreate line to create a new thread that calls an average function to calculate the average of the same numbers.
Figure
#include
#include
#include
int sum; this data is shared by the threads
void runnervoid param; threads call this function
int mainint 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;
wait for the thread to exit
pthread jointidNULL;
printfsum dnsum;
The thread will execute in this function
void runnervoid param
int i upper atoiparam;
sum ;
for i ; i upper; i
sum i;
pthread exit;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
