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

Slides:



Advertisements
Similar presentations
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Advertisements

Chapter 4 Threads Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William.
Threads. What do we have so far The basic unit of CPU utilization is a process. To run a program (a sequence of code), create a process. Processes are.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
Chapter 4 Threads, SMP, and Microkernels Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall Operating Systems: Internals and Design.
Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)
Day 10 Threads. Threads and Processes  Process is seen as two entities Unit of resource allocation (process or task) Unit of dispatch or scheduling (thread.
Lecture 4: Concurrency and Threads CS 170 T Yang, 2015 Chapter 4 of AD textbook.
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
Fork Fork is used to create a child process. Most network servers under Unix are written this way Concurrent server: parent accepts the connection, forks.
3.5 Interprocess Communication
 2004 Deitel & Associates, Inc. All rights reserved. Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
A. Frank - P. Weisberg Operating Systems Threads Implementation.
Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place  (address space + resources) –Thread is a program.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
Chapter 4: Threads Adapted to COP4610 by Robert van Engelen.
Chapter 4: Threads. From Processes to Threads 4.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads Changes by MA Doman 2013.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Threads A thread (or lightweight process) is a basic unit of CPU.
Chapter 4 Threads, SMP, and Microkernels Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall Operating Systems: Internals and Design.
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/16/ Realizing Concurrency using the thread model B. Ramamurthy.
 2004 Deitel & Associates, Inc. All rights reserved. 1 Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for.
B. RAMAMURTHY 10/24/ Realizing Concurrency using the thread model.
CS 346 – Chapter 4 Threads –How they differ from processes –Definition, purpose Threads of the same process share: code, data, open files –Types –Support.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
CS333 Intro to Operating Systems Jonathan Walpole.
Chapter 4 – Threads (Pgs 153 – 174). Threads  A "Basic Unit of CPU Utilization"  A technique that assists in performing parallel computation by setting.
Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 14 Threads 2 Read Ch.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
CSC 360, Instructor: Kui Wu Thread & PThread. CSC 360, Instructor: Kui Wu Agenda 1.What is thread? 2.User vs kernel threads 3.Thread models 4.Thread issues.
Threads A thread is an alternative model of program execution
Saurav Karmakar. Chapter 4: Threads  Overview  Multithreading Models  Thread Libraries  Threading Issues  Operating System Examples  Windows XP.
Operating Systems Unit 2: – Process Context switch Interrupt Interprocess communication – Thread Thread models Operating Systems.
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
1 Threads, SMP, and Microkernels Chapter 4. 2 Process Resource ownership - process includes a virtual address space to hold the process image Scheduling/execution-
Threads, SMP, and Microkernels Chapter 4. Processes and Threads Operating systems use processes for two purposes - Resource allocation and resource ownership.
Lecturer 3: Processes multithreaded Operating System Concepts Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess.
2.2 Threads  Process: address space + code execution  There is no law that states that a process cannot have more than one “line” of execution.  Threads:
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads  Overview  Multithreading Models  Threading Issues  Pthreads.
Contents 1.Overview 2.Multithreading Model 3.Thread Libraries 4.Threading Issues 5.Operating-system Example 2 OS Lab Sun Suk Kim.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
B. RAMAMURTHY 5/10/2013 Amrita-UB-MSES Realizing Concurrency using the thread model.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
Tutorial 4. In this tutorial session we’ll see Threads.
A thread is a basic unit of CPU utilization within a process Each thread has its own – thread ID – program counter – register set – stack It shares the.
Chapter 4 – Thread Concepts
Chapter 4 – Thread Concepts
Threads Threads.
Thread Programming.
Chapter 4: Threads.
Chapter 4 Threads.
Principles of Operating Systems Lecture 8
Threads & multithreading
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Thread Programming.
Multithreading Tutorial
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Presentation transcript:

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  Thread Definition  Creating a Thread  Exiting a Thread  Basic Thread Management THREADS

3 Threads THREADS

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

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

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 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 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 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 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 The pthread_create Library Function. THREADS

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 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 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 Exiting a Thread Return void pthread_exit (void * retval);Summary 3Manual Section Include File(s) Sets errnoFailureSuccess This call does not return Table The pthread_exit Library Function. THREADS

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 Basic Thread Management Return int pthread_join( pthread_t target_thread, void **status ); Summary 3Manual Section Include File(s) Sets errnoFailureSuccess Nonzero0 Table The pthread_join Library Function. THREADS

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 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 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 Basic Thread Management Return int pthread_detach (pthread_t threadID);Summary 3Manual Section Include File(s) Sets errnoFailureSuccess Nonzero0 Table The pthread_detach Library Function. THREADS

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