2009-11-02

Chapter 55: File locking

Previous chapters have covered various techniques that processes can use to synchronize their actions, including signals (Chapters 20, 21, and 22) and semaphores (Chapters 47 and 53). In this chapter, we look at further synchronization techniques designed specifically for use with files.

55 File Locking
55.1 Overview
55.2 File Locking with flock()
        55.2.1 Semantics of Lock Inheritance and Release
        55.2.2 Limitations of flock()
55.3 Record Locking with fcntl()
        55.3.1 Deadlock
        55.3.2 Example: An Interactive Locking Program
        55.3.3 Example: A Library of Locking Functions
        55.3.4 Lock Limits and Performance
        55.3.5 Semantics of Lock Inheritance and Release
        55.3.6 Lock Starvation and Priority of Queued Lock Requests
55.4 Mandatory Locking
55.5 The /proc/locks File
55.6 Running Just One Instance of a Program
55.7 Older Locking Techniques
55.8 Summary
55.9 Exercises

2009-11-01

Chapter 54: POSIX Shared Memory

This chapter describes POSIX shared memory, and compares it with the other shared memory APIs available on Linux: System V shared memory (Chapter 48) and shared file mappings (Chapter 49).

54 POSIX Shared Memory
54.1 Overview
54.2 Creating Shared Memory Objects: shm_open()
54.3 Using Shared Memory Objects
54.4 Removing Shared Memory Objects: shm_unlink()
54.5 Comparisons between Shared Memory APIs
54.6 Summary
54.7 Exercises

2009-10-30

Chapter 53: POSIX Semaphores

This chapter describes POSIX semaphores, which allow processes and threads to synchronize access to shared resources. In Chapter 47, we described System V semaphores, and we'll assume that the reader is familiar with the general semaphore concepts and rationale for using semaphores that were presented at the start of that chapter. During the course of this chapter, we'll make comparisons between POSIX semaphores and System V semaphores to clarify the ways in which these two semaphore APIs are the same and the ways in which they differ.

53 POSIX Semaphores
53.1 Overview
53.2 Named Semaphores
        53.2.1 Opening a Named Semaphore
        53.2.2 Closing a Semaphore
        53.2.3 Removing a Named Semaphore
53.3 Semaphore Operations
        53.3.1 Waiting on a Semaphore
        53.3.2 Posting a Semaphore
        53.3.3 Retrieving the Current Value of a Semaphore
53.4 Unnamed Semaphores
        53.4.1 Initializing an Unnamed Semaphore
        53.4.2 Destroying an Unnamed Semaphore
53.5 Comparisons with Other Synchronization Techniques
53.6 Semaphore Limits
53.7 Summary
53.8 Exercises

2009-10-29

Chapter 52: POSIX Message Queues

This chapter describes POSIX message queues, which allow processes to exchange data in the form of messages. POSIX message queues are in several respects similar to System V message queues (Chapter 46), but there are also some notable differences, which we describe in this chapter.

52 POSIX Message Queues
52.1 Overview
52.2 Opening, Closing, and Unlinking a Message Queue
52.3 Relationship between Descriptors and Message Queues
52.4 Message Queue Attributes
52.5 Exchanging Messages
        52.5.1 Sending Messages: mq_send()
        52.5.2 Receiving Messages: mq_receive()
        52.5.3 Sending and Receiving Messages with a Timeout
52.6 Message Notification
        52.6.1 Receiving Notification Using a Signal (SIGEV_SIGNAL)
        52.6.2 Receiving Notification Using a Thread (SIGEV_THREAD)
52.7 Linux-specific Features
52.8 Message Queue Limits
52.9 Comparison of POSIX and System V Message Queues
52.10 Summary
52.11 Exercises

2009-10-28

Chapter 51: Introduction to POSIX IPC

The POSIX.1b realtime extensions defined a set of IPC mechanisms that are analogous to the System V IPC mechanisms that we described in Chapters 45 to 48. (One of the POSIX.1b developers' aims was to devise a set of IPC mechanisms that did not suffer the deficiencies of the System V IPC facilities.) These IPC mechanisms are collectively referred to as POSIX IPC. The three POSIX IPC mechanisms are message queues, semaphores, and shared memory. This chapter provides an overview of the POSIX IPC facilities, focusing on the common features of the three POSIX IPC mechanisms.

51 Introduction to POSIX IPC
51.1 API Overview
51.2 Comparison of System V IPC and POSIX IPC
51.3 Summary

2009-10-26

Chapters 56 to 61 are in copyedit

Chapter 55 cam back from copyedit. Chapters 56 to 61 have now gone off to the copyeditor.

2009-10-22

Chapter 55 is in copyedit

Chapters 51 to 54 came back from copyedit. Chapter 55 is with the copyeditor.

2009-10-21

Chapter 50: Virtual Memory Operations

This chapter looks at various system calls that perform operations on a process’s virtual address space:
  • The mprotect() system call changes the protection on a region of virtual memory.
  • The mlock() and mlockall() system calls lock a region of virtual memory into physical memory, thus preventing it from being swapped out.
  • The mincore() system call allows a process to determine whether the pages in a region of virtual memory are resident in physical memory.
  • The madvise() system call allows a process to advise the kernel about its future patterns of usage of a virtual memory region.
50 Virtual Memory Operations
50.1 Changing Memory Protection: mprotect()
50.2 Memory Locking: mlock() and mlockall()
50.3 Determining Memory Residence: mincore()
50.4 Advising Future Memory Usage Patterns: madvise()
50.5 Summary
50.6 Exercises

2009-10-19

Chapter 49: Memory Mappings

This chapter discusses the use of the mmap() system call to create memory mappings. Among other topics, we consider four general uses of memory mappings:
  • allocating process-private memory (private anonymous mappings);
  • initializing the contents of the text and initialized data segments of a process (private file mappings);
  • performing IPC by sharing sharing memory between processes related via fork() (shared anonymous mappings); and
  • performing memory-mapped I/O, optionally combined with memory sharing (and thus IPC) between unrelated processes (shared file mappings).
49 Memory Mappings
49.1 Overview
49.2 Creating a Mapping: mmap()
49.3 Unmapping a Mapped Region: munmap()
49.4 File Mappings
      49.4.1 Private (MAP_PRIVATE) File Mappings
      49.4.2 Shared (MAP_SHARED) File Mappings
      49.4.3 Boundary Cases
      49.4.4 Memory Protection and File Access Mode Interactions
49.5 Synchronizing a Mapped Region: msync()
49.6 Additional mmap() Flags
49.7 Anonymous Mappings
49.8 Remapping a Mapped Region: mremap()
49.9 The MAP_NORESERVE Flag and Swap Space Overcommitting
49.10 The MAP_FIXED Flag
49.11 Nonlinear Mappings: remap_file_pages()
49.12 Summary
49.13 Exercises

2009-10-16

Chapter 48: System V Shared Memory

This chapter describes System V shared memory. Shared memory allows two or more processes to share the same region (usually referred to as a segment) of physical memory. Since a shared memory segment becomes part of a process's user-space memory, no kernel intervention is required for IPC. All that is required is that one process copies data into the shared memory; that data is immediately available to all other processes sharing the same segment. This provides fast IPC by comparison with techniques such as pipes or message queues, where the sending process copies data from a buffer in user space into kernel memory and the receiving process copies in the reverse direction. (Each process also incurs the overhead of a system call to perform the copy operation.)

On the other hand, the fact that IPC using shared memory is not mediated by the kernel means that, typically, some method of synchronization is required so that processes don’t simultaneously access the shared memory (e.g., two processes performing simultaneous updates, or one process fetching data from the shared memory while another process is in the middle of updating it). System V semaphores are a natural method for synchronizing access to System V shared memory segments

48 System V Shared Memory
48.1 Overview
48.2 Creating or Opening a Shared Memory Segment: shmget()
48.3 Using Shared Memory: shmat() and shmdt()
48.4 Example: Transferring Data Via Shared Memory
48.5 Location of Shared Memory Segments in Virtual Memory
48.6 Storing Pointers in Shared Memory
48.7 Shared Memory Control Operations: shmctl()
48.8 Shared Memory Associated Data Structure
48.9 Shared Memory Limits
48.10 Summary
48.11 Exercises

2009-10-14

Chapter 47: System V Semaphores

This chapter describes System V semaphores. Unlike the IPC mechanisms described in previous chapters, System V semaphores are not used to transfer data between processes. Instead, they allow processes to synchronize their actions. One common use of a semaphore is to synchronize access to a block of shared memory, in order to prevent one process from accessing the shared memory at the same time as another process is updating it.

47 System V Semaphores
47.1 Overview
47.2 Creating or Opening a Semaphore Set: semget()
47.3 Semaphore Control Operations: semctl()
47.4 Semaphore Associated Data Structure
47.5 Semaphore Initialization
47.6 Semaphore Operations: semop()
47.7 Handling of Multiple Blocked Semaphore Operations
47.8 Semaphore Undo Values
47.9 Implementing a Binary Semaphores Protocol
47.10 Semaphore Limits
47.11 Disadvantages of System V Semaphores
47.12 Summary
47.13 Exercises

2009-10-13

Chapters 51 to 54 are in copyedit

Chapters 49 and 50 came back from copyedit. Chapters 51 to 54 have gone to the copyeditor.