System-level programs
Unix shells, linkers, dynamic memory allocators
Background Required
C knowledge is required
Working knowledge of Unix/Linux is very useful
Classroom Etiquette
Come on time to both class and labs
Talking, cell phones, etc. will not be tolerated
Textbooks
Required
Randal Bryant and David OHallaron,
Computer Systems: A Programmers Perspective,
Pearson, 2003
This book really matters for the course!
How to solve labs
Practice problems typical of exam problems
Recommended
Keith Haviland, Dina Gray and Ben Salama
UNIX System Programming, Addison-Wesley, 1998
Brian Kernighan and Dennis Ritchie,
The C Programming Language, Second Edition,
Prentice Hall, 1988
Course Coverage
Machine Level Representation of Programs (Ch. 3)
Linking (Ch. 7)
Exceptional Control Flow (Ch. 8)
Virtual Memory (Ch. 10)
System-Level I/O (Ch. 11)
Network Programming (Ch. 12, Ch. 13)
Grade breakdown
Source program
Created by an editor and saved as a text file
Text file
Text files
Files that consist exclusively of ASCII characters
Binary files
Files other than text files
< s
t
d
i
o
.
60 115 116 100 105 111 46
m
a i
n
( ) \n {
109 97 105 110 40 41 10 123
10
Information is Bits+Context
In different contexts, the same sequence of bytes
might represent
Programs stored in memory
Data stored in memory
Context is very important in interpreting bits!
11
Compiler driver
To translate our hello program into an executable
object file, we must invoke a COMPILER DRIVER.
We might press the Build" button in Visual Studio
or invoke the Gnu C Compiler (gcc) from the
command line:
bash% gcc -o hello hello.c
12
Results of Compiling
Assembly language
13
Results of Assembling
File: hello.o
Listing omitted
Not human-readable
Machine language
Object file
Still missing definition of printf()
14
File: hello.o
Listing omitted
Not human-readable
Machine language
The linker:
Observes that
Code in hello.o calls printf()
Code in hello.o does not define printf()
Fetches machine language definition of printf() from
standard C library (/usr/lib/libc.a)
Merges those definitions with hello.o to create
15
Results of Linking
File: hello
Listing omitted
Not human-readable
Machine language
16
Program execution
A shell is an application program that runs
commands on behalf of user
We can run our hello program as follows:
% ./hello
Hello, World
%
The shell grabs the executable code for hello from
disk, loads it into memory, starts it running, and
waits for it to finish
17
Software
Operating system
Processor
Main memory
I/O devices
Hardware
18
OS Abstractions
A process is a program in execution. Every time you
run a program, a process is created. It runs until
completion. Then it is destroyed.
In reality, the main memory is shared between
many processes. But the OS uses virtual memory to
give each process the illusion of a large memory
address space of its own.
In reality, every device is different. But the OS
provides the file abstraction so that processes
access all devices in the same way.
19
Connecting processes
20
Virtual memory
21
Virtual memory
Kernel virtual memory
Here is how
virtual
memory
is laid out in
Linux when
0x40000000
hello world
is
running
User stack
(created at runtime)
Memory
invisible to
user code
printf function
Run-time heap
(created by malloc)
Read/write data
Read-only code and data
0x08048000
0
22
Word size
Every system has a word size (the normal size of a
pointer).
The word size determines the size of the virtual
address space.
Most PCs have a 32-bit word size.
23
24
Byte Ordering
There are two ways to store multi-byte objects in
memory
Intel is a little endian architecture
25
Big Endian
0x101
0x102
0x103
01
23
45
67
26
Little Endian
0x101
0x102
0x103
67
45
23
01
27
28
Endianness
add %eax,
29
Pointers
30
Offset
Offset
Offset
Offset
0:
1:
2:
3:
39
30
0
0
Decimal: 12345
Binary:
Hex:
31
Casting
32
show_bytes routine P. 37
typedef
typedef unsigned
unsigned char
char *byte_pointer;
*byte_pointer;
void
void show_bytes(byte_pointer
show_bytes(byte_pointer start,
start, int
int len){
len){
int
int i;
i;
for
for (i
(i == 0;
0; ii << len;
len; i++)
i++)
printf(
printf( %.2x",
%.2x", start[i]);
start[i]);
printf("\n");
printf("\n");
}}
33
Problem
P. 40
34
Problem P. 100
35
Summary
What have we learned this week?
Next week
Read Chapter 1
36