Operating System: Three Easy Pieces --- Locks (Note)

From the introduction to concurrency, we saw one of the fundamental problems in concurrent

programming: we would like to execute a series of instructions atomically, but due to the presence

of interrupts on a single processor (multiple threads executing on multiple processors concurrently),

we could‘t. In this chapter, we thus attack this problem directly, with the introduction of something

referred to as a lock. Programmers annotate source code with locks, putting them around critical

sections, and thus ensure that any such critical section executes as if it were a single atomic instruction.

Locks: The basic idea

As an example, assume our critical section looks like this, the canonical update of a shared variable:

balance = balance + 1;

of course, other critical sections are possible, such as adding an element to a linked list or other more

complex updates to shared structures, but we will just keep to this example for now. To use a lock, we

add some code around the critical section like this:

lock_t mutex;

.....

lock(&mutex);

balance = balance + 1;

unlock(&mutex);

A lock is just a variable, and thus to use one, you must declare a lock variable of some kind (such as mutex

above). This lock variable (or just "lock" for short) holds the state of the lock at any instant in time. It is

either available (or unlocked or free) and thus no thread holds the lock, or acquired (or locked or held), and

thus exactly one thread holds the lock and presumably is in a critical section. We could store other information

in the data type as well, such as which thread holds the lock, or a queue for ordering lock acquisition, but

information like that is hidden from the user of lock.

The semantics of the lock() and unlock() routines are simple. Calling the routine lock() tries to acquire the lock;

if no other thread holds the lock (i.e. it is free), the thread will acquire the lock and enter the critical section; this

thread is sometimes said to be the owner of the lock. If another thread then calls lock() on that same lock variable

(mutex in this example), it will not return while the lock is held by another thread; in this way, other threads are

prevented from entering the critical section while the first thread that holds the lock is in there.

Once the owner of the lock calls unlock(), the lock is now available (free) again. If no other threads are waiting for

the lock (i.e. no other thread has called lock() and is stuck therein), the state of the lock is simply changed to free.

If there are waiting threads (stuck in lock()), one of them will (eventually) notice (or be informed of) this change

of the lock‘s state, acquire the lock, and enter the critical section.

Locks provide some minimal amount of control over scheduling to programmers. In general, we view threads as

entities created by the programmer but scheduled by the OS, in any fashion that the OS chooses. Locks yield some

of that control back to the programmer; by putting a lock around a section of code, the programmer can guarantee

the no more than a single thread can ever be active within that code. Thus locks help transform the chaos that is

traditional OS scheduling into a more controlled activity.

时间: 2025-01-12 05:21:44

Operating System: Three Easy Pieces --- Locks (Note)的相关文章

Operating System: Three Easy Pieces --- Locks: Pthread Locks (Note)

The name that the POSIX library uses for a lock is mutex, as it is used to provide mutual exclusion between threads, i.e., if one thread is in the critical sections, it excludes the others from entering until it has completed the section. Thus, when

Operating System: Three Easy Pieces --- Process (Note)

1. How can the operating system provide the illusion of a nearly-endless supply of said CPUs? The OS creates this illusion by virtualizing the CPU. The basic technique, known as the time- sharing the CPU, allows users to run as many concurrent proces

Operating System: Three Easy Pieces --- Locks: Test and Set (Note)

Because disabling interrupts does not work on multiple processors, system designers started to invent hardware support for locking. The earliest multiprocessor systems, such as the Burroughts B5000 in the  early 1960's, had such support; today all sy

Operating System: Three Easy Pieces --- API (Note)

Aside: RTFM --- Read The Man Pages Many times in this book, when referring to a particular system call or library call, we will tell you to read the mannual pages, or man pages for short. Man pages are the original form of documantation that exist on

Operating System: Three Easy Pieces --- LDE (Note)

ASIDE: Why System Calls Look Like Procedure Calls? You may wonder why a call to a system call, such as open() or read() looks exactly like a typical procedure call in C; that is, if it looks just like a procedure call, how does the system know it is

Operating System: Three Easy Pieces --- Mechanism: Limited Direct Execution (Note)

In order to virtualize the CPU, the operating system needs to somehow share the physical CPU among many jobs  running seemingly at the same time. The basic idea is simple: run one process for a little while, then run another, and so forth. By time sh

Operating System: Three Easy Pieces --- Limited Directed Execution (Note)

In order to virtualize the CPU, the operating system needs to somehow share the physical CPU among many jobs running seemingly at the same time. The basic idea is simple: run one process for a little while, then run another one, and so forth. By time

Operating System: Three Easy Pieces --- Pthread Locks (Note)

The name that the POSIX library uses for a lock is a mutex, as it is used to provide mutual exclusion between threads, i.e., if one thread is in the critical section, it excludes the others from entering until it has completed the section. Thus, when

Operating System: Three Easy Pieces --- Thread API (Note)

This chapter briefly covers the main properties of the thread API. Each part will be explained further in the subsequent chapters, as we know how to use the API. More details can be found in various books and online sources. We should note that the s