zoukankan      html  css  js  c++  java
  • Computer System Basic Concept(1)

    Note: the following content is copied from my google doc, so the format is weird, and I am too laze to modify it.

    • Be able to describe the basic components of a PC-architecture computer (e.g. the north/south bridges, etc.) and their functions. Be able to enumerate the different mechanisms for devices to communicate with the CPU and memory and describe how they work.
    • Basic components

      CPU: it is the hardware within a computer that carries out the instructions of a computer program by performing the basic arithmetical, logical and I/O operations of the system.

      RAM: it is also called main memory, where the OS, application programs and data are

      currently stored. It is directly accessible through CPU

      Northbridge: manage data communication between CPU and motherboard?

        Southbridge: Facilitates I/O between devices, the CPU, and main memory

      PCI: it is an interconnection system between a microprocessor and attached devices


      Communicate mechanisms



      • Signal from a device to the CPU

      • Interrupt causes the OS to switch to handler code

      • Interrupts transfer control from the running code to an interrupt handler

      – Each interrupt is assigned a number

      – Number acts as an index into the Interrupt Vector Table

      – Table maps interrupt numbers to handlers

      • Interrupts cause a context switch


      Memory Mapping

      • Memory shared by the CPU and a device

      • Memory range reserved for devices, so that CPU can access devices through

        memory access instructions


      • Enables devices to transfer blocks of data directly to memory

      – Interrupt generated when transfer is completed

      • Much faster than the alternative method

      – Interrupt generated after each byte is transferred


      •  Be able to explain the workings of the x86 stack in detail, including how it grows/shrinks, the stack's relation to ESP, how parameters are passed, how local scopes are allocated, and how return values are passed to the caller. Be able to compare and contrast the stack and the heap.

      x86 Stack in detail


      - Growing downstairs

      - Shrinking upstairs

      - Push, ESP is decremented and the value is written to the new address in ESP

      - Pop, the CPU reads the value at the address ESP points to , then increments



      - Points to the top of the stack

      -- The stack grows down, so this is the lowest address

      - Extended Stack Pointer

      Parameter passing

      - The calling code pushes arguments on the stack in reverse order, then uses

       CALL to invoke the function

      Local scope allocate  

      - Within the function, arguments are accessed at offsets from ESP

      Value pass to the caller

      Call a function: call addr

      -       calculates a return address (the address of the instruction following call)

      -       pushes the return address on to the stack

      -       jumps to addr (EIP = addr)

      return from a function:

      -       pops the return address from the stack

      -       jumps to the return address


      - Function return values are stored in a register

      - Most compilers and libraries for a given CPU agree on which register this is.

      on Intel CPUs, it’s EAX(so does pintos)

      Stack vs. Heap

      - Stack, growing downwards, is used for local variables and function calls

      - Heap, growing upwards, is allocated dynamically


      • Be able to list the sequence of things that happen when a PC turns on, starting with the BIOS up through the loading/parsing/execution of the MBR. Be able to explain the most important functions of the BIOS and MBR. Be prepared to explain why chainloading is necessary

      Start Sequence

      1. Start the BIOS

      2. Load settings from CMOS

      3. Initializing devices

      4. Run POST

      5. Initiate the bootstrap sequence

      Goal of BIOS

      – Check hardware to make sure its functional

      – Install simple, low-level device drivers

      – Scan storage media for a Master Boot Record (MBR)

      • Load the boot record into RAM

      • Tells the CPU to execute the loaded code

      Goal of MBR

      - execute the bootloader and do the partition for disk

      Why chainloading

      MBR is too small to hold the entire system

      • Be able to explain the purpose of the interrupt table and the system call table, and how execution flows through these tables.

      Goal of Interrupt Table

      - To reduce the need for a single interrupt handler to search all possible sources of

       interrupts to determine which one needs service

      Goal of system call table

      - To get better program portability on any system which support the same table.

      - To replace actual system calls which are more detailed and difficult to work with.

      Execution flows

      • Memorize the important sections of ELF binaries, including what types of data go in these sections, and how each one is interpreted by the program loader.

      ELF details

      - ELF: Executable and Linkable Format, which is a Unix/Linux program format

      • Sections are the various pieces of code and data that get linked together by the


      • Each segment contains one or more sections

      – Segments are the basic units for the loader

      Sections are the various pieces of code and data that compose a program

      • Key sections:

      .text – Executable code

      .bss – Global variables initialized to zero

      – .data, .rodata – Fixed, constant values

      – .strtab – String constants

      – .symtab – Debug symbols

      • Understand the key difference between protected and user mode, and be prepared to explain why protected mode is a good feature for modern computers.

      Protected mode

      - In computing, protected mode, also called protected virtual address mode, is an

       operational mode of x86-compatible central processing units (CPU). (I think kernel

       mode or ring 0 is also the same as protected mode)

      – Execution with the full privileges of the hardware

      – Read/write to any memory, access any I/O device, read/write any disk sector,    

        send/read any packet

      User mode

      – Limited privileges

      – Only those granted by the operating system kernel

      Why protected mode is good

      • Privileged instructions

      – Available to kernel

      – Not available to user code

      • Limits on memory accesses

      – To prevent user code from overwriting the kernel

      • Timer

      – To regain control from a user program in a loop

      • Safe way to switch from user mode to kernel mode, and vice versa


      • Understand how Linux manages processes (their key execution states, how they are organized into a tree, etc. Be able to explain how context switching is implemented in detail, and be prepared to discuss the various ways that context switching can be triggered. Be prepared to explain the workings of fork() in detail.


      PCB: Process Control Block

      • OS structure that represents a process in memory

      • Created for each process by the loader

      • Managed by the kernel

      Process States

      • As a process executes, it changes state

      new: The process is being created

      running: Instructions are being executed

      waiting: The process is waiting for some event to occur

      ready: The process is waiting to be assigned to a processor

      terminated: The process has finished execution

      Process Tree

      - All processes have parents and if a process spawns other processes, they become its

      children. This mechanism creates a tree of processes.

      Context switching

      – Saves state of a process before a switching to another process

      – Restores original process state when switching back

      Ways trigger context switching

      Voluntary yielding

      - processes must voluntarily give up control by calling an OS API, e.g.


      - Problem

      -- Misbehaving or buggy apps may never yield

      – No guarantee that apps will yield in a reasonable amount of time

      – Wasteful of CPU resources, i.e. what if a process is idle-waiting on I/O?

      Switch during API calls to the OS

      - whenever a process calls an OS API, this gives the OS an opportunity to

      context switch

      - Problems:

      – Misbehaving or buggy apps may never yield

      – Some normal apps don’t use OS APIs for long periods of time


      Switch on I/O

      - when one process is waiting on I/O, switch to another process

      - Problems:

      – Some apps don’t have any I/O for long periods of time

      Switch based on a timer interrupt

      - use a timer interrupt to force context switching at set intervals(think about

      pintos, hehe)

      - Problems:

      – Requires hardware support (a programmable timer)

      • This is built-in to most modern CPUs


      • fork() – system call to create a copy of the current process, and start it running

      – No arguments!


      • Steps to implement UNIX fork()

      – Create and initialize the process control block (PCB) in the kernel

      – Create a new address space

      – Initialize the address space with a copy of the entire contents of the address

        space of the parent

      – Inherit the execution context of the parent (e.g., any open files)

      – Inform the scheduler that the new process is ready to run

      • Be able to compare and contrast various IPC mechanisms.

      IPC: Inter-process Communication
      • Typically, two ways of implementing Inter-process communication (IPC)

      – Shared memory

      • A region of memory that many processes can all read/write

      – Message passing

      • Various OS-specific APIs

      • Pipes

      • Sockets

      • Signals

      I am too lazy to write all the details

      •  Be able to compare and contrast processes and threads. When is one more advantageous than the other?


      • Process creation is heavyweight (i.e. slow)

      – Space must be allocated for the new process

      – fork() copies all state of the parent to the child

      • IPC mechanisms are cumbersome

      – Difficult to use fine-grained synchronization

      – Message passing is slow

      • Each message may have to go through the kernel Thread


      • Light-weight processes that share the same memory and state space

      • Every process has at least one thread

      • Benefits:

      – Resource sharing, no need for IPC

      – Economy: faster to create, faster to context switch

      – Scalability: simple to take advantage of multi-core CPUs


      Threads are better if:

      -       you need to create new ones quickly, on-the-fly

      -       you need to share a lots of state

      Processes are better if:

      -       you want protection: one process that crashes or freezes doesn’t impact the others

      -       you need high security: o