ebook img

PThreads Programming: A POSIX Standard for Better Multiprocessing PDF

307 Pages·1996·3.27 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview PThreads Programming: A POSIX Standard for Better Multiprocessing

Pthreads Programming Dick Buttlar Jacqueline Farrell Bradford Nichols Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo Special Upgrade Offer If you purchased this ebook directly from oreilly.com, you have the following benefits: DRM-free ebooks—use your ebooks across devices without restrictions or limitations Multiple formats—use on your laptop, tablet, or phone Lifetime access, with free updates Dropbox syncing—your files, anywhere If you purchased this ebook from another retailer, you can upgrade your ebook to take advantage of all these benefits for just $4.99. Click here to access your ebook upgrade. Please note that upgrade offers are not available from sample content. A Note Regarding Supplemental Files Supplemental files and examples for this book can be found at http://examples.oreilly.com/9781565921153/. Please use a standard desktop web browser to access these files, as they may not be accessible from all ereader devices. All code files or examples referenced in the book will be available online. For physical books that ship with an accompanying disc, whenever possible, we’ve posted all CD/DVD content. Note that while we provide as much of the media content as we are able via free download, we are sometimes limited by licensing restrictions. Please direct any questions or concerns to [email protected]. Examples Example 1-1 Example 1-2 Example 1-3 Example 1-4 Example 1-5 Example 1-6 Example 1-7 Example 1-8 Example 1-9 Example 1-10 Example 2-1 Example 2-2 Example 2-3 Example 2-4 Example 2-5 Example 2-6 Example 2-7 Example 2-8 Example 2-9 Example 2-10 Example 2-11 Example 3-1 Example 3-2 Example 3-3 Example 3-4 Example 3-5 Example 3-6 Example 3-7 Example 3-8 Example 3-9 Example 3-10 Example 3-11 Example 3-12 Example 3-13 Example 3-14 Example 3-15 Example 3-16 Example 3-17 Example 3-18 Example 3-19 Example 3-20 Example 3-21 Example 3-22 Example 3-23 Example 3-24 Example 3-25 Example 3-26 Example 3-27 Example 3-28 Example 4-1 Example 4-2 Example 4-3 Example 4-4 Example 4-5 Example 4-6 Example 4-7 Example 4-8 Example 4-9 Example 4-10 Example 4-11 Example 4-12 Example 4-13 Example 4-14 Example 4-15 Example 4-16 Example 4-17 Example 4-18 Example 4-19 Example 4-20 Example 4-21 Example 4-22 Example 4-23 Example 4-24 Example 4-25 Example 4-26 Example 4-27 Example 4-28 Example 4-29 Example 5-1 Example 5-2 Example 5-3 Example 5-4 Example 5-5 Example 5-6 Example 5-7 Example 5-8 Example 6-1 Example 6-2 Example 6-3 Example 6-4 Example 6-5 Example 6-6 Example 6-7 Example 6-8 Example 6-9 Example A-1 Example A-2 Example A-3 Preface It’s been quite a while since the people from whom we get our project assignments accepted the excuse “Gimme a break! I can only do one thing at a time!” It used to be such a good excuse, too, when things moved just a bit slower and a good day was measured in written lines of code. In fact, today we often do many things at a time. We finish off breakfast on the way into work; we scan the Internet for sports scores and stock prices while our application is building; we’d even read the morning paper in the shower if the right technology were in place! Being busy with multiple things is nothing new, though. (We’ll just give it a new computer-age name, like multitasking, because computers are happiest when we avoid describing them in anthropomorphic terms.) It’s the way of the natural world—we wouldn’t be able to write this book if all the body parts needed to keep our fingers moving and our brains engaged didn’t work together at the same time. It’s the way of the mechanical world—we wouldn’t have been able to get to this lovely prefabricated office building to do our work if the various, clanking parts of our automobiles didn’t work together (most of the time). It’s the way of the social and business world—three authoring tasks went into the making of this book, and the number of tasks, all happening at once, grew exponentially as it went into its review cycles and entered production. Computer hardware and operating systems have been capable of multitasking for years. CPUs using a RISC (reduced instruction set computing) microprocessor break down the processing of individual machine instructions into a number of separate tasks. By pipelining each instruction through each task, a RISC machine can have many instructions in progress at the same time. The end result is the heralded speed and throughput of RISC processors. Time-sharing operating systems have been allowing users nearly simultaneous access to the processor for longer than we can remember. Their ability to schedule different tasks (typically called processes) really pays off when separate tasks can actually execute simultaneously on separate CPUs in a multiprocessor system. Although real user applications can be adapted to take advantage of a computer’s ability to do more than one thing at once, a lot of operating system code must execute to make it possible. With the advent of threads we’ve reached an ideal state—the ability to perform multiple tasks simultaneously with as little operating system overhead as possible. Although threaded programming styles have been around for some time now, it’s only recently that they’ve been adopted by the mainstream of UNIX it’s only recently that they’ve been adopted by the mainstream of UNIX programmers (not to mention those erstwhile laborers in the vineyards of Windows NT and other operating systems). Software sages swear at the lunchroom table that transaction processing monitors and real-time embedded systems have been using thread-like abstractions for more than twenty years. In the mid-to-late eighties, the general operating system community embarked on several research efforts focused on threaded programming designs, as typified by the work of Tom Doeppner at Brown University and the Mach OS developers at Carnegie-Mellon. With the dawn of the nineties, threads became established in the various UNIX operating systems, such as USL’s System V Release 4, Sun Solaris, and the Open Software Foundation’s OSF/1. The clash of platform- specific threads programming libraries advanced the need of some portable, platform-independent threads interface. The IEEE has just this year met this need with the acceptance of the IEEE Standard for Information Technology Portable Operating System Interface (POSIX) Part 1: System Application Programming Interface (API) Amendment 2: Threads Extension [C Language] —the Pthreads standard, for short. This book is about Pthreads—a lightweight, easy-to-use, and portable mechanism for speeding up applications. Organization We’ll start off Chapter 1, by introducing you to multithreading as a way of performing the many tasks of a program with greater efficiency and speed than would be possible in a serial or multiprocess design. We’ll then examine the pitfalls of serial and multiprocess programming, and discuss the concept of potential parallelism, the cornerstone of any decision to write a multitasking program. We’ll introduce you to your first Pthreads call—pthread_create—and look at those structures by which a thread is uniquely identified. We’ll briefly examine the ways in which multiple threads in the same process exchange data, and we’ll highlight some synchronization issues. We’ll continue our discussion of planning and structuring a multithreaded program in Chapter 2. Here, we’ll look at the types of applications that can benefit most from multithreading. We’ll present the three classic methods for distributing work among threads—the boss/worker model, the peer model, and the pipeline model. We’ll also compare two strategies for creating threads— creation on demand versus thread pools. After a brief discussion of thread data- buffering techniques, we’ll introduce the ATM server application example that

Description:
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
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.