Get (eBook PDF) Computer Systems: A Programmer's Perspective 3nd Edition free all chapters
Get (eBook PDF) Computer Systems: A Programmer's Perspective 3nd Edition free all chapters
https://ebookluna.com/product/ebook-pdf-health-informatics-a-systems-
perspective-second-edition/
ebookluna.com
https://ebookluna.com/product/ebook-pdf-health-informatics-a-systems-
perspective-first-edition/
ebookluna.com
https://ebookluna.com/product/ebook-pdf-comparative-health-systems-a-
global-perspective-2nd-edition/
ebookluna.com
https://ebookluna.com/product/cmos-vlsi-design-a-circuits-and-systems-
perspective-4th-edition/
ebookluna.com
(eBook PDF) Computer Systems 5th Edition
https://ebookluna.com/product/ebook-pdf-computer-systems-5th-edition/
ebookluna.com
https://ebookluna.com/product/computer-systems-5th-edition-ebook-pdf/
ebookluna.com
https://ebookluna.com/product/ebook-pdf-health-care-operations-
management-a-systems-perspective-2nd-edition/
ebookluna.com
https://ebookluna.com/download/a-new-ecology-systems-perspective-
ebook-pdf/
ebookluna.com
ebookluna.com
Global Global
edition edition
edition
Global Computer Systems
A Programmer’s Perspective
For these Global Editions, the editorial team at Pearson has
collaborated with educators across the world to address a
wide range of subjects and requirements, equipping students
with the best possible learning tools. This Global Edition
preserves the cutting-edge approach and pedagogy of the
original, but also features alterations, customization, and
adaptation from the North American version.
edition
Third
Computer Systems
Bryant • O’Hallaron
This is a special edition of an established A Programmer’s Perspective
title widely used by colleges and universities
throughout the world. Pearson published this Third edition
exclusive edition for the benefit of students
outside the United States and Canada. If you
purchased this book within the United States
Randal E. Bryant • David R. O’Hallaron
or Canada, you should be aware that it has
been imported without the approval of the
Publisher or Author.
Preface 19
About the Authors 35
1
A Tour of Computer Systems 37
1.1 Information Is Bits + Context 39
1.2 Programs Are Translated by Other Programs into Different Forms 40
1.3 It Pays to Understand How Compilation Systems Work 42
1.4 Processors Read and Interpret Instructions Stored in Memory 43
1.4.1 Hardware Organization of a System 44
1.4.2 Running the hello Program 46
1.5 Caches Matter 47
1.6 Storage Devices Form a Hierarchy 50
1.7 The Operating System Manages the Hardware 50
1.7.1 Processes 51
1.7.2 Threads 53
1.7.3 Virtual Memory 54
1.7.4 Files 55
1.8 Systems Communicate with Other Systems Using Networks 55
1.9 Important Themes 58
1.9.1 Amdahl’s Law 58
1.9.2 Concurrency and Parallelism 60
1.9.3 The Importance of Abstractions in Computer Systems 62
1.10 Summary 63
Bibliographic Notes 64
Solutions to Practice Problems 64
2
Representing and Manipulating Information 67
2.1 Information Storage 70
2.1.1 Hexadecimal Notation 72
2.1.2 Data Sizes 75
7
8 Contents
3
Machine-Level Representation of Programs 199
3.1 A Historical Perspective 202
Contents 9
4
Processor Architecture 387
4.1 The Y86-64 Instruction Set Architecture 391
4.1.1 Programmer-Visible State 391
4.1.2 Y86-64 Instructions 392
4.1.3 Instruction Encoding 394
4.1.4 Y86-64 Exceptions 399
4.1.5 Y86-64 Programs 400
4.1.6 Some Y86-64 Instruction Details 406
4.2 Logic Design and the Hardware Control Language HCL 408
4.2.1 Logic Gates 409
4.2.2 Combinational Circuits and HCL Boolean Expressions 410
4.2.3 Word-Level Combinational Circuits and HCL
Integer Expressions 412
4.2.4 Set Membership 416
4.2.5 Memory and Clocking 417
4.3 Sequential Y86-64 Implementations 420
4.3.1 Organizing Processing into Stages 420
Contents 11
5
Optimizing Program Performance 531
5.1 Capabilities and Limitations of Optimizing Compilers 534
5.2 Expressing Program Performance 538
5.3 Program Example 540
5.4 Eliminating Loop Inefficiencies 544
5.5 Reducing Procedure Calls 548
5.6 Eliminating Unneeded Memory References 550
5.7 Understanding Modern Processors 553
5.7.1 Overall Operation 554
5.7.2 Functional Unit Performance 559
5.7.3 An Abstract Model of Processor Operation 561
5.8 Loop Unrolling 567
5.9 Enhancing Parallelism 572
5.9.1 Multiple Accumulators 572
5.9.2 Reassociation Transformation 577
12 Contents
6
The Memory Hierarchy 615
6.1 Storage Technologies 617
6.1.1 Random Access Memory 617
6.1.2 Disk Storage 625
6.1.3 Solid State Disks 636
6.1.4 Storage Technology Trends 638
6.2 Locality 640
6.2.1 Locality of References to Program Data 642
6.2.2 Locality of Instruction Fetches 643
6.2.3 Summary of Locality 644
6.3 The Memory Hierarchy 645
6.3.1 Caching in the Memory Hierarchy 646
6.3.2 Summary of Memory Hierarchy Concepts 650
6.4 Cache Memories 650
6.4.1 Generic Cache Memory Organization 651
6.4.2 Direct-Mapped Caches 653
6.4.3 Set Associative Caches 660
6.4.4 Fully Associative Caches 662
6.4.5 Issues with Writes 666
6.4.6 Anatomy of a Real Cache Hierarchy 667
6.4.7 Performance Impact of Cache Parameters 667
6.5 Writing Cache-Friendly Code 669
6.6 Putting It Together: The Impact of Caches on Program Performance 675
Contents 13
7
Linking 705
7.1 Compiler Drivers 707
7.2 Static Linking 708
7.3 Object Files 709
7.4 Relocatable Object Files 710
7.5 Symbols and Symbol Tables 711
7.6 Symbol Resolution 715
7.6.1 How Linkers Resolve Duplicate Symbol Names 716
7.6.2 Linking with Static Libraries 720
7.6.3 How Linkers Use Static Libraries to Resolve References 724
7.7 Relocation 725
7.7.1 Relocation Entries 726
7.7.2 Relocating Symbol References 727
7.8 Executable Object Files 731
7.9 Loading Executable Object Files 733
7.10 Dynamic Linking with Shared Libraries 734
7.11 Loading and Linking Shared Libraries from Applications 737
7.12 Position-Independent Code (PIC) 740
7.13 Library Interpositioning 743
7.13.1 Compile-Time Interpositioning 744
7.13.2 Link-Time Interpositioning 744
7.13.3 Run-Time Interpositioning 746
7.14 Tools for Manipulating Object Files 749
7.15 Summary 749
Bibliographic Notes 750
Homework Problems 750
Solutions to Practice Problems 753
14 Contents
8
Exceptional Control Flow 757
8.1 Exceptions 759
8.1.1 Exception Handling 760
8.1.2 Classes of Exceptions 762
8.1.3 Exceptions in Linux/x86-64 Systems 765
8.2 Processes 768
8.2.1 Logical Control Flow 768
8.2.2 Concurrent Flows 769
8.2.3 Private Address Space 770
8.2.4 User and Kernel Modes 770
8.2.5 Context Switches 772
8.3 System Call Error Handling 773
8.4 Process Control 774
8.4.1 Obtaining Process IDs 775
8.4.2 Creating and Terminating Processes 775
8.4.3 Reaping Child Processes 779
8.4.4 Putting Processes to Sleep 785
8.4.5 Loading and Running Programs 786
8.4.6 Using fork and execve to Run Programs 789
8.5 Signals 792
8.5.1 Signal Terminology 794
8.5.2 Sending Signals 795
8.5.3 Receiving Signals 798
8.5.4 Blocking and Unblocking Signals 800
8.5.5 Writing Signal Handlers 802
8.5.6 Synchronizing Flows to Avoid Nasty Concurrency Bugs 812
8.5.7 Explicitly Waiting for Signals 814
8.6 Nonlocal Jumps 817
8.7 Tools for Manipulating Processes 822
8.8 Summary 823
Bibliographic Notes 823
Homework Problems 824
Solutions to Practice Problems 831
9
Virtual Memory 837
9.1 Physical and Virtual Addressing 839
9.2 Address Spaces 840
Contents 15
10
System-Level I/O 925
10.1 Unix I/O 926
10.2 Files 927
10.3 Opening and Closing Files 929
10.4 Reading and Writing Files 931
10.5 Robust Reading and Writing with the Rio Package 933
10.5.1 Rio Unbuffered Input and Output Functions 933
10.5.2 Rio Buffered Input Functions 934
10.6 Reading File Metadata 939
10.7 Reading Directory Contents 941
10.8 Sharing Files 942
10.9 I/O Redirection 945
10.10 Standard I/O 947
10.11 Putting It Together: Which I/O Functions Should I Use? 947
10.12 Summary 949
Bibliographic Notes 950
Homework Problems 950
Solutions to Practice Problems 951
Contents 17
11
Network Programming 953
11.1 The Client-Server Programming Model 954
11.2 Networks 955
11.3 The Global IP Internet 960
11.3.1 IP Addresses 961
11.3.2 Internet Domain Names 963
11.3.3 Internet Connections 965
11.4 The Sockets Interface 968
11.4.1 Socket Address Structures 969
11.4.2 The socket Function 970
11.4.3 The connect Function 970
11.4.4 The bind Function 971
11.4.5 The listen Function 971
11.4.6 The accept Function 972
11.4.7 Host and Service Conversion 973
11.4.8 Helper Functions for the Sockets Interface 978
11.4.9 Example Echo Client and Server 980
11.5 Web Servers 984
11.5.1 Web Basics 984
11.5.2 Web Content 985
11.5.3 HTTP Transactions 986
11.5.4 Serving Dynamic Content 989
11.6 Putting It Together: The Tiny Web Server 992
11.7 Summary 1000
Bibliographic Notes 1001
Homework Problems 1001
Solutions to Practice Problems 1002
12
Concurrent Programming 1007
12.1 Concurrent Programming with Processes 1009
12.1.1 A Concurrent Server Based on Processes 1010
12.1.2 Pros and Cons of Processes 1011
12.2 Concurrent Programming with I/O Multiplexing 1013
12.2.1 A Concurrent Event-Driven Server Based on I/O
Multiplexing 1016
12.2.2 Pros and Cons of I/O Multiplexing 1021
12.3 Concurrent Programming with Threads 1021
12.3.1 Thread Execution Model 1022
18 Contents
A
Error Handling 1077
A.1 Error Handling in Unix Systems 1078
A.2 Error-Handling Wrappers 1079
References 1083
Index 1089
Preface
This book (known as CS:APP) is for computer scientists, computer engineers, and
others who want to be able to write better programs by learning what is going on
“under the hood” of a computer system.
Our aim is to explain the enduring concepts underlying all computer systems,
and to show you the concrete ways that these ideas affect the correctness, perfor-
mance, and utility of your application programs. Many systems books are written
from a builder’s perspective, describing how to implement the hardware or the sys-
tems software, including the operating system, compiler, and network interface.
This book is written from a programmer’s perspective, describing how application
programmers can use their knowledge of a system to write better programs. Of
course, learning what a system is supposed to do provides a good first step in learn-
ing how to build one, so this book also serves as a valuable introduction to those
who go on to implement systems hardware and software. Most systems books also
tend to focus on just one aspect of the system, for example, the hardware archi-
tecture, the operating system, the compiler, or the network. This book spans all
of these aspects, with the unifying theme of a programmer’s perspective.
If you study and learn the concepts in this book, you will be on your way to
becoming the rare power programmer who knows how things work and how to
fix them when they break. You will be able to write programs that make better
use of the capabilities provided by the operating system and systems software,
that operate correctly across a wide range of operating conditions and run-time
parameters, that run faster, and that avoid the flaws that make programs vulner-
able to cyberattack. You will be prepared to delve deeper into advanced topics
such as compilers, computer architecture, operating systems, embedded systems,
networking, and cybersecurity.
19
20 Preface
code/intro/hello.c
1 #include <stdio.h>
2
3 int main()
4 {
5 printf("hello, world\n");
6 return 0;
7 }
code/intro/hello.c
Each code example in the text was formatted directly, without any manual
intervention, from a C program compiled with gcc and tested on a Linux system.
Of course, your system may have a different version of gcc, or a different compiler
altogether, so your compiler might generate different machine code; but the
overall behavior should be the same. All of the source code is available from the
CS:APP Web page (“CS:APP” being our shorthand for the book’s title) at csapp
.cs.cmu.edu. In the text, the filenames of the source programs are documented
in horizontal bars that surround the formatted code. For example, the program in
Figure 1 can be found in the file hello.c in directory code/intro/. We encourage
you to try running the example programs on your system as you encounter them.
To avoid having a book that is overwhelming, both in bulk and in content, we
have created a number of Web asides containing material that supplements the
main presentation of the book. These asides are referenced within the book with
a notation of the form chap:top, where chap is a short encoding of the chapter sub-
ject, and top is a short code for the topic that is covered. For example, Web Aside
data:bool contains supplementary material on Boolean algebra for the presenta-
tion on data representations in Chapter 2, while Web Aside arch:vlog contains
22 Preface
material describing processor designs using the Verilog hardware description lan-
guage, supplementing the presentation of processor design in Chapter 4. All of
these Web asides are available from the CS:APP Web page.
Book Overview
The CS:APP book consists of 12 chapters designed to capture the core ideas in
computer systems. Here is an overview.
Chapter 1: A Tour of Computer Systems. This chapter introduces the major ideas
and themes in computer systems by tracing the life cycle of a simple “hello,
world” program.
Chapter 2: Representing and Manipulating Information. We cover computer arith-
metic, emphasizing the properties of unsigned and two’s-complement num-
ber representations that affect programmers. We consider how numbers
are represented and therefore what range of values can be encoded for
a given word size. We consider the effect of casting between signed and
unsigned numbers. We cover the mathematical properties of arithmetic op-
erations. Novice programmers are often surprised to learn that the (two’s-
complement) sum or product of two positive numbers can be negative. On
the other hand, two’s-complement arithmetic satisfies many of the algebraic
properties of integer arithmetic, and hence a compiler can safely transform
multiplication by a constant into a sequence of shifts and adds. We use the
bit-level operations of C to demonstrate the principles and applications of
Boolean algebra. We cover the IEEE floating-point format in terms of how
it represents values and the mathematical properties of floating-point oper-
ations.
Having a solid understanding of computer arithmetic is critical to writ-
ing reliable programs. For example, programmers and compilers cannot re-
place the expression (x<y) with (x-y < 0), due to the possibility of overflow.
They cannot even replace it with the expression (-y < -x), due to the asym-
metric range of negative and positive numbers in the two’s-complement
representation. Arithmetic overflow is a common source of programming
errors and security vulnerabilities, yet few other books cover the properties
of computer arithmetic from a programmer’s perspective.
Chapter 3: Machine-Level Representation of Programs. We teach you how to read
the x86-64 machine code generated by a C compiler. We cover the ba-
sic instruction patterns generated for different control constructs, such as
conditionals, loops, and switch statements. We cover the implementation
of procedures, including stack allocation, register usage conventions, and
parameter passing. We cover the way different data structures such as struc-
tures, unions, and arrays are allocated and accessed. We cover the instruc-
tions that implement both integer and floating-point arithmetic. We also
use the machine-level view of programs as a way to understand common
code security vulnerabilities, such as buffer overflow, and steps that the pro-
Preface 23
grammer, the compiler, and the operating system can take to reduce these
threats. Learning the concepts in this chapter helps you become a better
programmer, because you will understand how programs are represented
on a machine. One certain benefit is that you will develop a thorough and
concrete understanding of pointers.
Chapter 6: The Memory Hierarchy. The memory system is one of the most visible
parts of a computer system to application programmers. To this point, you
have relied on a conceptual model of the memory system as a linear array
with uniform access times. In practice, a memory system is a hierarchy of
storage devices with different capacities, costs, and access times. We cover
the different types of RAM and ROM memories and the geometry and
organization of magnetic-disk and solid state drives. We describe how these
storage devices are arranged in a hierarchy. We show how this hierarchy is
made possible by locality of reference. We make these ideas concrete by
introducing a unique view of a memory system as a “memory mountain”
with ridges of temporal locality and slopes of spatial locality. Finally, we
show you how to improve the performance of application programs by
improving their temporal and spatial locality.
Chapter 7: Linking. This chapter covers both static and dynamic linking, including
the ideas of relocatable and executable object files, symbol resolution, re-
location, static libraries, shared object libraries, position-independent code,
and library interpositioning. Linking is not covered in most systems texts,
but we cover it for two reasons. First, some of the most confusing errors that
programmers can encounter are related to glitches during linking, especially
for large software packages. Second, the object files produced by linkers are
tied to concepts such as loading, virtual memory, and memory mapping.
Chapter 9: Virtual Memory. Our presentation of the virtual memory system seeks
to give some understanding of how it works and its characteristics. We want
you to know how it is that the different simultaneous processes can each use
an identical range of addresses, sharing some pages but having individual
copies of others. We also cover issues involved in managing and manip-
ulating virtual memory. In particular, we cover the operation of storage
allocators such as the standard-library malloc and free operations. Cov-
Other documents randomly have
different content
jollei se tule hänen avukseen. Ainoana lohdutuksena olivat enään
kaupunginlähetyksen sikarit. August oli sullonut koko kyökkikaapin
alaosan niitä täyteen.
Hän puhui niin yhtämittaa, ettei Konrad voinut muuta kuin antaa
kuuran kaatua päälleen. Mutta Bock heitti neitiin ärtyisän katseen, ja
seurasi häntä sillä vielä sittenkin, kun hän, istuutuneena vanhalle
paikalleen, koetti herättää rouva Simmerissä myötätuntoisuutta
tunteilleen. Julius olisi mielellään suonut hänet sinne missä pippurit
kasvavat, mutta hän oli hiljan lahjoittanut hänen seurakuntansa
turvattomille lapsille sievän summan ja sentähden täytyi häneen nyt
tyytyä sellaisena kuin hän oli.
Kymmenes luku.
Klaudina oli syypää siihen, että Josefa ensin oli kääntynyt hänen
puoleensa, sillä Klaudina oli tahallaan tahtonut nolata
superintendenttiä.
Josefa oli viety sänkyyn, jossa hän heti oli vaipunut levottomaan
uneen. Molemmat veljekset olivat yksin huoneessa, mutta eivät
sanallakaan koskettaneet siihen mitä vasta oli tapahtunut. Lääkäri
saapui ja puheli jonkun aikaa Juliuksen kanssa; Josefa tarvitsi hyvää
hoitoa. Hänen mentyään, sanoi Konrad äkkiä ja odottamatta:
— On. Ole hyvä ja lähetä Hedvig tänne, kun käyt siellä. Tai olkoon,
korjasi hän samassa. — Minun täytyy kuitenkin mennä sinne ja mitä
Josefaan tulee, niin täytän velvollisuuteni häntä kohtaan.
Jäätyään yksin, läksi Julius Josefan huoneeseen. Hän nukkui,
kasvot hohtivat ja hengitys oli hätäinen. Julius ei enään ollut kahden
hänen kanssaan, sillä hän oli lähettänyt noutamaan
sairaanhoitajatarta, joka hänen astuessaan huoneeseen nousi
paikaltaan ja vaatimattomasti väistyi syrjään. Pastori oli käskenyt,
ettei hän ilman hänen erityistä lupaansa saanut päästää sisään
ketään, paitsi lääkäriä. Hetkisen ajan Julius vaieten katseli nukkuvaa,
laski kätensä hänen otsalleen ja meni sitte pois. Tultuaan
työhuoneeseensa, otti hän kirjoituspöytänsä laatikosta rahaa ja soitti
Klaudinaa.
— Tehkää se, neiti Schaff, virkkoi hän sitten. — Minä kiitän teitä
kaikesta, vaikkette aina olekkaan pysynyt totuudessa. Ymmärrätte
varmaankin mitä tarkoitan… Johtakoon teitä Jumalan siunaus
teillänne, lisäsi hän saarnaäänellä. Eroaminen oli tässä totisesti
vaikeaa.
Klaudina oli tähän saakka ollut siinä luulossa, että pastori vain
tahtoi suututtaa häntä sekä että tuo tavallista pahempi tuuli menisi
menojaan niinkuin muutkin. Mutta nyt nousi veri hänen poskilleen ja
rinta alkoi hätäisesti kohota. Jos kerran pastori siis nyt todellakin oli
tosissaan, niin ei hänenkään tarvinnut häikäillä.
— Älkää lasketelko loruja, huudahti hän astuen pari askelta
eteenpäin. Hän oli silmänräpäyksessä niin muuttunut, että pastori
vallan säpsähti. — Totuutta, totuutta! jatkoi hän ylenkatseellisesti,
venytellen sanojaan. — Minäpä sanon teille jotakin, herra pastori…
Tässä talossa on minun aikanani vain kaksi kertaa puhuttu totuutta.
Ja sen teki veljenne, silloin kun hän sanoi koko kirkon kristillisyyttä
valheeksi ja tänään, kun hän oikein perinpohjaisesti luki
hovisaarnaajalle lain. Muuten täällä on ollut paljon mätää, niinkuin
kaikkialla, missä ihmiset ovat valmiit pikemmin näkemään tunkioita
lähimmäistensä kuin omien oviensa edessä… Tunnemmehan me
toisemme, me molemmat. Minun jälkeeni tulee toinen, siinä koko
muutos. Olkaa suora ja sanokaa, enkö ole oikeassa? Voi pyhä taivas,
ettenkö olisi! Mutta mitä minä sinua siitä soimaisin. Voithan sinä
tehdä rippisi kotona.
Julius naurahti.
— Sinä olet siis vielä jäänyt minulle, sanoi Konrad liikutettuna, veti
sisaren syliinsä ja suuteli hänen silmiään ja otsaansa. Hän ei
ensinkään huomannut, että hän samassa, aivan aiheettomasti, jätti
huoneen. Surullisesti hän jäi tuijottamaan eteensä lattiaan. Äkkiä
kuului hänen takanaan hiljainen kahina ja kuiskaus:
— Kuuleppas nyt, Niilo… Jos asian laita niin on, niin heitämme
hiiteen koko kirkon. Kyllä olemme jo tarpeeksi kiusaantuneet… Mutta
täytyyhän ihmisen elää.
Ovi lisahti lukkoon ja kissa, jonka häntä oli jäänyt väliin, päästi
surkean huudon.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookluna.com