A Tour of Computer Systems

 The lifetime of the hello program 

  • Information Representation
  • Compilation System
  • Hardware Organization 
  • Operating System
#include<stdio.h>

int main()
{
    printf("hello, world\n");
}

hello.c

  • Information Representation
main:
 	subq $8, %rsp
 	movl $.LCO, %edi
 	call puts
 	movl $0, %eax
 	addq $8, %rsp
ret
  • Compilation System

character stream

token stream

token stream

 syntax tree 

 syntax tree 

 syntax tree 

int foo(unsigned int u) {
    return (u > -1) ? 1 : 0;
}

 syntax tree 

 intermediate representation 

 intermediate representation 

 intermediate representation 

 intermediate representation 

 machine code

一个典型的系统硬件构成

  • Hardware Organization 
  • Operating System

Main memory Allocation

  • Contiguous Allocation
  • Paging
  • Segmentation
  • Contiguous Allocation

Single Contiguous Allocation

fixed-sized Allocation

dynamic storage allocation

  • first-fit
  • next-fit
  • best-fit
  • worst-fit
  • Paging

Divide physical memory into fixed-sized blocks called frames

Divide logical memory into blocks of same size called pages

  • Segmentation

CPU Scheduling

  • First-Come First-Serve 
  • Shortest-Job First 
  • Priority 
  • Round-Robin 
  • Shortest Remaining Time First 
  • Tips#1:
    Ints are not Integers, Floats are not Reals

Is x2 ≥ 0?

 

     Float’s: Yes!

 

     Int’s:

     40000 * 40000  ➙ 1600000000

     50000 * 50000  ➙ ??

  • Tips#2:
    Pointers Are Not Limited
typedef struct {
  int a[2];
  double d;
} struct_t;

double fun(int i) {
  struct_t s;
  s.d = 3.14;
  s.a[i] = 1073741824; /* Possibly out of bounds */
  return s.d;
}

fun(0)  ➙  3.14

fun(1)    3.14

fun(2)    3.1399998664856

fun(3)    2.00000061035156

fun(4)    3.14

fun(6)    Segmentation fault

§Result is system specific

  • Tips#2:
    Pointers Are Not Limited
typedef struct {
  int a[2];
  double d;
} struct_t;

double fun(int i) {
  struct_t s;
  s.d = 3.14;
  s.a[i] = 1073741824; /* Possibly out of bounds */
  return s.d;
}

fun(0)  ➙  3.14

fun(1)    3.14

fun(2)    3.1399998664856

fun(3)    2.00000061035156

fun(4)    3.14

fun(6)    Segmentation fault

Explanation:

  • Tips#3:
    Constant factors matter too
void copyij(int src[2048][2048],
            int dst[2048][2048])
{
  int i,j;
  for (i = 0; i < 2048; i++)
    for (j = 0; j < 2048; j++)
      dst[i][j] = src[i][j];
}
void copyji(int src[2048][2048],
            int dst[2048][2048])
{
  int i,j;
  for (j = 0; j < 2048; j++)
    for (i = 0; i < 2048; i++)
      dst[i][j] = src[i][j];
}

4.3ms

81.8ms

2.1 GHz Intel Core i7 Haswell

Core i7 Haswell

2.1 GHz

32 KB L1 SRAM

256 KB L2 SRAM

8 MB L3 SRAM

終わり

A Tour of Computer Systems

By orashi

A Tour of Computer Systems

  • 455