Threads - PowerPoint PPT Presentation

About This Presentation
Title:

Threads

Description:

In a POSIX implementation, if we want to generate a thread that does not have ... Scheduling of a POSIX thread in Linux is determined by priority and scheduling ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 42
Provided by: abdya
Category:
Tags: posix | threads

less

Transcript and Presenter's Notes

Title: Threads


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
2
Topics
THREADS
Thread Definition Creating a Thread Exiting a Thread Basic Thread Management Thread Attributes Scheduling Threads
3
Threads
THREADS

4
Thread Definition
THREADS
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.
5
Thread Definition
THREADS
Figure 11.1 compares communications of single thread processes versus multiple threads process.
6
Thread Definition
THREADS

Figure 11.1. Conceptual communicationsmultiple
single-threaded processes versus a single process
with multiple threads.
7
Thread Definition
THREADS
At system implementation level there are two traditional approaches or models used to generate and manage threads. User-level model Kernel-level model
8
Thread Definition
THREADS
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.
9
Thread Definition
THREADS
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
10
Creating a Thread
THREADS
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.)
11
Creating a Thread
THREADS

3 Manual Section ltpthread.hgt Include File(s)
int pthread_create(pthread_t thread, pthread_attr_t attr, void (start_routine)(void ), void arg) Summary
Return
Sets errno Failure Success
Nonzero 0
Table 11.1. The pthread_create Library Function.
12
Creating a Thread
THREADS
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.
13
Creating a Thread
THREADS
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.
14
Exiting a Thread
THREADS
The pthread_exit library call terminates a thread in much the same manner as a call to exit terminates a process. (Table 11.2.)
15
Exiting a Thread
THREADS

3 Manual Section ltpthread.hgt Include File(s)
void pthread_exit (void retval) Summary
Return
Sets errno Failure Success
This call does not return
Table 11.2. The pthread_exit Library Function.
16
Basic Thread Management
THREADS
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.)
17
Basic Thread Management
THREADS

3 Manual Section ltpthread.hgt Include File(s)
int pthread_join( pthread_t target_thread, void status ) Summary
Return
Sets errno Failure Success
Nonzero 0
Table 11.3. The pthread_join Library Function.
18
Basic Thread Management
THREADS
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.
19
Basic Thread Management
THREADS
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.
20
Basic Thread Management
THREADS
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)
21
Basic Thread Management
THREADS

3 Manual Section ltpthread.hgt Include File(s)
int pthread_detach (pthread_t threadID) Summary
Return
Sets errno Failure Success
Nonzero 0
Table 11.4. The pthread_detach Library Function.
22
Basic Thread Management
THREADS
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.
23
Thread Attributes
THREADS
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)
24
Thread Attributes
THREADS

3 Manual Section ltpthread.hgt Include File(s)
int pthread_attr_init ( pthread_attr_t attr ) Summary
Return
Sets errno Failure Success
Nonzero 0
Table 11.5. The pthread_attr_init Library
Function.
25
Thread Attributes
THREADS
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)
26
Thread Attributes
THREADS

Comments Default Attribute
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. PTHREAD_CREATE_JOINABLE detachstate
Indicates whether or not scheduling attributes are inherited from parent thread or set explicitly by the attribute object. PTHREAD_EXPLICIT_SCHED inheritsched
Scheduling parameters (priority). 0 schedparam
Scheduling is determined by the system (most often some sort of timesharing). Note the missing PTHREAD_prefix. SCHED_OTHER schedpolicy
Scope of scheduling contentionwith all threads in same process or all processes in the system. PTHREAD_SCOPE_SYSTE scope
Table 11.6. Thread Attributes and Default
Settings.
27
Thread Attributes
THREADS
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.
28
Thread Attributes
THREADS
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)
29
Thread Attributes
THREADS

Defined Constants for the 2nd setxxx parameter Set and get Calls Attribute
PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_DETACHED int pthread_attr_setdetachstate ( pthread_attr_t attr, int detachstate) int pthread_attr_getdetachstate ( const pthread_attr_t attr, int detachstate) detachstate
Table 11.7. Thread Attribute Set and Get functions
30
Thread Attributes
THREADS

Defined Constants for the 2nd setxxx parameter Set and get Calls Attribute
PTHREAD_EXPLICIT_SCHED PTHREAD_INHERIT_SCHED int pthread_attr_setinheritsched ( pthread_attr_t attr, int inheritsched int pthread_attr_getinheritsched ( const pthread_attr_t attr, int inheritsched) inheritsched
Table 11.7. Thread Attribute Set and Get functions
31
Thread Attributes
THREADS

Defined Constants for the 2nd setxxx parameter Set and get Calls Attribute
0 Reference to valid sched_param structure with its sched_priority member assigned a valid priority. int 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) schedparam
Table 11.7. Thread Attribute Set and Get functions
32
Thread Attributes
THREADS

Defined Constants for the 2nd setxxx parameter Set and get Calls Attribute
SCHED_OTHER SCHED_FIFO SCHED_RR int pthread_attr_setschedpolicy ( pthread_attr_t attr, int policy) int pthread_attr_getschedpolicy ( const pthread_attr_t attr, int policy) schedpolicy
Table 11.7. Thread Attribute Set and Get functions
33
Thread Attributes
THREADS

Defined Constants for the 2nd setxxx parameter Set and get Calls Attribute
PTHREAD_SCOPE_SYSTEM PTHREAD_SCOPE_PROCESS int pthread_attr_setscope ( pthread_attr_t attr, int contentionscope) int pthread_attr_getscope ( const pthread_attr_t attr, int contentionscope) scope
Table 11.7. Thread Attribute Set and Get functions
34
Scheduling Threads
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
35
Scheduling Threads
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.
36
Scheduling Threads
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.
37
Scheduling Threads
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.
38
Scheduling Threads
THREADS
Three scheduling policies for threads SCHED_OTHER (system default) It is a time sharing policy in which all threads with same static priority 0 receives a time slice. SCHED_FIFO First in first out 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
39
Scheduling Threads
THREADS

3 Manual Section ltpthread.hgt Include File(s)
int pthread_setschedparam( pthread_t target_thread, int policy, const struct sched_param param ) Summary
Return
Sets errno Failure Success
Nonzero 0
Table 11.8. The pthread_setschedparam Library
Function.
40
Scheduling Threads
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.
41
Scheduling Threads
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.
Write a Comment
User Comments (0)
About PowerShow.com