Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this section you will learn how to create threads using the Pthreads ( POSIX standard ) API. 1 . The C program shown on

In this section you will learn how to create threads using the Pthreads (POSIX standard)
API.
1. The C program shown on the next page demonstrates the basic Pthreads API for
constructing a multithreaded program that calculates the summation of a non-
negative integer in a separate thread.
2. In a Pthreads program, separate threads begin execution in a specified function. In
the code on the next page, this is the runner() function.
3. When this program begins, a single thread of control begins in main().
4. After some initialization, main() creates a second thread that begins control in the
runner() function. Both threads share the global data sum.
5. All Pthreads programs must include the pthread.h header file.
6. The statement pthread_t tid declares the identifier for the thread we will create.
Each thread has a set of attributes, including stack size and scheduling information.
The pthread_attr_t attr declaration represents the attributes for the thread.
We set the attributes in the function call pthread_attr_init(&attr). Because
we did not explicitly set any attributes, we use the default attributes provided.
7. A separate thread is created with the pthread_create() function call. In addition
to passing the thread identifier and the attributes for the thread, we also pass the
name of the function where the new thread will begin executionin this case, the
runner() function. Last, we pass the integer parameter that was provided on the
command line, argv[1].

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions