Home Page Icon
Home Page
Table of Contents for
II. Asynchronous Events
Close
II. Asynchronous Events
by Steven Robbins, Kay A. Robbins
Unix™ Systems Programming: Communication, Concurrency, and Threads
Copyright
Dedication
About the Web Site
Preface
Acknowledgments
I. Fundamentals
1. Technology’s Impact on Programs
1.1. Terminology of Change
1.2. Time and Speed
1.3. Multiprogramming and Time Sharing
1.4. Concurrency at the Applications Level
1.4.1. Interrupts
1.4.2. Signals
1.4.3. Input and output
1.4.4. Processes, threads and the sharing of resources
1.4.5. Multiple processors with shared memory
1.4.6. The network as the computer
1.5. Security and Fault Tolerance
1.6. Buffer Overflows for Breaking and Entering
1.6.1. Consequences of buffer overflows
1.6.2. Buffer overflows and security
1.7. UNIX Standards
1.8. Additional Reading
2. Programs, Processes and Threads
2.1. How a Program Becomes a Process
2.2. Threads and Thread of Execution
2.3. Layout of a Program Image
2.4. Library Function Calls
2.5. Function Return Values and Errors
2.6. Argument Arrays
2.6.1. Creating an argument array with makeargv
2.6.2. Implementation of makeargv
2.7. Thread-Safe Functions
2.8. Use of Static Variables
2.9. Structure of Static Objects
2.10. Process Environment
2.11. Process Termination
2.12. Exercise: An env Utility
2.13. Exercise: Message Logging
2.14. Additional Reading
3. Processes in UNIX
3.1. Process Identification
3.2. Process State
3.3. UNIX Process Creation and fork
3.4. The wait Function
3.4.1. Status values
3.5. The exec Function
3.6. Background Processes and Daemons
3.7. Critical Sections
3.8. Exercise: Process Chains
3.9. Exercise: Process Fans
3.10. Additional Reading
4. UNIX I/O
4.1. Device Terminology
4.2. Reading and Writing
4.3. Opening and Closing Files
4.4. The select Function
4.5. The poll Function
4.6. File Representation
4.6.1. File descriptors
4.6.2. File pointers and buffering
4.6.3. Inheritance of file descriptors
4.7. Filters and Redirection
4.8. File Control
4.9. Exercise: Atomic Logging
4.9.1. An atomic logging library
4.10. Exercise: A cat Utility
4.11. Additional Reading
5. Files and Directories
5.1. UNIX File System Navigation
5.1.1. The current working directory
5.1.2. Search paths
5.2. Directory Access
5.2.1. Accessing file status information
5.2.2. Determining the type of a file
5.3. UNIX File System Implementation
5.3.1. UNIX file implementation
5.3.2. Directory implementation
5.4. Hard Links and Symbolic Links
5.4.1. Creating or removing a link
5.4.2. Creating and removing symbolic links
5.5. Exercise: The which Command
5.6. Exercise: Biffing
5.7. Exercise: News biff
5.8. Exercise: Traversing Directories
5.9. Additional Reading
6. UNIX Special Files
6.1. Pipes
6.2. Pipelines
6.3. FIFOs
6.4. Pipes and the Client-Server Model
6.5. Terminal Control
6.5.1. Canonical and noncanonical input processing
6.6. Audio Device
6.7. Exercise: Audio
6.8. Exercise: Barriers
6.9. Exercise: The stty Command
6.10. Exercise: Client-Server Revisited
6.11. Additional Reading
7. Project: The Token Ring
7.1. Ring Topology
7.2. Ring Formation
7.3. Ring Exploration
7.4. Simple Communication
7.5. Mutual Exclusion with Tokens
7.6. Mutual Exclusion by Voting
7.7. Leader Election on an Anonymous Ring
7.8. Token Ring for Communication
7.9. Pipelined Preprocessor
7.10. Parallel Ring Algorithms
7.10.1. Image filtering
Filtering algorithms on the ring
A bidirectional ring
Block computation
7.10.2. Matrix multiplication
7.11. Flexible Ring
7.12. Additional Reading
II. Asynchronous Events
8. Signals
8.1. Basic Signal Concepts
8.2. Generating Signals
8.3. Manipulating Signal Masks and Signal Sets
8.4. Catching and Ignoring Signals—sigaction
8.5. Waiting for Signals—pause, sigsuspend and sigwait
8.5.1. The pause function
8.5.2. The sigsuspend function
8.5.3. The sigwait function
8.6. Handling Signals: Errors and Async-signal Safety
8.7. Program Control with siglongjmp and sigsetjmp
8.8. Programming with Asynchronous I/O
8.9. Exercise: Dumping Statistics
8.10. Exercise: Spooling a Slow Device
8.11. Additional Reading
9. Times and Timers
9.1. POSIX Times
9.1.1. Expressing time in seconds since the Epoch
9.1.2. Displaying date and time
9.1.3. Using struct timeval to express time
9.1.4. Using realtime clocks
9.1.5. Contrasting elapsed time to processor time
9.2. Sleep Functions
9.3. POSIX:XSI Interval Timers
9.4. Realtime Signals
9.5. POSIX:TMR Interval Timers
9.6. Timer Drift, Overruns and Absolute Time
9.7. Additional Reading
10. Project: Virtual Timers
10.1. Project Overview
10.2. Simple Timers
10.3. Setting One of Five Single Timers
10.3.1. The virtualtimers object
10.3.2. The hardwaretimer object
10.3.3. Main program implementation
10.3.4. Instrumentation of the timer code with show
10.4. Using Multiple Timers
10.4.1. Setting multiple timers
10.4.2. Testing with multiple timers
10.5. A Robust Implementation of Multiple Timers
10.6. POSIX:TMR Timer Implementation
10.7. mycron, a Small Cron Facility
10.8. Additional Reading
11. Project: Cracking Shells
11.1. Building a Simple Shell
11.2. Redirection
11.3. Pipelines
11.4. Signal Handling in the Foreground
11.5. Process Groups, Sessions and Controlling Terminals
11.5.1. Process Groups
11.5.2. Sessions
11.6. Background Processes in ush
11.7. Job Control
11.8. Job Control for ush
11.8.1. A job list object
11.8.2. The job list in ush
11.8.3. Job control in ush
11.8.4. Process behavior in waiting for a pipeline
11.9. Additional Reading
III. Concurrency
12. POSIX Threads
12.1. A Motivating Problem: Monitoring File Descriptors
12.2. Use of Threads to Monitor Multiple File Descriptors
12.3. Thread Management
12.3.1. Referencing threads by ID
12.3.2. Creating a thread
12.3.3. Detaching and joining
12.3.4. Exiting and cancellation
12.3.5. Passing parameters to threads and returning values
12.4. Thread Safety
12.5. User Threads versus Kernel Threads
12.6. Thread Attributes
12.6.1. The thread state
12.6.2. The thread stack
12.6.3. Thread scheduling
12.7. Exercise: Parallel File Copy
12.8. Additional Reading
13. Thread Synchronization
13.1. POSIX Synchronization Functions
13.2. Mutex Locks
13.2.1. Creating and initializing a mutex
13.2.2. Destroying a mutex
13.2.3. Locking and unlocking a mutex
13.2.4. Protecting unsafe library functions
13.2.5. Synchronizing flags and global values
13.2.6. Making data structures thread-safe
13.3. At-Most-Once and At-Least-Once-Execution
13.4. Condition Variables
13.4.1. Creating and destroying condition variables
13.4.2. Waiting and signaling on condition variables
13.5. Signal Handling and Threads
13.5.1. Directing a signal to a particular thread
13.5.2. Masking signals for threads
13.5.3. Dedicating threads for signal handling
13.6. Readers and Writers
13.7. A strerror_r Implementation
13.8. Deadlocks and Other Pesky Problems
13.9. Exercise: Multiple Barriers
13.10. Additional Reading
14. Critical Sections and Semaphores
14.1. Dealing with Critical Sections
14.2. Semaphores
14.3. POSIX:SEM Unnamed Semaphores
14.4. POSIX:SEM Semaphore Operations
14.5. POSIX:SEM Named Semaphores
14.5.1. Creating and opening named semaphores
14.5.2. Closing and unlinking named semaphores
14.6. Exercise: License Manager
14.6.1. License object
14.6.2. The runsim main program
14.6.3. Extensions to the license manager
14.7. Additional Reading
15. POSIX IPC
15.1. POSIX:XSI Interprocess Communication
15.1.1. Identifying and accessing IPC objects
15.1.2. Accessing POSIX:XSI IPC resources from the shell
15.2. POSIX:XSI Semaphore Sets
15.2.1. Semaphore creation
15.2.2. Semaphore control
15.2.3. POSIX semaphore set operations
15.3. POSIX:XSI Shared Memory
15.3.1. Accessing a shared memory segment
15.3.2. Attaching and detaching a shared memory segment
15.3.3. Controlling shared memory
15.3.4. Shared memory examples
15.4. POSIX:XSI Message Queues
15.4.1. Accessing a message queue
15.5. Exercise: POSIX Unnamed Semaphores
15.6. Exercise: POSIX Named Semaphores
15.7. Exercise: Implementing Pipes with Shared Memory
15.8. Exercise: Implementing Pipes with Message Queues
15.9. Additional Reading
16. Project: Producer Consumer Synchronization
16.1. The Producer-Consumer Problem
16.2. Bounded Buffer Protected by Mutex Locks
16.3. Buffer Implementation with Semaphores
16.4. Introduction to a Simple Producer-Consumer Problem
16.5. Bounded Buffer Implementation Using Condition Variables
16.6. Buffers with Done Conditions
16.7. Parallel File Copy
16.7.1. Parallel file copy producer
16.7.2. Parallel file copy consumer
16.7.3. Parallel file copy main program
16.7.4. Parallel file copy enhancements
16.8. Threaded Print Server
16.8.1. The request buffer
16.8.2. The producer thread
16.8.3. The consumer threads
16.8.4. The print server
16.8.5. Other enhancements
16.9. Additional Reading
17. Project: The Not Too Parallel Virtual Machine
17.1. PVM History, Terminology, and Architecture
17.2. The Not Too Parallel Virtual Machine
17.3. NTPVM Project Overview
17.3.1. NEWTASK packets
17.3.2. DATA packets
17.3.3. DONE packets
17.4. I/O and Testing of Dispatcher
17.4.1. Testing with multiple windows
17.4.2. Testing with remote logging
17.5. Single Task with No Input
17.6. Sequential Tasks
17.6.1. The input thread
17.6.2. The output thread
17.7. Concurrent Tasks
17.8. Packet Communication, Broadcast and Barriers
17.9. Termination and Signals
17.10. Ordered Message Delivery
17.11. Additional Reading
IV. Communication
18. Connection-Oriented Communication
18.1. The Client-Server Model
18.2. Communication Channels
18.3. Connection-Oriented Server Strategies
18.4. Universal Internet Communication Interface (UICI)
18.4.1. Handling errors
18.4.2. Reading and writing
18.5. UICI Implementations of Different Server Strategies
18.6. UICI Clients
18.7. Socket Implementation of UICI
18.7.1. The socket function
18.7.2. The bind function
18.7.3. The listen function
18.7.4. Implementation of u_open
18.7.5. The accept function
18.7.6. Implementation of u_accept
18.7.7. The connect function
18.7.8. Implementation of u_connect
18.8. Host Names and IP Addresses
18.9. Thread-Safe UICI
18.10. Exercise: Ping Server
18.11. Exercise: Transmission of Audio
18.12. Additional Reading
19. Project: WWW Redirection
19.1. The World Wide Web
19.2. Uniform Resource Locators (URLs)
19.3. HTTP Primer
19.3.1. Client requests
19.3.2. Server response
19.3.3. HTTP message exchange
19.4. Web Communication Patterns
19.4.1. Tunnels
19.4.2. Proxies
19.4.3. Caching and Transparency
19.4.4. Gateways
19.5. Pass-through Monitoring of Single Connections
19.6. Tunnel Server Implementation
19.7. Server Driver for Testing
19.8. HTTP Header Parsing
19.9. Simple Proxy Server
19.10. Proxy Monitor
19.11. Proxy Cache
19.12. Gateways as Portals
19.13. Gateway for Load Balancing
19.14. Postmortem
19.14.1. Threading and timing errors
19.14.2. Uncaught errors and bad exits
19.14.3. Writing style and presentation
19.14.4. Poor testing and presentation of results
19.14.5. Programming errors and bad style
19.15. Additional Reading
20. Connectionless Communication and Multicast
20.1. Introduction to Connectionless Communication
20.2. Simplified Interface for Connectionless Communication
20.2.1. Host names and the u_buf_t structure
20.2.2. UICI UDP return errors
20.2.3. UDP buffer size and UICI UDP
20.3. Simple-Request Protocols
20.4. Request-Reply Protocols
20.5. Request-Reply with Timeouts and Retries
20.6. Request-Reply-Acknowledge Protocols
20.7. Implementation of UICI UDP
20.7.1. Implementation of u_openudp
20.7.2. The sendto function
20.7.3. Implementation of u_sendto and u_sendtohost
20.7.4. The recvfrom function
20.7.5. Implementation of u_recvfrom and u_recvfromtimed
20.7.6. Host names and u_buf_t
20.8. Comparison of UDP and TCP
20.9. Multicast
20.9.1. Multicast Addressing
20.9.2. Implementation of u_join
20.9.3. Implementation of u_leave
20.10. Exercise: UDP Port Server
20.11. Exercise: Stateless File Server
20.11.1. Remote File Services
20.12. Additional Reading
21. Project: Internet Radio
21.1. Project Overview
21.2. Audio Device Simulation
21.3. UDP Implementation with One Program and One Receiver
21.3.1. Simple implementation
21.3.2. Termination of the receiver
21.3.3. Buffering by the receiver to handle network latency
21.3.4. Buffering by the receiver to handle out-of-order delivery
21.4. UDP Implementation with Multiple Programs and Receivers
21.4.1. Multiple programs and one receiver
21.4.2. Multiple programs and multiple receivers
21.5. UDP Implementation of Radio Broadcasts
21.6. Multicast Implementation of Radio Broadcasts
21.7. TCP Implementation Differences
21.7.1. TCP implementation of one program and one receiver
21.7.2. TCP implementation of multiple programs with one receiver
21.7.3. TCP implementation of radio broadcasts
21.8. Receiving Streaming Audio Through a Browser
21.8.1. Using browser helper applications
21.8.2. Setting a new mime type in your web server
21.8.3. Setting your browser to handle a new mime type
21.8.4. Creating a web page
21.8.5. Using a predefined mime type
21.9. Additional Reading
22. Project: Server Performance
22.1. Server Performance Costs
22.2. Server Architectures
22.3. Project Overview
22.4. Single-Client Driver
22.4.1. Processing a connection
22.4.2. Programming the response
22.4.3. Gathering statistics
22.4.4. Testing the client
22.5. Multiple-Client Driver
22.5.1. Alternative multiple-client design
22.6. Thread-per-request and Process-per-request Implementations
22.7. Thread-worker-pool Strategy
22.8. Thread-worker Pool with Bounded Buffer
22.9. Process-worker Pool
22.10. Influence of Disk I/O
22.11. Performance Studies
22.11.1. Baseline measurements
22.11.2. Sources of variability
22.11.3. Measurement errors
22.11.4. Synchronization
22.11.5. Just plain errors
22.11.6. What to measure?
22.11.7. Data analysis and presentation
22.12. Report Writing
22.12.1. Introduction
22.12.2. Design, implementation and testing
22.12.3. Experiments
22.12.4. Results and analysis
22.12.5. Conclusion
22.12.6. Bibliography
22.13. Additional Reading
Appendices
A. UNIX Fundamentals
A.1. Manual Pages
A.2. Compilation
A.2.1. Header files
A.2.2. Linking and libraries
A.2.3. Macros and conditional compilation
A.3. Makefiles
A.4. Debugging Aids
A.4.1. The lint utility
A.4.2. Debuggers
A.4.3. The truss utility
A.4.4. Profilers
A.5. Identifiers, Storage Classes and Linkage Classes
A.6. Additional Reading
B. Restart Library
C. UICI Implementation
C.1. Connection-Oriented UICI TCP Implementation
C.2. Name Resolution Implementations
C.2.1. Implementation with gethostbyaddr and gethostbyname
C.2.2. Reentrant versions of name resolution functions
C.2.3. Reentrant name resolution with mutex locks
C.3. Connectionless UICI UDP Implementation
D. Logging Functions
D.1. Local Atomic Logging
D.2. Remote Logging
D.2.1. Use of the remote logging facility
D.2.2. Implementation details
E. POSIX Extensions
Bibliography
Search in book...
Toggle Font Controls
Playlists
Add To
Create new playlist
Name your new playlist
Playlist description (optional)
Cancel
Create playlist
Sign In
Email address
Password
Forgot Password?
Create account
Login
or
Continue with Facebook
Continue with Google
Sign Up
Full Name
Email address
Confirm Email Address
Password
Login
Create account
or
Continue with Facebook
Continue with Google
Prev
Previous Chapter
7. Project: The Token Ring
Next
Next Chapter
8. Signals
Part II. Asynchronous Events
Add Highlight
No Comment
..................Content has been hidden....................
You can't read the all page of ebook, please click
here
login for view all page.
Day Mode
Cloud Mode
Night Mode
Reset