Question: Multithreaded C program using the Pthreads API ( Use Ubuntu ( Linux ) ) Create, compile, and execute a C program to demonstrate the basic

Multithreaded C program using the Pthreads API (Use Ubuntu (Linux))
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
For full points, submit before due time (2/21/245:30 p.m. PST)
Pthreads:
#include
#include
#include
int sum;
void *runner(void *param){
int i, upper = atoi((char *)param);
sum =0;
for (i =1; i <= upper; i++)
sum += i;
pthread_exit(0);
}
int main(int argc, char *argv[]){
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tid, &attr, runner, argv[1]);
pthread_join(tid, NULL);
printf("sum =%d
", sum);
return 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 Databases Questions!