Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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 THREADS

3 3 Threads THREADS

4 4 Thread Definition  One common programming methodology is to divide the problem into smaller specific subtasks.  When each of the subtasks (processes) is finished, its solved portion of the problem is integrated into the whole to provide an overall solution.  This approach is the basis for distributed computation. THREADS

5 5 Thread Definition  Generating and managing individual processes consume system resources.  During process execution lifetime, the system must keep track of the current state, program counter, memory usage, file descriptors, signal tables, and other details of the process.  When the process exits, the operating system must remove the process-associated data structures and return the process's status information to its parent process. THREADS

6 6 Thread Definition  Conceptually, a thread is a distinct sequence of execution steps performed within a process.  Threads have the ability to simultaneously take different execution paths through their process space. THREADS

7 7 Thread Definition  In a traditional setting, there is a single thread of control.  The thread starts at the first statement and continues through the program logic in a serial manner until the process finishes.  In a multithreading (MT) setting, there can be more than one thread of control active within a process, each progressing concurrently. THREADS

8 8 Thread Definition  There are certain problem types that can have a multithreaded solution.  Problems that inherently consist of multiple, independent tasks are prime candidates. For example:  monitor programs that manage a large number of simultaneous connections and concurrent window displays  Producer—Consumer type problems  Client—Server type problems  Some numerical computations (such as matrix multiplication) that can be divided into separate subtasks THREADS

9 9 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

10 10 Thread Definition  In a multithreaded process each thread executes independently and asynchronously.  However, many tasks handled by threads have sequences that must be done serially. When these sequences are encountered, synchronization problems concerning data access—updating—can arise.  The term sibling is sometimes used to denote other peer threads, as there is no inherent parent/child relationship with threads. THREADS

11 11 Thread Definition  Figure 11.1 compares communications of single thread processes versus multiple threads process. THREADS

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

13 13 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

14 14 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

15 15 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

16 16 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

17 17 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

18 18 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

19 19 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

20 20 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

21 21 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

22 22 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

23 23 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

24 24 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

25 25 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

26 26 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

27 27 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

28 28 Basic Thread Management  Once a thread is detached, other threads can no longer synchronize their activities based on its termination. THREADS


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

Similar presentations


Ads by Google