Anda di halaman 1dari 36

Getting Started

IT215: Systems Software


Week 1, Jan. 3, 2014

Whats This Course All About?

Programming interface to services provided by OS


Concurrent programming: processes, signals, pipes, threads,
synchronization
Network programming: sockets and servers

System-level programs
Unix shells, linkers, dynamic memory allocators

Foundation for advanced courses in computer


systems
OS, Compilers, Networks,

Background Required

Computer architecture basics (IT 209)


CPU, interrupts, memory, etc.

C knowledge is required
Working knowledge of Unix/Linux is very useful

Can you edit, compile, and run a C program in Unix?

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

Exams (64%): weighted 16%, 16%, 32% (final)

Lab work (36%)


Lab attendance is mandatory
Quick practice problems that involve writing/running
programs
Programming assignments that may take one or two weeks
to complete
E.g. Write our own Unix shell
0 marks for copying or allowing someone to copy

The hello program


#include <stdio.h>
int main() {
{
printf(Hello, World\n);
}

Source program
Created by an editor and saved as a text file

Text file

Source program is stored in a file


As a sequence of bytes
Each byte has an integer value that corresponds to some
character

Text files
Files that consist exclusively of ASCII characters

Binary files
Files other than text files

ASCII representation of source


program
# i
n
c l
u
d
e <sp>
35 105 110 99 108 117 100 101 32
h
> \n \n i
n
t <sp>
104 62 10 10 105 110 116 32

< 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

\n <sp> <sp> <sp> <sp> p


r
i
n
t
f
( " h
e
10
32 32
32
32 112 114 105 110 116 102 40 34 104 101
l
l
o
, <sp> w
o
r
l
d
\ n
" ) ; \n }
108 108 111 44 32 119 111 114 108 100 92 110 34 41 59 10 125

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

File: hello.s (assembly language)


Listing omitted
Human-readable

Assembly language

Missing definition of printf()

13

Results of Assembling

File: hello.o
Listing omitted
Not human-readable

Machine language

Object file
Still missing definition of printf()

14

The Linkers View

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

Complete executable binary file

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

OS controls the hardware

When the shell loads hello, the program doesn't


really access the I/O devices directly.
Application programs

Software

Operating system
Processor

Main memory

I/O devices

Hardware

Why dont applications access I/O devices directly?

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

The shell command


% who | wc l

creates two processes to run who and the word


count program wc
| = pipe, output of who sent to the input of wc

20

Virtual memory

Every byte of memory is identified by a unique


number
its address

The set of all possible addresses is known as


the virtual address space

Compilers subdivide the virtual address space into


chunks that store different program objects, e.g.:
program data
program instructions
control information

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 mapped region for


shared libraries

Memory
invisible to
user code

printf function

Run-time heap
(created by malloc)
Read/write data
Read-only code and data

Loaded from the


hello executable file

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.

What is the size of the virtual address space on these PCs?

23

Addresses of multi-byte objects


Ex: consider the declaration
int x;
on a 32-bit machine. x takes up 4 bytes in memory.
The convention is that the address of a multi-byte
object is the smallest of the addresses used to store
the bytes.
So if x occupies the addresses 0x100 through
0x103, what is the value of the expression &x?

24

Byte Ordering
There are two ways to store multi-byte objects in
memory
Intel is a little endian architecture

Some other systems use big endian

Least significant byte is stored at lowest address


Least significant byte is stored at highest address

Ex: How would int x = 0x01234567 at address


0x100 be stored?

25

Big Endian

Ex: How would int x = 0x01234567 at address


0x100 be stored?
0x100

0x101

0x102

0x103

01

23

45

67

26

Little Endian

Ex: How would int x = 0x01234567 at address


0x100 be stored?
0x100

0x101

0x102

0x103

67

45

23

01

27

Origin of Big & Little Endians


Jonathan Swift, Gullivers Travels
A law requiring all citizens of Lilliput to break their
soft-eggs at the little ends only
A civil war breaking between the Little Endians and
the Big Endians, resulting in the Big Endians taking
refuge on a nearby island, the kingdom of Blefuscu
Satire over holy wars between Protestant Church of
England and the Catholic Church of France

28

Endianness

Data must be transformed when binary values are


transferred over a network between machines with
different endianness

Endianness is visible when inspecting binary data


representing integers. See the following IA32
instruction and its diassembly:
80483bd: 01 05 64 94 04 08
0x8049464

add %eax,

29

Pointers

Pointers can be to any data type, including structures


int foo[215];
/* An array of ints */
/* p is a pointer to an int, initialized
to foo */
int *p = foo;

We can use subscripts with pointers. Incrementing a


pointer adds the increment times the size of the data
type that it points to
foo[99] is the same thing as p[99]
&foo[99] is the same thing as (p + 99)

30

Code to print byte representation


int main(void) {
int i=12345, j;
unsigned char *p = (unsigned char *) &i;
for (j=0; j<4; j++)
printf(Offset %d: %x\n", j, p[j]);
}
Output on a
little-endian
machine

Offset
Offset
Offset
Offset

0:
1:
2:
3:

39
30
0
0

Decimal: 12345
Binary:
Hex:

0011 0000 0011 1001


3

31

Casting

Reference an object according to a different data


type from which it was created
Casting pointer to unsigned char * creates byte array

Strongly discouraged for most application


programming, but quite useful and even necessary
for system-level programming

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

Consider the following three calls to show_bytes


int val = 0x12345678;
byte_pointer valp = (byte_pointer) &val;
show_bytes(valp, 1); /* A. */
show_bytes(valp, 2); /* B. */
show_bytes(valp, 3); /* C. */
Indicate the values that would be printed by each call
on a little-endian machine and on a big-endian
machine.

34

Problem P. 100

Write a procedure is_little_endian that will


return 1 when compiled and run on a little-endian
machine, and will return 0 when compiled and run
on a big-endian machine. This program should
always run on any machine, regardless of its word
size.

35

Summary
What have we learned this week?
Next week

Lab 1: Bit/Byte Twiddling


Machine level representation of programs (Ch. 3)

Read Chapter 1

36

Anda mungkin juga menyukai