PThreads Programming. A POSIX Standard for Better Multiprocessing - Helion
ISBN: 978-14-493-6474-8
stron: 286, Format: ebook
Data wydania: 2016-03-18
Księgarnia: Helion
Cena książki: 118,15 zł (poprzednio: 137,38 zł)
Oszczędzasz: 14% (-19,23 zł)
Computers are just as busy as the rest of us nowadays. They have lots of tasks to do at once, and need some cleverness to get them all done at the same time.That's why threads are seen more and more often as a new model for programming. Threads have been available for some time. The Mach operating system, the Distributed Computer Environment (DCE), and Windows NT all feature threads.One advantage of most UNIX implementations, as well as DCE, is that they conform to a recently ratified POSIX standard (originally 1003.4a, now 1003.1c), which allows your programs to be portable between them. POSIX threads are commonly known as pthreads, after the word that starts all the names of the function calls. The standard is supported by Solaris, OSF/1, AIX, and several other UNIX-based operating systems.The idea behind threads programming is to have multiple tasks running concurrently within the same program. They can share a single CPU as processes do, or take advantage of multiple CPUs when available. In either case, they provide a clean way to divide the tasks of a program while sharing data.A window interface can read input on dozens of different buttons, each responsible for a separate task. A network server has to accept simultaneous calls from many clients, providing each with reasonable response time. A multiprocessor runs a number-crunching program on several CPUs at once, combining the results when all are done. All these kinds of applications can benefit from threads.In this book you will learn not only what the pthread calls are, but when it is a good idea to use threads and how to make them efficient (which is the whole reason for using threads in the first place). The authors delves into performance issues, comparing threads to processes, contrasting kernel threads to user threads, and showing how to measure speed. He also describes in a simple, clear manner what all the advanced features are for, and how threads interact with the rest of the UNIX system.Topics include:
- Basic design techniques
- Mutexes, conditions, and specialized synchronization techniques
- Scheduling, priorities, and other real-time issues
- Cancellation
- UNIX libraries and re-entrant routines
- Signals
- Debugging tips
- Measuring performance
- Special considerations for the Distributed Computing Environment (DCE)
Osoby które kupowały "PThreads Programming. A POSIX Standard for Better Multiprocessing", wybierały także:
- D3.js w akcji 67,42 zł, (20,90 zł -69%)
- Tablice informatyczne. Node.js 16,86 zł, (5,90 zł -65%)
- React i GatsbyJS. Kurs video. Nowoczesne strony internetowe w oparciu o headless CMS i GraphQL 148,98 zł, (59,59 zł -60%)
- TypeScript i React. Kurs video. Tworzenie nowoczesnych komponent 129,00 zł, (51,60 zł -60%)
- Vue.js w praktyce. Kurs video. Poznaj narz 148,98 zł, (59,59 zł -60%)
Spis treści
PThreads Programming. A POSIX Standard for Better Multiprocessing eBook -- spis treści
- Pthreads Programming
- A Note Regarding Supplemental Files
- Examples
- Preface
- Organization
- Example Programs
- FTP
- Typographical Conventions
- Acknowledgments
- 1. Why Threads?
- What Are Pthreads?
- Potential Parallelism
- Specifying Potential Parallelism in a Concurrent Programming Environment
- UNIX Concurrent Programming: Multiple Processes
- Creating a new process: fork
- Pthreads Concurrent Programming: Multiple Threads
- Creating a new thread: pthread_create
- Threads are peers
- UNIX Concurrent Programming: Multiple Processes
- Parallel vs. Concurrent Programming
- Synchronization
- Sharing Process Resources
- Communication
- Scheduling
- Who Am I? Who Are You?
- Terminating Thread Execution
- Exit Status and Return Values
- Pthreads Library Calls and Errors
- Why Use Threads Over Processes?
- A Structured Programming Environment
- Choosing Which Applications to Thread
- 2. Designing Threaded Programs
- Suitable Tasks for Threading
- Models
- Boss/Worker Model
- Peer Model
- Pipeline Model
- Buffering Data Between Threads
- Some Common Problems
- Performance
- Example: An ATM Server
- The Serial ATM Server
- Handling asynchronous events: blocking with select
- Handling file I/O: blocking with read/write
- The Multithreaded ATM Server
- Model: boss/worker model
- The boss thread
- Dynamically detaching a thread
- A worker thread
- Synchronization: whats needed
- Future enhancements
- The Serial ATM Server
- Example: A Matrix Multiplication Program
- The Serial Matrix-Multiply Program
- The Multithreaded Matrix-Multiply Program
- Passing data to a new thread
- Synchronization in the matrix-multiply program
- 3. Synchronizing Pthreads
- Selecting the Right Synchronization Tool
- Mutex Variables
- Using Mutexes
- Error Detection and Return Values
- Using pthread_mutex_trylock
- When Other Tools Are Better
- Some Shortcomings of Mutexes
- Contention for a Mutex
- Example: Using Mutexes in a Linked List
- Complex Data Structures and Lock Granularity
- Requirements and Goals for Synchronization
- Access Patterns and Granularity
- Locking Hierarchies
- Sharing a Mutex Among Processes
- Condition Variables
- Using a Mutex with a Condition Variable
- When Many Threads Are Waiting
- Checking the Condition on Wake Up: Spurious Wake Ups
- Condition Variable Attributes
- Condition Variables and UNIX Signals
- Condition Variables and Cancellation
- Reader/Writer Locks
- Synchronization in the ATM Server
- Synchronizing Access to Account Data
- Limiting the Number of Worker Threads
- Synchronizing a Server Shutdown
- Thread Pools
- An ATM Server Example That Uses a Thread Pool
- Initializing a thread pool
- Checking for work
- Adding work
- Deleting a thread pool
- Adapting the atm_server_init and main routines
- An ATM Server Example That Uses a Thread Pool
- 4. Managing Pthreads
- Setting Thread Attributes
- Setting a Threads Stack Size
- Specifying the Location of a Threads Stack
- Setting a Threads Detached State
- Setting Multiple Attributes
- Destroying a Thread Attribute Object
- The pthread_once Mechanism
- Example: The ATM Servers Communication Module
- Using a statically initialized mutex
- Using the pthread_once mechanism
- Example: The ATM Servers Communication Module
- Keys: Using Thread-Specific Data
- Initializing a Key: pthread_key_create
- Associating Data with a Key
- Retrieving Data from a Key
- Destructors
- Cancellation
- The Complication with Cancellation
- Cancelability Types and States
- Cancellation Points: More on Deferred Cancellation
- A Simple Cancellation Example
- The bullet_proof thread: no effect
- The ask_for_it thread: deferred cancellation
- The sitting_duck thread: asynchronous cancellation
- Cleanup Stacks
- Cancellation in the ATM Server
- Aborting a deposit
- Scheduling Pthreads
- Scheduling Priority and Policy
- Scheduling Scope and Allocation Domains
- Runnable and Blocked Threads
- Scheduling Priority
- Scheduling Policy
- Using Priorities and Policies
- Setting Scheduling Policy and Priority
- Inheritance
- Scheduling in the ATM Server
- Mutex Scheduling Attributes
- Priority Ceiling
- Priority Inheritance
- The ATM Example and Priority Inversion
- Setting Thread Attributes
- 5. Pthreads and UNIX
- Threads and Signals
- Traditional Signal Processing
- Sending signals and waiting for signals
- Using a signal mask to block signals
- Signal Processing in a Multithreaded World
- Synchronously generated signals
- Asynchronously generated signals
- Per-thread signal masks
- Per-process signal actions
- Putting it all together
- Threads in Signal Handlers
- A Simple Example
- Some Signal Issues
- Handling Signals in the ATM Example
- Traditional Signal Processing
- Threadsafe Library Functions and System Calls
- Threadsafe and Reentrant Functions
- Example of Thread-Unsafe and Threadsafe Versions of the Same Function
- Functions That Return Pointers to Static Data
- Library Use of errno
- The Pthreads Standard Specifies Which Functions Must Be Threadsafe
- Alternative interfaces for functions that return static data
- Additional routines for performance considerations
- File-locking functions for threads
- Where are the threadsafe functions?
- Using Thread-Unsafe Functions in a Multithreaded Program
- Cancellation-Safe Library Functions and System Calls
- Asynchronous Cancellation-Safe Functions
- Cancellation Points in System and Library Calls
- Thread-Blocking Library Functions and System Calls
- Threads and Process Management
- Calling fork from a Thread
- Fork-handling stacks
- Calling exec from a Thread
- Process Exit and Threads
- Calling fork from a Thread
- Multiprocessor Memory Synchronization
- Threads and Signals
- 6. Practical Considerations
- Understanding Pthreads Implementation
- Two Worlds
- Two Kinds of Threads
- Whos Providing the Thread?
- User-space Pthreads implementations
- Kernel threadbased Pthreads implementations
- Two-level scheduler Pthreads implementations: the best of both worlds
- Debugging
- Deadlock
- Race Conditions
- Event Ordering
- Less Is Better
- Trace Statements
- Debugger Support for Threads
- Example: Debugging the ATM Server
- Debugging a deadlock caused by a missing unlock
- Debugging a race condition caused by a missing lock
- Performance
- The Costs of Sharing Too MuchLocking
- Thread Overhead
- Thread context switches
- Synchronization Overhead
- How Do Your Threads Spend Their Time?
- Performance in the ATM Server Example
- Performance depends on input workload: increasing clients and contention
- Performance depends on a good locking strategy
- Performance depends on the type of work threads do
- Key performance issues between using threads and using processes
- Conclusion
- Understanding Pthreads Implementation
- A. Pthreads and DCE
- The Structure of a DCE Server
- What Does the DCE Programmer Have to Do?
- Example: The ATM as a DCE Server
- B. Pthreads Draft 4 vs. the Final Standard
- Detaching a Thread
- Mutex Variables
- Condition Variables
- Thread Attributes
- The pthread_once Function
- Keys
- Cancellation
- Scheduling
- Signals
- Threadsafe System Interfaces
- Error Reporting
- System Interfaces and Cancellation-Safety
- Process-Blocking Calls
- Process Management
- C. Pthreads Quick Reference
- D. About the Authors
- Index
- About the Authors
- Colophon
- Copyright