Download presentation
Presentation is loading. Please wait.
Published byFranklin Short Modified over 9 years ago
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: January 13, 2003
2
2 Topics Thread Definition Creating a Thread Exiting a Thread Basic Thread Management Thread Attributes Scheduling Threads THREADS
3
3 Threads THREADS
4
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
5
5 Thread Definition Figure 11.1 compares communications of single thread processes versus multiple threads process. THREADS
6
6 Thread Definition THREADS Figure 11.1. Conceptual communications—multiple single-threaded processes versus a single process with multiple threads.
7
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
8
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
9
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
10
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
11
11 Creating a Thread Return int pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg); Summary 3Manual Section Include File(s) Sets errnoFailureSuccess Nonzero0 Table 11.1. The pthread_create Library Function. THREADS
12
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
13
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
14
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
15
15 Exiting a Thread Return void pthread_exit (void * retval);Summary 3Manual Section Include File(s) Sets errnoFailureSuccess This call does not return Table 11.2. The pthread_exit Library Function. THREADS
16
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
17
17 Basic Thread Management Return int pthread_join( pthread_t target_thread, void **status ); Summary 3Manual Section Include File(s) Sets errnoFailureSuccess Nonzero0 Table 11.3. The pthread_join Library Function. THREADS
18
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
19
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
20
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
21
21 Basic Thread Management Return int pthread_detach (pthread_t threadID);Summary 3Manual Section Include File(s) Sets errnoFailureSuccess Nonzero0 Table 11.4. The pthread_detach Library Function. THREADS
22
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. Exp 6.1 THREADS
23
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
24
24 Thread Attributes Return int pthread_attr_init ( pthread_attr_t *attr );Summary 3Manual Section Include File(s) Sets errnoFailureSuccess Nonzero0 Table 11.5. The pthread_attr_init Library Function. THREADS
25
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
26
26 Thread Attributes CommentsDefaultAttribute 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_JOINABL E detachstate Indicates whether or not scheduling attributes are inherited from parent thread or set explicitly by the attribute object. PTHREAD_EXPLICIT_SCHEDinheritsched Scheduling parameters (priority).0schedparam Scheduling is determined by the system (most often some sort of timesharing). Note the missing PTHREAD_prefix. SCHED_OTHERschedpolicy Scope of scheduling contention—with all threads in same process or all processes in the system. PTHREAD_SCOPE_SYSTEscope Table 11.6. Thread Attributes and Default Settings. THREADS
27
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
28
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
29
29 Thread Attributes Defined Constants for the 2nd setxxx parameter Set and get CallsAttribute 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 THREADS
30
30 Thread Attributes Defined Constants for the 2nd setxxx parameter Set and get CallsAttribute 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 THREADS
31
31 Thread Attributes Defined Constants for the 2nd setxxx parameter Set and get CallsAttribute 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 THREADS
32
32 Thread Attributes Defined Constants for the 2nd setxxx parameter Set and get CallsAttribute 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 THREADS
33
33 Thread Attributes Defined Constants for the 2nd setxxx parameter Set and get CallsAttribute 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 THREADS
34
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
35
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
36
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
37
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
38
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 Exp 6.2 THREADS
39
39 Scheduling Threads THREADS Return int pthread_setschedparam( pthread_t target_thread, int policy, const struct sched_param *param ); Summary 3Manual Section Include File(s) Sets errnoFailureSuccess Nonzero0 Table 11.8. The pthread_setschedparam Library Function.
40
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
41
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.