m1522.000800 system programming · m1522.000800 system programming, fall 2018 27 system programming...

28
M1522.000800 System Programming, Fall 2018 Introduction to System Programming

Upload: others

Post on 22-Mar-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: M1522.000800 System Programming · M1522.000800 System Programming, Fall 2018 27 System Programming System software (or systems software) is computer software or an operating system

M1522.000800 System Programming, Fall 2018

Introduction to

System Programming

Page 2: M1522.000800 System Programming · M1522.000800 System Programming, Fall 2018 27 System Programming System software (or systems software) is computer software or an operating system

2M1522.000800 System Programming, Fall 2018

Introduction to System Programming

Why system programming?

Basic operation of a computer system

Summary

Acknowledgement: slides based on the cs:app2e material

Page 3: M1522.000800 System Programming · M1522.000800 System Programming, Fall 2018 27 System Programming System software (or systems software) is computer software or an operating system

3M1522.000800 System Programming, Fall 2018

Why System Programming?

“I program everything in

JavaScript (Python, Ruby, …).

I don’t need to know this stuff.”

Page 4: M1522.000800 System Programming · M1522.000800 System Programming, Fall 2018 27 System Programming System software (or systems software) is computer software or an operating system

4M1522.000800 System Programming, Fall 2018

Why System Programming?

Abstractions only partially reflect reality

To be a good computer engineer you have to know that abstractions in

computer science are only abstractions; the reality may look very different,

especially in special cases

To understand the limitations of these abstractions and write safe and efficient

code you need to know

computer architecture

assembly programming

system programming

Programming, data structures, computer architecture, and system programming

prepare you for more advanced classes in computer science & engineering

operating systems, compilers, embedded systems, networks, data base

systems, …

Page 5: M1522.000800 System Programming · M1522.000800 System Programming, Fall 2018 27 System Programming System software (or systems software) is computer software or an operating system

5M1522.000800 System Programming, Fall 2018

Why System Programming?

Programming

Data

Structures

Computer

Architecture

System

Programming

Operating

Systems

Compilers

Programming

Languages

Computer

SecurityDatabase

Systems

Embedded

Systems

Networking

Graphics

Programming

Theoretical

CS

AI, Machine

Learning

Page 6: M1522.000800 System Programming · M1522.000800 System Programming, Fall 2018 27 System Programming System software (or systems software) is computer software or an operating system

6M1522.000800 System Programming, Fall 2018

Introduction to System Programming

Why system programming?

Basic operation of a computer system

Summary

Page 7: M1522.000800 System Programming · M1522.000800 System Programming, Fall 2018 27 System Programming System software (or systems software) is computer software or an operating system

7M1522.000800 System Programming, Fall 2018

Hardware Organization

General hardware organization

Memory

PU(*) PU PU…

(*) PU = Processing Unit

I/O

processor

I/O

processor…

disk NIC mouse kbdprinter

Page 8: M1522.000800 System Programming · M1522.000800 System Programming, Fall 2018 27 System Programming System software (or systems software) is computer software or an operating system

8M1522.000800 System Programming, Fall 2018

From Batch To Parallel Processing

1940’s: special-purpose computers

Z1, Colossus, ENIAC

“Programmable” by rewiring the system

early 1950’s: general-purpose computers

EDVAC, bombe

programs stored in memory

CPU fetch-execute cycle

single program, single user at a time

Page 9: M1522.000800 System Programming · M1522.000800 System Programming, Fall 2018 27 System Programming System software (or systems software) is computer software or an operating system

9M1522.000800 System Programming, Fall 2018

From Batch To Parallel Processing

mid 1950’s: batch programming

operator combines programs into

batches of programs

executing a batch meant executing

the programs one by one

results available after all jobs in

the batch had completed

“resident monitor”: a first primitive

version of system software (OS)

control card interpreter

loader

device drivers

Page 10: M1522.000800 System Programming · M1522.000800 System Programming, Fall 2018 27 System Programming System software (or systems software) is computer software or an operating system

10M1522.000800 System Programming, Fall 2018

From Batch To Parallel Processing

mid 1950’s: batch programming (cont’d)

no protection:

a faulty job reading too many cards, over-

writing the resident monitor’s memory, or entering an

endless loop would affect the entire system

lead to:

operating modes (user/monitor)

memory protection

execution timers

Page 11: M1522.000800 System Programming · M1522.000800 System Programming, Fall 2018 27 System Programming System software (or systems software) is computer software or an operating system

11M1522.000800 System Programming, Fall 2018

From Batch To Parallel Processing

early 1960’s: multiprogramming

more memory keep several programs

in memory at once

(memory partitioning to separate the

different jobs)

OS monitor could switch between jobs

when one became idle (i.e., waiting for

I/O)

e.g., IBM OS/360

mid 1960’s: timesharing

switch between jobs periodically

access via remote terminals

e.g., CTSS, MULTICS, UNIX

Page 12: M1522.000800 System Programming · M1522.000800 System Programming, Fall 2018 27 System Programming System software (or systems software) is computer software or an operating system

12M1522.000800 System Programming, Fall 2018

From Batch To Parallel Processing

early 1960’s: multiprogramming

more memory keep several programs

in memory at once

(memory partitioning to separate the

different jobs)

OS monitor could switch between jobs

when one became idle (i.e., waiting for

I/O)

e.g., IBM OS/360

mid 1960’s: timesharing

switch between jobs periodically

access via remote terminals

e.g., CTSS, MULTICS, UNIX

Page 13: M1522.000800 System Programming · M1522.000800 System Programming, Fall 2018 27 System Programming System software (or systems software) is computer software or an operating system

13M1522.000800 System Programming, Fall 2018

From Batch To Parallel Processing

late 1970’s: personal computers

single user, dedicated workstation

WIMP user interface

peripherals connected directly

single processor, time-sharing

Page 14: M1522.000800 System Programming · M1522.000800 System Programming, Fall 2018 27 System Programming System software (or systems software) is computer software or an operating system

14M1522.000800 System Programming, Fall 2018

From Batch To Parallel Processing

parallel processing

several physical processors

several cores per physical processor

hyper-threading in a single core

several users, several programs

Page 15: M1522.000800 System Programming · M1522.000800 System Programming, Fall 2018 27 System Programming System software (or systems software) is computer software or an operating system

15M1522.000800 System Programming, Fall 2018

From Batch To Parallel Processing

still to this day

a single (single-threaded) applications executes under the assumption it runs

exclusively on the hardware

private memory

its own CPU

uninterrupted access to peripherals

yet, programs may

be multi-threaded

communicate with each other

this illusion is maintained by the operating system

three core abstractions:

processes, virtual memory, and files

Page 16: M1522.000800 System Programming · M1522.000800 System Programming, Fall 2018 27 System Programming System software (or systems software) is computer software or an operating system

16M1522.000800 System Programming, Fall 2018

System Software Basics

The system software (operating system) manages the hardware

protect H/W from misuse by buggy/malicious programs

provide simple and uniform mechanisms for manipulating hardware devices

Fundamental abstractions provided by the OS

processes

virtual memory

files

Page 17: M1522.000800 System Programming · M1522.000800 System Programming, Fall 2018 27 System Programming System software (or systems software) is computer software or an operating system

17M1522.000800 System Programming, Fall 2018

Processes and Threads

A process is the operating system’s abstraction for a running program

multiple processes can run concurrently

multi-core processors: true parallelism

single-cores: apparent parallelism through context-switching

OS provides the illusion that the process has exclusive access to the H/W

full memory address space

exclusive access to the I/O devices

Page 18: M1522.000800 System Programming · M1522.000800 System Programming, Fall 2018 27 System Programming System software (or systems software) is computer software or an operating system

18M1522.000800 System Programming, Fall 2018

Processes and Threads

Context-Switching

OS keeps track of all state (context) that a process needs in order to run

current PC

register file

memory contents

open files

switch to a new process

periodically

when a process has to wait for an event

A process can consist of multiple threads

not as heavy as full processes

easier sharing of data

typically more efficient scheduling

Page 19: M1522.000800 System Programming · M1522.000800 System Programming, Fall 2018 27 System Programming System software (or systems software) is computer software or an operating system

19M1522.000800 System Programming, Fall 2018

Virtual Memory

An abstraction of the physical memory

Provides each process with the illusion that it has exclusive use of the main

memory

Managed by the OS with the help of a hardware translation unit, the MMU

(memory management unit)

Typical layout on Linux systems

Page 20: M1522.000800 System Programming · M1522.000800 System Programming, Fall 2018 27 System Programming System software (or systems software) is computer software or an operating system

20M1522.000800 System Programming, Fall 2018

Files

a file is a sequence of bytes

in *nix systems, “everything” is modeled as a file

disk

keyboard

mouse

display

network

shared memory

single interface to interact with files

Page 21: M1522.000800 System Programming · M1522.000800 System Programming, Fall 2018 27 System Programming System software (or systems software) is computer software or an operating system

21M1522.000800 System Programming, Fall 2018

Isolation of OS and User Processes

In principle, a user process has no direct access to physical hardware resources

no access to memory of the OS or other user processes

unless explicitly permitted (process relations, shared memory)

no direct access to disk drives

direct access to the CPU, but only in user mode

Page 22: M1522.000800 System Programming · M1522.000800 System Programming, Fall 2018 27 System Programming System software (or systems software) is computer software or an operating system

22M1522.000800 System Programming, Fall 2018

Isolation of OS and User Processes

Process’ View of the Hardware (Linux)

memory

(3GB/128TB)

OS memory

(1GB/128TB)

CPU(s)

exclusive useexclusive use

file

systemnetwork printer/…

access through API

Page 23: M1522.000800 System Programming · M1522.000800 System Programming, Fall 2018 27 System Programming System software (or systems software) is computer software or an operating system

23M1522.000800 System Programming, Fall 2018

Isolation of OS and User Processes

OS’ View of the Hardware

CPU(s)

exclusive use

file

systemnetwork printer/…

access through API

process virtual

memory

physical

memory

time

time sharing

Page 24: M1522.000800 System Programming · M1522.000800 System Programming, Fall 2018 27 System Programming System software (or systems software) is computer software or an operating system

24M1522.000800 System Programming, Fall 2018

Isolation of OS and User Processes

Hardware Support

CPU

user mode

kernel mode

Memory System

address translation

System

timer interrupt

much more these days, mostly for virtualization

devices, etc

Page 25: M1522.000800 System Programming · M1522.000800 System Programming, Fall 2018 27 System Programming System software (or systems software) is computer software or an operating system

25M1522.000800 System Programming, Fall 2018

Isolation of OS and User Processes

Example: Intel x86 Processors

ISA

four levels of protection

– ring 0:

all instructions can be executed

– ring 3:

only instructions that cannot compromise

system state are allowed to execute

example:

– mov $0, %eax set %eax to 0

– hlt halt processor

– wbinvd flush caches with write-back

source: Wikipedia

Page 26: M1522.000800 System Programming · M1522.000800 System Programming, Fall 2018 27 System Programming System software (or systems software) is computer software or an operating system

26M1522.000800 System Programming, Fall 2018

Introduction to System Programming

Abstractions are good but don’t forget reality

Basic operation of a computer system

Summary

Page 27: M1522.000800 System Programming · M1522.000800 System Programming, Fall 2018 27 System Programming System software (or systems software) is computer software or an operating system

27M1522.000800 System Programming, Fall 2018

System Programming

System software (or systems software) is computer software or an operating

system designed to operate and control the computer hardware and to provide

a platform for running application software.(source: Wikipedia)

System Programming

concerns itself with writing system software

from relatively high-level (web servers) down to very low-level (boot loaders)

Page 28: M1522.000800 System Programming · M1522.000800 System Programming, Fall 2018 27 System Programming System software (or systems software) is computer software or an operating system

28M1522.000800 System Programming, Fall 2018

Wrapping it up

In this class you will learn

learn how to interact with the operating system on a low level

get an idea of what the operating system is doing

how to write safer code

how to write faster code

Prerequisites

some programming experience

computer architecture

x86 assembly

SP will require a substantial effort

read the book

do the homework

start with labs early!