CSI500/400 Operating Systems

36
CSI500/400 Operating Systems Prof. Seth Chaiken Aug. 26, 2013

Transcript of CSI500/400 Operating Systems

Page 1: CSI500/400 Operating Systems

CSI500/400 Operating Systems

Prof. Seth Chaiken

Aug. 26, 2013

Page 2: CSI500/400 Operating Systems

Organization

Program to running process

Lecture 02

About This Course

Page 3: CSI500/400 Operating Systems

“Flipped classes”

I Pre-class prep homework: Focussed readings & Google formsdue BEFORE a topic is discussed in class or practiced in lab.(Maybe some videos!)

I Traditional lectures are ineffective. We do 3 “Lecturemeetings”/week to question, discuss, demonstrate; NOT tohear something first time.

Page 4: CSI500/400 Operating Systems

First 3 weeks’ assignments: http://www.cs.albany.edu/~sdc/CSI500/Fal13/Assg01Fal13.htmlWeb site: http://www.cs.albany.edu/~sdc/CSI500

Page 5: CSI500/400 Operating Systems

I There’s now a 3rd 5:00PM-7:00 PM Lab meeting MONDAYS(starting TODAY!)

I The original Tue and Thu labs are overcrowded.

I If the Monday time is much much better for you, tell metoday and I’ll reserve a seat.

I Come to lab (LI-99) tonight if possible, and we’ll begin sortingout how to even out the labs.

I With more lab times, I hope to allow the policy that you canattend any lab meeting you want as long there is room; evendifferent days among M, Tue., Thu. each week.

Page 6: CSI500/400 Operating Systems

First class agenda

I Demonstration: “Hello world” source code transformed to anexecutable file and then run in a Gnu/Linux x86 process

I Pre-lab prep homework: Follow directions to try to reproducethe demo and begin the lab homework.

I Labs this week: Help each other out with that!

I Post-topic homework DUE ON PAPER, Friday: Scribble whatI asked for on the two objdump listings handed out.

I Pre-class prep homework: Readings and Google form answersdue NEXT class, Wednesday.

I More Pre-class prep homework: Readings and Google formanswers due next Wednesday (Sept. 4) (there’s only onelecture meeting that week.)

Page 7: CSI500/400 Operating Systems

Second Week Plan

Monday: Labor day (no lecture or lab)Tuesday: Extra Lab meeting for Lab 01, especially for Mondaypeople who left to take the discrete math exam.Wednesday: Lecture meeting.Thursday: No lab.Friday: No Lecture meeting.

Page 8: CSI500/400 Operating Systems

What to do NOW!

1. Go on the Web: Do form for CS Dept. Unix account ANDmake sure you’re getting official UA albany.edu email (a fewof you might have one; ask if unsure)

2. Go on the Web: Do the assigned reading of OperatingSystems, Three Easy Pieces.

3. Get or share the CSAPP book and do the assigned reading ofthat.

4. Go on the Web: Do the PRW 1 Google forms questionnaireBEFORE NEXT LECTURE (Wednesday).

5. Retrieve your email with your new CS Dept. password,remotely log in to the mothra.cs.albany.edu, and start theLabHW 1 BEFORE your lab meeting!

Page 9: CSI500/400 Operating Systems

Hello world! Here are commands, vocabulary and the C languageyou will be using throughout this course.

#include <unistd.h> /* Preprocessor literally REPLACES* this line with the contents of the* file named /usr/include/stdio.h* Among many other things, that file* contains a declaration of write,* like int write( int, char *, int )* [NO! arch. independent types used] */

#include <stdlib.h> /* Needed to declare exit( ) */int main(int argc, char *argv[]){

write(1, "Hello world!\n", 13);/*Terrible that I counted 13 by hand!*/exit( 167 );

}

Page 10: CSI500/400 Operating Systems

How did I know to #include the right files for write, exit, andwhat parameters to use?

$ man 2 write$ man exit

Section 2 of the traditional Unix manual is for system calls; sec. 3is for C library functions.Nowadays, Unix is standardized under POSIX, and the “manpages” are posted all over the Web.

Page 11: CSI500/400 Operating Systems

Make a list of information resources you’ll need throughoutthe semester

1. C Programming: Tutorials, Textbooks, Coursenotes, Reference books ...

2. “man” pages for Unix (POSIX) system interfaceand the C library.

3. Your 500/400 lab notebook w/ dated entries.

4. ...

Page 12: CSI500/400 Operating Systems

Type it into file hello.c with a text editor:

(shell prompt) $ nano hello.c

Command to build an executable file, w/ option to name it helloinstead of a.out

(shell prompt) $ gcc -o hello hello.c

Page 13: CSI500/400 Operating Systems

Running

(won’t say shell prompt anymore)

$ ./helloHello world!$

What happened?A program called “the shell” was and is still running. “shell”means command line interpreter.The shell printed $Knowing that there is an executable file named hello in thatshell’s current working directory, I typed ./helloThe shell scanned what I typed and (after calculations involving ifstatements) invoked OPERATING SYSTEM SYSTEM CALLS to(1) CREATE A NEW PROCESS and then (2) COMMAND THATPROCESS TO FILL UP its virtual memory with contents from filehello and RUN my little program.

Page 14: CSI500/400 Operating Systems

Some subject vocabulary ... list it in your “Studynotebook”

Program. C Preprocessor. Declaration. Types. Shell. C functions,parameter, function call, return value. Command line (how to useand talk about it). File. Executable file. Directory. Currentworking directory. OPERATING SYSTEM. SYSTEM CALL.VIRTUAL MEMORY. RUN.

PROCESS

A program is like a printed script for a play, stored in a library, etc.A PROCESS running that program is like one particularperformance of the play following the script, usually affected byaudience reaction.

Page 15: CSI500/400 Operating Systems

very short history13∗(OS 3 Easy Pieces) == Virtualization What is the REAL,physical machine that is “TRANSFORMED into amore general, powerful, and easy-to-use virtualform of itself

1. Early programmable computers: A student, scientist, tech,etc. puts the program in, computer RUNS it, hopefully there’soutput, and stops. Repeat.

2. A little later: Put the punched card job decks in a hopper. Amonitor software reads a job; the job TAKES OVER THECOMPUTER, and (assuming it stops, doesn’t trash themonitor or even worse, the disk) the monitor runs again.

That “physical machine”

The WHOLE COMPUTER, taken over by your ONE program, isthe MACHINE Arpaci-Dusseau say is Virtualized.Its programming language is the machine/asm language we areseeing now!

Page 16: CSI500/400 Operating Systems

The KIND OF PROGRAMS our OS + HARDWARE runs

kind of programm

Late 20th-early 21st century Operating System PLUS ComputerHardware systems run MACHINE LANGUAGE PROGRAMS.

what’s an OSOperating System (Software) INTEGRATED WITH ComputerHardware (CPU, system board, memory, peripherals, powersupplies, etc. ) make a system for making it easier todesign and run such PROGRAMS.

Page 17: CSI500/400 Operating Systems

NOW: Show/explain what you try to reproduce for your lab thisweek: http://www.cs.albany.edu/~sdc/CSI500

Page 18: CSI500/400 Operating Systems

CSI500/400 Operating SystemsProf. S. ChaikenAugust 28, 2013

Page 19: CSI500/400 Operating Systems

Demonstration notes

Remote access to CS Dept lab from UA PC (on lecturn)

mkdir Lect01Demogit clone git://github.com/chaikens/CSI500lsls -lcd (find the sources)make

That was too magical. Do it by hand: Type in each commandgenerated by make on my Makefile, say what it is, anddemonstrate it.I took time for this to show you what YOU are expected to doyourself!

Page 20: CSI500/400 Operating Systems

Executable file hello built from hello.s SPECIFIESSOME INITIAL CONTENTS OF VIRTUAL MEMORY

Running C Program’s and Other Processes’ Abstraction ofMemory

Page 21: CSI500/400 Operating Systems

The PROGRAM HEADER of an EXECUTABLE FILESPECIFIES virtual memory address ranges where some initialcontents are copied. SEGMENT: One contiguous virtual memoryaddress range over which the run time behavior of memory seemsto be uniform.

Program Header:LOAD off 0x00000000 vaddr 0x08048000

filesz 0x00000096 memsz 0x00000096LOAD off 0x00000098 vaddr 0x08049098

filesz 0x0000000d memsz 0x0000000d[the four] paddr 0x08048000 align 2**12[lines above] flags r-x[are folded so] paddr 0x08049098 align 2**12[you can see ’em] flags rw-

I When the OS is commanded to “execute” hello twosegments are set up and initialized from data in hello

I The OS sets up a few other segments: say stack, “.bss” forsome variables, and, for malloc, the free-store or heap.

Page 22: CSI500/400 Operating Systems

You’ll become a Linux expert who knows

cat /proc/self/maps

prints info about all the segments of the process running the abovecat command. (Try it!)

Page 23: CSI500/400 Operating Systems

Why do ELF program headers specify BOTH a physical and avirtual address (for the beginning) of each segment?Google “ELF standard”, figure out how it’s organized, and find...

Page 24: CSI500/400 Operating Systems

p vaddr

This member gives the virtual address at which the first byte ofthe segment resides in memory.

p paddr

On systems for which physical addressing is relevant, this memberis reserved for the segment’s physical address. This memberrequires operating system specific information, which is describedin the appendix at the end of Book III.

(For our systems, it isn’t relevent.)

Page 25: CSI500/400 Operating Systems

Program

(Static) Machine instructions and initial data that might somedaybe copied into a process and run.

Process(Dynamic) One single, particular run of a program.

Thread(Dynamic) One single flow of control, running machine instructionsone after the other, within one process.Beginner programs (like the ones you have seen already in thiscourse) make their processes run only one thread. Butmultithreading programs are very common now.

Page 26: CSI500/400 Operating Systems

What is threading when a thread runs: .section .text .globl _start_start: nop pushl $printbuf pushl $0xDEADBE0F call ltohex movl $4,%eax movl $1,%ebx movl $printbuf,%ecx movl $9,%edx int $0x80 movl $1,%eax movl $−1,%ebx int $0x80ltohex: pushl %ebp movl $8, %ecx movl %esp, %ebp movl 8(%ebp), %edx pushl %ebx movl 12(%ebp), %ebx jmp .L2.L10: addl $48, %eax movb %al, −1(%ecx,%ebx) subl $1, %ecx je .L8.L11: sarl $4, %edx.L2: movl %edx, %eax andl $15, %eax cmpb $9, %al jle .L10 addl $55, %eax movb %al, −1(%ecx,%ebx) subl $1, %ecx jne .L11.L8: popl %ebx popl %ebp ret

.section .dataprintbuf: .ascii "12345678\n"

Page 27: CSI500/400 Operating Systems

Individual Quiz!

Page 28: CSI500/400 Operating Systems

Team Quiz!

Page 29: CSI500/400 Operating Systems

Draw the states on the black/white board

Page 30: CSI500/400 Operating Systems

Registers and stack–show scribblesWhat is threading when a thread runs: .section .text .globl _start_start: nop pushl $printbuf pushl $0xDEADBE0F call ltohex movl $4,%eax movl $1,%ebx movl $printbuf,%ecx movl $9,%edx int $0x80 movl $1,%eax movl $−1,%ebx int $0x80ltohex: pushl %ebp movl $8, %ecx movl %esp, %ebp movl 8(%ebp), %edx pushl %ebx movl 12(%ebp), %ebx jmp .L2.L10: addl $48, %eax movb %al, −1(%ecx,%ebx) subl $1, %ecx je .L8.L11: sarl $4, %edx.L2: movl %edx, %eax andl $15, %eax cmpb $9, %al jle .L10 addl $55, %eax movb %al, −1(%ecx,%ebx) subl $1, %ecx jne .L11.L8: popl %ebx popl %ebp ret

.section .dataprintbuf: .ascii "12345678\n"

Page 31: CSI500/400 Operating Systems

What is an OS?

1. Operating System

2. Is Software

3. The OS interfaces application software with people, storageand communication media, and exotic devices like robots(through the hardware).

We’ll study HOW OPERATING SYSTEMS WORK

1. What they do.

2. How to use them.

3. Difficulties and Concepts: Concurrency, Synchronization,Resource Management, Organization of Complexity.

4. How they do what they do.

Page 32: CSI500/400 Operating Systems

Why study OS?

1. Create better applications, deploy and manage systems better,and fix problems more efficiently.

2. Gain (maybe you’re first) experience with a highly complexcomputing system. Become able to work with complexity.

3. Learn to think system-wide.

4. Know some technology good for a few years (like 5?).

5. Maybe build device drivers or perhaps even lower-level OScomponents, perhaps in embedded applications like cellphones and robots.

Page 33: CSI500/400 Operating Systems

C is a big part of this course.

Reason ONE

I The interface between popular OS/hardware combinationsand application software is the C language and executionmodel. POSIX says so!

I Unlike Java, C exposes the addressable memory.

Reason TWO

I Most currently active OS are coded mostly in C.

I C with GNU C’s asm extension provides the kind of hardwareaccess that OS code needs. There are very few assemblylanguage files in the Linux kernel.

Page 34: CSI500/400 Operating Systems

The Big Picture

I (When system is not idle,) most of the time, the computerhardware executes machine instructions.

I The CPU keeps track of the memory address of the currentmachine instruction in the Program Counter register (PC)

I The CPU keeps track of a small amount of data in a fewGeneral Purpose and/or Index Registers.

I The CPU activates RAM to fetch machine instructions.

I Each machine instruction tells the CPU to sometimes activateRAM to fetch data, sometimes fetch and/or change data in(CPU) registers, sometimes activate RAM to store data there.Also the new value for the PC is determined.

This part of beautiful interface operates most often.

Page 35: CSI500/400 Operating Systems

i386 Basic Execution Environment

The address space rangesfrom 0 to 232 − 1, which is 4 Gigabinary -1, approximately 4 billion!

Page 36: CSI500/400 Operating Systems

Virtualization

OS + hardware system featuresimplementsONE, SEPARATE, mostly ISOLATED, INDEPENDENTVirtual ComputerCPU (1 or more with REGISTERS) + AddressableMemoryone FOR EACH PROCESScurrently running a program

Good Idea!Imagine that ASM program from lab, by itself, in a bare hardwarecomputer!