41
1 Threads Chapter 11 from the book: Inter-process Communications in Linux : The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date: January 13, 2003

1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

Embed Size (px)

Citation preview

Page 1: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

1

Threads

Chapter 11 from the book: Inter-process Communications in Linux:The Nooks & Crannies by John Shapley Gray

Publisher: Prentice Hall Pub Date: January 13, 2003

Page 2: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

2

Topics Thread Definition Creating a Thread Exiting a Thread Basic Thread Management Thread Attributes Scheduling Threads

THREADS

Page 3: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

3

Threads

THREADS

Page 4: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

4

Thread Definition Each thread has its own

stack register set program counter thread-specific data thread-local variables thread-specific signal mask state information

However, all such threads share the same address space, general signal handling, virtual memory, data, and I/O with the other threads within the same process.

THREADS

Page 5: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

5

Thread Definition Figure 11.1 compares communications of single

thread processes versus multiple threads process.

THREADS

Page 6: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

6

Thread Definition

THREADS

Figure 11.1. Conceptual communications—multiple single-threaded processes versus a single process with multiple threads.

Page 7: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

7

Thread Definition At system implementation level there are two

traditional approaches or models used to generate and manage threads.

User-level model Kernel-level model

THREADS

Page 8: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

8

Thread Definition The user-level model, runs on top of the existing

operating system and is transparent to it. Library functions and system calls made within

the thread are wrapped in special code to allow for thread run-time management.

Threads implemented in this manner have low system overhead and are easily extensible.

More importantly, user-level threads are designed to share resources with other threads within their process space when running on a single processor.

THREADS

Page 9: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

9

Thread Definition In the kernel-level model, the operating system

is aware of each thread. While the management of kernel-level threads is

less intensive than that of individual processes, it is still more expensive than user-level-based threads.

The kernel-level threads are run as lightweight processes (LWP) and are scheduled and maintained by the operating system

THREADS

Page 10: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

10

Creating a Thread Every process contains at least one main or

initial thread of control created by the operating system when the process begins to execute.

The library function pthread_create is used to add a new thread of control to the current process. The new thread executes with the other threads within the process and, if directed, with other threads of control in other processes.

(Table 11.1.)

THREADS

Page 11: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

11

Creating a Thread

Return

Summaryint pthread_create(pthread_t *thread,

pthread_attr_t *attr,

void *(*start_routine)(void *),

void *arg);

Include File(s)

<pthread.h>

Manual Section3

SuccessFailureSets errno

0Nonzero

Table 11.1. The pthread_create Library Function.

THREADS

Page 12: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

12

Creating a Thread Once a thread is created, it has its own set of

attributes and an execution stack. It inherits its signal mask (which it then can

alter) and scheduling priority from the calling program (the initiating thread).

It does not inherit any pending signals. If needed, a thread can allocate its own storage for thread-specific data.

THREADS

Page 13: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

13

Creating a Thread The thread continues to execute until

The function completes (implicitly or explicitly). A call is made to pthread_exit. The thread is canceled with a call to

pthread_cancel. The process that created the thread exits

(implicitly or explicitly). One of the threads performs an exec.

THREADS

Page 14: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

14

Exiting a Thread The pthread_exit library call terminates a thread

in much the same manner as a call to exit terminates a process.

(Table 11.2.)

THREADS

Page 15: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

15

Exiting a Thread

Return

Summaryvoid pthread_exit (void * retval);

Include File(s)

<pthread.h>

Manual Section3

SuccessFailureSets errno

This call does not return

Table 11.2. The pthread_exit Library Function.

THREADS

Page 16: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

16

Basic Thread Management Once a thread is created, we can direct the

calling process to wait until the thread is finished (it calls pthread_exit or is cancelled). This is accomplished with a call to the pthread_join library function.

(Table 11.3.)

THREADS

Page 17: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

17

Basic Thread Management

Return

Summaryint pthread_join( pthread_t target_thread,

void **status );

Include File(s)

<pthread.h>

Manual Section3

SuccessFailureSets errno

0Nonzero

Table 11.3. The pthread_join Library Function.

THREADS

Page 18: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

18

Basic Thread Management There are some caveats associated with joining

threads. A thread should be waited upon (joined) by only

one other thread. The thread issuing the join does not need to be

the initiating thread. If multiple threads wait for the same thread to

complete, only one will receive the correct status information.

The joins in competing threads will return an error. Should the thread initiating the join be canceled,

the waited upon thread can be joined by another thread.

THREADS

Page 19: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

19

Basic Thread Management If the targeted thread has terminated prior to the

issuing of the call to pthread_join, the call will return immediately and will not block.

A non detached thread (which is the default) that is not joined will not release its resources when the thread finishes and will only release its resources when its creating process terminates.

Such threads can be the source of memory leaks.

THREADS

Page 20: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

20

Basic Thread Management The process of joining a thread is somewhat

analogous to a process waiting on a forked child process.

Unlike a forked child process, a thread can become detached with a single library call.

When a detached thread finishes, its resources are automatically returned to the system.

The pthread_detach library call is used to dynamically detach a joinable thread.

(Table 11.4)

THREADS

Page 21: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

21

Basic Thread Management

Return

Summaryint pthread_detach (pthread_t threadID);

Include File(s)

<pthread.h>

Manual Section3

SuccessFailureSets errno

0Nonzero

Table 11.4. The pthread_detach Library Function.

THREADS

Page 22: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

22

Basic Thread Management Once a thread is detached, other threads can no

longer synchronize their activities based on its termination.

Exp 6.1: The program for creating and joining threads.

THREADS

Page 23: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

23

Thread Attributes In a POSIX implementation, if we want to

generate a thread that does not have the default attributes (obtained by setting the second parameter of the pthread_create call to NULL), an attribute object is used.

To use an attribute object, it must first be initialized. This is accomplished using the library call pthread_attr_init.

(Table 11.5)

THREADS

Page 24: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

24

Thread Attributes

Return

Summaryint pthread_attr_init ( pthread_attr_t *attr );

Include File(s)

<pthread.h>

Manual Section3

SuccessFailureSets errno

0Nonzero

Table 11.5. The pthread_attr_init Library Function.

THREADS

Page 25: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

25

Thread Attributes If the call is successful, it returns a 0 and

initializes the referenced attribute object with the default value for each attribute.

(Table 11.6)

THREADS

Page 26: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

26

Thread Attributes

AttributeDefaultComments

detachstatePTHREAD_CREATE_JOINABLE

A non detached thread that can be joined by other threads. The thread's resources are not freed until a call is made to pthread_join or the calling process exits.

inheritschedPTHREAD_EXPLICIT_SCHEDIndicates whether or not scheduling attributes are inherited from parent thread or set explicitly by the attribute object.

schedparam0Scheduling parameters (priority).

schedpolicySCHED_OTHERScheduling is determined by the system (most often some sort of timesharing). Note the missing PTHREAD_prefix.

scopePTHREAD_SCOPE_SYSTEScope of scheduling contention—with all threads in same process or all processes in the system.

Table 11.6. Thread Attributes and Default Settings.

THREADS

Page 27: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

27

Thread Attributes Once initialized, individual attribute values can

be modified. The newly created thread will have the specified

attributes. The attribute object is independent of the thread,

and changes to the attribute object after a thread has been created are not reflected in existing threads.

Once established, a thread attribute object can be used in the generation of multiple threads.

THREADS

Page 28: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

28

Thread Attributes If the user wants a thread to have different

characteristics, he should first initialize the attribute object using the pthread_attr_init library call and then change the attributes he wants to be different.

Each attribute has an associated pthread_attr_setxxx and pthread_attr_getxxx function call that will act upon the attribute object.

(Table 11.7)

THREADS

Page 29: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

29

Thread Attributes

AttributeSet and get CallsDefined Constants for the 2nd setxxx parameter

detachstateint pthread_attr_setdetachstate)

pthread_attr_t *attr,

int detachstate(

int pthread_attr_getdetachstate)

const pthread_attr_t *attr,

int *detachstate;(

PTHREAD_CREATE_JOINABLE

PTHREAD_CREATE_DETACHED

Table 11.7. Thread Attribute Set and Get functions

THREADS

Page 30: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

30

Thread Attributes

AttributeSet and get CallsDefined Constants for the 2nd setxxx parameter

inheritschedint pthread_attr_setinheritsched (

pthread_attr_t *attr,

int inheritsched

int pthread_attr_getinheritsched (

const pthread_attr_t *attr,

int *inheritsched) ;

PTHREAD_EXPLICIT_SCHED

PTHREAD_INHERIT_SCHED

Table 11.7. Thread Attribute Set and Get functions

THREADS

Page 31: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

31

Thread Attributes

AttributeSet and get CallsDefined Constants for the 2nd setxxx parameter

schedparamint pthread_attr_setschedparam)

pthread_attr_t *attr,

const struct sched_param *param;(

int pthread_attr_getschedparam)

pthread_attr_t *attr,

const struct sched_param *param;(

0

Reference to valid sched_param structure

with its sched_priority member assigned a valid priority.

Table 11.7. Thread Attribute Set and Get functions

THREADS

Page 32: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

32

Thread Attributes

AttributeSet and get CallsDefined Constants for the 2nd setxxx parameter

schedpolicyint pthread_attr_setschedpolicy)

pthread_attr_t *attr,

int policy;(

int pthread_attr_getschedpolicy)

const pthread_attr_t *attr,

int *policy(

SCHED_OTHER

SCHED_FIFO

SCHED_RR

Table 11.7. Thread Attribute Set and Get functions

THREADS

Page 33: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

33

Thread Attributes

AttributeSet and get CallsDefined Constants for the 2nd setxxx parameter

scopeint pthread_attr_setscope)

pthread_attr_t *attr,

int contentionscope;(

int pthread_attr_getscope)

const pthread_attr_t *attr,

int *contentionscope;(

PTHREAD_SCOPE_SYSTEM

PTHREAD_SCOPE_PROCESS

Table 11.7. Thread Attribute Set and Get functions

THREADS

Page 34: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

34

Scheduling Threads

Understanding the Linux Kernel provides an excellent in-depth presentation of Linux scheduling.

Essentially, scheduling is used to determine which ready-to-run task the CPU will execute next.

A good operating system will reveal a variety of scheduling policies, some of the more common of which are:

THREADS

Page 35: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

35

Scheduling Threads

First come, first served— first to request service is processed first (also called a FIFO arrangement).

Shortest job first— the task requiring least amount of processing is done first.

Priority-based— tasks with higher priority are done first.

Round-robin— each task gets a small time slice; tasks reside in a circular queue until they are finished.

THREADS

Page 36: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

36

Scheduling Threads

Furthermore, many of these strategies can be implemented as

Nonpreemptive• Once the task begins, it goes to completion

Preemptive • The task can be removed by a task of a higher

priority

In current operating systems preemption is the norm.

Keep in mind, processes can be in user mode or kernel mode.

THREADS

Page 37: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

37

Scheduling Threads

Traditionally, a process running on a single processor system is nonpreemptive when it is in kernel mode.

Similar to processes, Threads can be in ready, running and blocked states.

Scheduling of a POSIX thread in Linux is determined by priority and scheduling policy parameters.

sched_get_priority_max sched_get_priority_min

can be used to determine the actual priority limits for a specific scheduling policy.

THREADS

Page 38: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

38

Scheduling Threads

Three scheduling policies for threads:

1. SCHED_OTHER: (system default) It is a time sharing policy in which all threads with same static priority 0 receives a time slice.

2. SCHED_FIFO: First in first out

3. SCHED_RR: Round robin A scheduling policy and priority of a thread that

already exists can be set with the pthread_setschedparam library function.

(Table 11.8.) Exp 6.2 : See the program for scheduling the threads

THREADS

Page 39: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

39

Scheduling Threads

THREADS

Return

Summaryint pthread_setschedparam(

pthread_t target_thread, int policy,

const struct sched_param *param );

Include File(s)

>pthread.h<

Manual Section3

SuccessFailureSets errno

0 Nonzero

Table 11.8. The pthread_setschedparam Library Function.

Page 40: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

40

Scheduling Threads

The first argument of pthread_setschedparam is a valid thread ID.

The second argument should be one of the following defined constants:

SCHED_OTHER SCHED_FIFO SCHED_RR

The third argument for this call is a reference to a sched_param structure.

THREADS

Page 41: 1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:

41

Scheduling Threads

If successful, the pthread_setschedparam library returns a 0; otherwise, it returns one of the following values:

EPERM (1), the calling process does not have superuser privileges;

ESRCH (3), the specified thread does not exist; EFAULT (14), param references an illegal

address; EINVAL (22), invalid policy or param value or

priority value is inconsistent with scheduling policy.

THREADS