Operating Systems Slides 1 - Introduction

35
Operating System Introduction Wang Xiaolin September 3, 2013 [email protected] 1 / 35

description

Handouts version: http://cs2.swfu.edu.cn/~wx672/lecture_notes/os/slides/intro-a.pdf

Transcript of Operating Systems Slides 1 - Introduction

Page 1: Operating Systems Slides 1 - Introduction

Operating SystemIntroduction

Wang Xiaolin

September 3, 2013

) [email protected]

1 / 35

Page 2: Operating Systems Slides 1 - Introduction

Textbooks

A.S. Tanenbaum. Modern Operating Systems. 3rd ed.Pearson Prentice Hall, 2008.Silberschatz, Galvin, and Gagne. Operating SystemConcepts Essentials. John Wiley & Sons, 2011.邹恒明. 计算机的心智:操作系统之哲学原理. 机械工业出版社,2009.D.P. Bovet and M. Cesatı. Understanding The LinuxKernel. 3rd ed. O’Reilly, 2005.

2 / 35

Page 3: Operating Systems Slides 1 - Introduction

Course Web Site

Course web site: http://cs3.swfu.edu.cn/moodleLecture slides:

http://cs2.swfu.edu.cn/∼wx672/lecture_notes/os/slides/Source code:

http://cs2.swfu.edu.cn/∼wx672/lecture_notes/os/src/Sample lab report:

http://cs2.swfu.edu.cn/∼wx672/lecture_notes/os/sample-report/

3 / 35

Page 4: Operating Systems Slides 1 - Introduction

What’s an Operating System?

▶ “Everything a vendor ships when you order an operatingsystem”

▶ It’s the program that runs all the time▶ It’s a resource manager

- Each program gets time with the resource Each program gets space on the resource

▶ It’s a control program- Controls execution of programs to prevent errors and

improper use of the computer▶ No universally accepted definition

4 / 35

Page 5: Operating Systems Slides 1 - Introduction

System Goals

.Convenient vs. Efficient..

.

▶ Convenient for the user — for PCs▶ Efficient — for mainframes, multiusers▶ UNIX

- Started with keyboard + printer, none paid to convenience- Now, still concentrating on efficiency, with GUI support

5 / 35

Page 6: Operating Systems Slides 1 - Introduction

History of Operating Systems

1401 7094 1401

(a) (b) (c) (d) (e) (f)

Cardreader

Tapedrive Input

tapeOutputtape

Systemtape

Printer

Fig. 1-2. An early batch system. (a) Programmers bring cards to1401. (b) 1401 reads batch of jobs onto tape. (c) Operator carriesinput tape to 7094. (d) 7094 does computing. (e) Operator carriesoutput tape to 1401. (f) 1401 prints output.

6 / 35

Page 7: Operating Systems Slides 1 - Introduction

▶ First generation 1945 - 1955- vacuum tubes, plug boards

▶ Second generation 1955 - 1965- transistors, batch systems

▶ Third generation 1965 1980- ICs and multiprogramming

▶ Fourth generation 1980 present- personal computers

7 / 35

Page 8: Operating Systems Slides 1 - Introduction

Job 3

Job 2

Job 1

Operatingsystem

Memorypartitions

Fig. 1-4. A multiprogramming system with three jobs in memory.

8 / 35

Page 9: Operating Systems Slides 1 - Introduction

.Multi-programming is the first instance where theOS must make decisions for the users..

.

▶ Job scheduling — decides which job should be loadedinto the memory.

▶ Memory management — because several programs inmemory at the same time

▶ CPU scheduling — choose one job among all the jobs areready to run

▶ Process management — make sure processes don’toffend each other

9 / 35

Page 10: Operating Systems Slides 1 - Introduction

The Operating System Zoo

▶ Mainframe operating systems▶ Server operating systems▶ Multiprocessor operating systems▶ Personal computer operating systems▶ Real-time operating systems▶ Embedded operating systems▶ Smart card operating systems

10 / 35

Page 11: Operating Systems Slides 1 - Introduction

OS ServicesLike a government

.Helping the users:..

.

▶ User interface▶ Program execution▶ I/O operation▶ File system manipulation▶ Communication▶ Error detection

.Keeping the system efficient:..

.

▶ Resource allocation▶ Accounting▶ Protection and security

11 / 35

Page 12: Operating Systems Slides 1 - Introduction

A Computer System

Bankingsystem

Airlinereservation

Operating system

Webbrowser

Compilers Editors

Application programs

Hardware

Systemprograms

Commandinterpreter

Machine language

Microarchitecture

Physical devices

Fig. 1-1. A computer system consists of hardware, system pro-grams, and application programs. 12 / 35

Page 13: Operating Systems Slides 1 - Introduction

CPU Working Cycle Fetchunit

Fetchunit

Fetchunit

Decodeunit

Decodeunit

Executeunit

Executeunit

Executeunit

Executeunit

Decodeunit

Holdingbuffer

(a) (b)

Fig. 1-6. (a) A three-stage pipeline. (b) A superscalar CPU.

1. Fetch the first instruction from memory2. Decode it to determine its type and operands3. execute it

.Special CPU Registers..

.

▶ Program counter(PC): keeps the memory address of thenext instruction to be fetched

▶ Stack pointer(SP): points to the top of the current stackin memory

▶ Program status(PS): holdscondition code bitsprocessor state

13 / 35

Page 14: Operating Systems Slides 1 - Introduction

System BusMonitor

Keyboard Floppydisk drive

Harddisk drive

Harddisk

controller

Floppydisk

controller

Keyboardcontroller

VideocontrollerMemoryCPU

Bus

Fig. 1-5. Some of the components of a simple personal computer.▶ Address Bus: specifies the memory locations

(addresses) for the data transfers▶ Data Bus: holds the data transfered. bidirectional▶ Control Bus: contains various lines used to route timing

and control signals throughout the system

14 / 35

Page 15: Operating Systems Slides 1 - Introduction

Controllers and Peripherals

▶ Peripherals are real devices controlled by controllerchips

▶ Controllers are processors like the CPU itself, havecontrol registers

▶ Device driver writes to the registers, thus control it▶ Controllers are connected to the CPU and to each other

by a variety of buses

15 / 35

Page 16: Operating Systems Slides 1 - Introduction

ISAbridge

Modem

Mouse

PCIbridgeCPU

Mainmemory

SCSI USB

Local bus

Soundcard Printer Available

ISA slot

ISA bus

IDEdisk

AvailablePCI slot

Key-board

Mon-itor

Graphicsadaptor

Level 2cache

Cache bus Memory bus

PCI bus

Fig. 1-11. The structure of a large Pentium system16 / 35

Page 17: Operating Systems Slides 1 - Introduction

Motherboard Chipsets+--------------+

| Intel Core 2 |

| Processor |

| (CPU) |

+--------------+

|

Front Side Bus

|

+------------+ DDR2 +-------------+ +---------------+

| System RAM |<----------->| Northbridge |<--->| Graphics Card |

+------------+ Channel | Chip | +---------------+

+-------------+

|

DMI Interface

|

+---------------------+ +-------------+ +------------------+

| BIOS (flash memory) |<--->| |<--->| Power management |

+---------------------+ | Southbridge | +------------------+

| Serial ATA Ports |<--->| Chip |<--->| Clock generation |

+---------------------+ | | +------------------+

| USB Ports |<--->| |<--->| PCI Bus |

+---------------------+ +-------------+ +------------------+

17 / 35

Page 18: Operating Systems Slides 1 - Introduction

▶ The CPU doesn’t know what it’s connected to- CPU test bench? network router? toaster? brain

implant?▶ The CPU talks to the outside world through its pins

- some pins to transmit the physical memory address- other pins to transmit the values

▶ The CPU’s gateway to the world is the front-side bus

.Intel Core 2 QX6600..

.

▶ 33 pins to transmit the physical memory address- so there are 233 choices of memory locations

▶ 64 pins to send or receive data- so data path is 64-bit wide, or 8-byte chunks

This allows the CPU to physically address 64GB of memory(233 × 8B)

18 / 35

Page 19: Operating Systems Slides 1 - Introduction

.Some physical memory addressesare mapped away!..

.

▶ only the addresses, not the spaces▶ Memory holes

- 640KB ∼ 1MB- /proc/iomem

.Memory-mapped I/O..

.

▶ BIOS ROM▶ video cards▶ PCI cards▶ ...

This is why 32-bit OSes have problemsusing 4 gigs of RAM.

0xFFFFFFFF +--------------------+ 4GB

Reset vector | JUMP to 0xF0000 |

0xFFFFFFF0 +--------------------+ 4GB - 16B

| Unaddressable |

| memory, real mode |

| is limited to 1MB. |

0x100000 +--------------------+ 1MB

| System BIOS |

0xF0000 +--------------------+ 960KB

| Ext. System BIOS |

0xE0000 +--------------------+ 896KB

| Expansion Area |

| (maps ROMs for old |

| peripheral cards) |

0xC0000 +--------------------+ 768KB

| Legacy Video Card |

| Memory Access |

0xA0000 +--------------------+ 640KB

| Accessible RAM |

| (640KB is enough |

| for anyone - old |

| DOS area) |

0 +--------------------+ 0

What if you don’t have 4G RAM?

19 / 35

Page 20: Operating Systems Slides 1 - Introduction

.the northbridge..

.

1. receives a physical memory request2. decides where to route it

- to RAM? to video card? to ...?- decision made via the memory address map

20 / 35

Page 21: Operating Systems Slides 1 - Introduction

Bootstrapping

.Can you pull yourself up by your own bootstraps?..

.

A computer cannot run without first loading softwarebut must be running before any software can be loaded.

BIOS

InitializationMBR

InitializationKernelEarly

Boot LoaderInitialization

KernelFull

User ModeProcess

First

Kernel Services

Protected Mode

Switch to

BIOS Services

Hardware

CPU in Real Mode CPU in Protected Mode

Time Flow

21 / 35

Page 22: Operating Systems Slides 1 - Introduction

Intel x86 Bootstrapping1. BIOS (0xfffffff0)

1). POST 2). HW init 3). Find a boot device (FD,CD,HD...)4). Copy sector zero (MBR) of the boot device (e.g. HD) toRAM (0x00007c00)

2. MBR – the first 512 bytes, contains▶ Small code (< 446Bytes), e.g. GRUB stage 1, for loading

GRUB stage 2▶ the primary partition table (= 64Bytes)▶ its job is to load the second-stage boot loader.

3. GRUB stage 2 — load the OS kernel into RAM4. startup5. init — the first user-space program

|<-------------Master Boot Record (512 Bytes)------------>|

0 439 443 445 509 511

+----//-----+----------+------+------//---------+---------+

| code area | disk-sig | null | partition table | MBR-sig |

| 440 | 4 | 2 | 16x4=64 | 0xAA55 |

+----//-----+----------+------+------//---------+---------+

sudo hd -n512 /dev/sda22 / 35

Page 23: Operating Systems Slides 1 - Introduction

Why Interrupt?

.While a process is reading a disk file, can we do.....

.

while(!done_reading_a_file())

{

let_CPU_wait();

// or...

lend_CPU_to_others();

}

operate_on_the_file();

23 / 35

Page 24: Operating Systems Slides 1 - Introduction

Modern OS are Interrupt Driven

▶ Hardware INT by sending a signal to CPU▶ Software INT by executing a system call▶ Trap (exception) is a software-generated INT coursed by

an error or by a specific request from an user program▶ Interrupt vector is an array of pointers pointing to the

memory addresses of interrupt handlers. This array isindexed by a unique device number

∼$ less /proc/devices∼$ less /proc/interrupts

24 / 35

Page 25: Operating Systems Slides 1 - Introduction

Programmable Interrupt Controllers

InterruptINT

IRQ 0 (clock)IRQ 1 (keyboard)

IRQ 3 (tty 2)IRQ 4 (tty 1)IRQ 5 (XT Winchester)IRQ 6 (floppy)IRQ 7 (printer)

IRQ 8 (real time clock)IRQ 9 (redirected IRQ 2)IRQ 10IRQ 11IRQ 12IRQ 13 (FPU exception)IRQ 14 (AT Winchester)IRQ 15

ACK

Master interrupt controller

INT

ACK

Slave interrupt controller

INT

CPU

INTAInterrupt

ack

s y s t e m d a t a b u s

Figure 2-33. Interrupt processing hardware on a 32-bit Intel PC.

25 / 35

Page 26: Operating Systems Slides 1 - Introduction

Interrupt Processing

CPUInterruptcontroller

Diskcontroller

Disk drive

Current instruction

Next instruction

1. Interrupt3. Return

2. Dispatch to handler

Interrupt handler

(b)(a)

1

3

4 2

Fig. 1-10. (a) The steps in starting an I/O device and getting aninterrupt. (b) Interrupt processing involves taking the interrupt,running the interrupt handler, and returning to the user program.

26 / 35

Page 27: Operating Systems Slides 1 - Introduction

Interrupt Timeline1.2 Computer-System Organization 9

userprocessexecuting

CPU

I/O interruptprocessing

I/Orequest

transferdone

I/Orequest

transferdone

I/Odevice

idle

transferring

Figure 1.3 Interrupt time line for a single process doing output.

the interrupting device. Operating systems as different as Windows and UNIXdispatch interrupts in this manner.

The interrupt architecture must also save the address of the interruptedinstruction. Many old designs simply stored the interrupt address in afixed location or in a location indexed by the device number. More recentarchitectures store the return address on the system stack. If the interruptroutine needs to modify the processor state—for instance, by modifyingregister values—it must explicitly save the current state and then restore thatstate before returning. After the interrupt is serviced, the saved return addressis loaded into the program counter, and the interrupted computation resumesas though the interrupt had not occurred.

1.2.2 Storage Structure

The CPU can load instructions only from memory, so any programs to run mustbe stored there. General-purpose computers run most of their programs fromrewriteable memory, called main memory (also called random-access memoryor RAM). Main memory commonly is implemented in a semiconductortechnology called dynamic random-access memory (DRAM). Computers useother forms of memory as well. Because the read-only memory (ROM) cannotbe changed, only static programs are stored there. The immutability of ROMis of use in game cartridges. EEPROM cannot be changed frequently and socontains mostly static programs. For example, smartphones have EEPROM tostore their factory-installed programs.

All forms of memory provide an array of words. Each word has itsown address. Interaction is achieved through a sequence of load or storeinstructions to specific memory addresses. The load instruction moves a wordfrom main memory to an internal register within the CPU, whereas the storeinstruction moves the content of a register to main memory. Aside from explicitloads and stores, the CPU automatically loads instructions from main memoryfor execution.

A typical instruction–execution cycle, as executed on a system with a vonNeumann architecture, first fetches an instruction from memory and storesthat instruction in the instruction register. The instruction is then decodedand may cause operands to be fetched from memory and stored in some

27 / 35

Page 28: Operating Systems Slides 1 - Introduction

System Calls

.A System Call..

.

▶ is how a program requests a service from an OS kernel▶ provides the interface between a process and the OS

28 / 35

Page 29: Operating Systems Slides 1 - Introduction

Program 1 Program 2 Program 3

+---------+ +---------+ +---------+

| fork() | | vfork() | | clone() |

+---------+ +---------+ +---------+

| | |

V V V

+-----------------------------------+

| C Library |

+-----------------o-----------------+

| User space

------------------|-----------------------------------

| Kernel space

|

| +---------+

V : ... :

+-------------+ 3 +---------+

| System call |--o---->| fork() |--> sys_fork() ---.

+-------------+ | +---------+ |

| : ... : |

| 120 +---------+ |

|---->| clone() |--> sys_clone() --|

| +---------+ |

| : ... : |

| 289 +---------+ |

‘---->| vfork() |--> sys_vfork() --|

+---------+ |

: ... : V

+---------+ do_fork()

System Call Table

29 / 35

Page 30: Operating Systems Slides 1 - Introduction

User program 2�

User program 1�

Kernel call

Service �

procedure�

Dispatch table�

User programs �

run in user mode �

Operating �

system �

runs in �

kernel mode

4�

3�

1

2�

Mai

n m

emor

y

Figure 1-16. How a system call can be made: (1) User pro-gram traps to the kernel. (2) Operating system determines ser-vice number required. (3) Operating system calls service pro-cedure. (4) Control is returned to user program.

30 / 35

Page 31: Operating Systems Slides 1 - Introduction

.The 11 steps in making the system callread(fd,buffer,nbytes)..

.

Return to caller

410

6

0

9

7 8

321

11

DispatchSys callhandler

Address0xFFFFFFFF

User space

Kernel space (Operating system)

Libraryprocedureread

User programcalling read

Trap to the kernelPut code for read in register

Increment SPCall readPush fdPush &bufferPush nbytes

5

Fig. 1-17. The 11 steps in making the system callread(fd, buffer, nbytes).

31 / 35

Page 32: Operating Systems Slides 1 - Introduction

Process management22222222222222222222222222222222222222222222222222222222222222222222222222222222222Call Description22222222222222222222222222222222222222222222222222222222222222222222222222222222222

pid = fork( ) Create a child process identical to the parent22222222222222222222222222222222222222222222222222222222222222222222222222222222222pid = waitpid(pid, &statloc, options) Wait for a child to terminate22222222222222222222222222222222222222222222222222222222222222222222222222222222222s = execve(name, argv, environp) Replace a process’ core image22222222222222222222222222222222222222222222222222222222222222222222222222222222222exit(status) Terminate process execution and return status222222222222222222222222222222222222222222222222222222222222222222222222222222222221111111

1111111

1111111

File management22222222222222222222222222222222222222222222222222222222222222222222222222222222222Call Description22222222222222222222222222222222222222222222222222222222222222222222222222222222222

fd = open(file, how, ...) Open a file for reading, writing or both22222222222222222222222222222222222222222222222222222222222222222222222222222222222s = close(fd) Close an open file22222222222222222222222222222222222222222222222222222222222222222222222222222222222n = read(fd, buffer, nbytes) Read data from a file into a buffer22222222222222222222222222222222222222222222222222222222222222222222222222222222222n = write(fd, buffer, nbytes) Write data from a buffer into a file22222222222222222222222222222222222222222222222222222222222222222222222222222222222position = lseek(fd, offset, whence) Move the file pointer22222222222222222222222222222222222222222222222222222222222222222222222222222222222s = stat(name, &buf) Get a file’s status information222222222222222222222222222222222222222222222222222222222222222222222222222222222221111111111

1111111111

1111111111

Directory and file system management22222222222222222222222222222222222222222222222222222222222222222222222222222222222Call Description22222222222222222222222222222222222222222222222222222222222222222222222222222222222

s = mkdir(name, mode) Create a new directory22222222222222222222222222222222222222222222222222222222222222222222222222222222222s = rmdir(name) Remove an empty directory22222222222222222222222222222222222222222222222222222222222222222222222222222222222s = link(name1, name2) Create a new entry, name2, pointing to name122222222222222222222222222222222222222222222222222222222222222222222222222222222222s = unlink(name) Remove a directory entry22222222222222222222222222222222222222222222222222222222222222222222222222222222222s = mount(special, name, flag) Mount a file system22222222222222222222222222222222222222222222222222222222222222222222222222222222222s = umount(special) Unmount a file system222222222222222222222222222222222222222222222222222222222222222222222222222222222221111111111

1111111111

1111111111

Miscellaneous2222222222222222222222222222222222222222222222222222222222222222222222222222222222Call Description2222222222222222222222222222222222222222222222222222222222222222222222222222222222

s = chdir(dirname) Change the working directory2222222222222222222222222222222222222222222222222222222222222222222222222222222222s = chmod(name, mode) Change a file’s protection bits2222222222222222222222222222222222222222222222222222222222222222222222222222222222s = kill(pid, signal) Send a signal to a process2222222222222222222222222222222222222222222222222222222222222222222222222222222222seconds = time(&seconds) Get the elapsed time since Jan. 1, 197022222222222222222222222222222222222222222222222222222222222222222222222222222222221111111

1111111

1111111

Fig. 1-18. Some of the major POSIX system calls. The return codes is −1 if an error has occurred. The return codes are as follows:pid is a process id, fd is a file descriptor, n is a byte count, positionis an offset within the file, and seconds is the elapsed time. Theparameters are explained in the text.

32 / 35

Page 33: Operating Systems Slides 1 - Introduction

System Call Examples

.fork()..

.

1 #include <stdio.h>

2 #include <unistd.h>

3

4 int main ()

5 {

6 printf("Hello World!\n");

7 fork();

8 printf("Goodbye Cruel World!\n");

9 }

man 2 fork

33 / 35

Page 34: Operating Systems Slides 1 - Introduction

.exec()..

.

1 #include <stdio.h>

2 #include <unistd.h>

3

4 int main ()

5 {

6 printf("Hello World!\n");

7 if(fork() != 0 )

8 printf("I am the parent process.\n");

9 else {

10 printf("A child is listing the directory contents...\n");

11 execl("/bin/ls", "ls", "-al", NULL);

12 }

13 return 0;

14 }

man 3 exec

34 / 35

Page 35: Operating Systems Slides 1 - Introduction

Hardware INT vs. Software INT

Device: Send electrical signal to interrupt controller.

Controller: 1. Interrupt CPU. 2. Send digital identification of interrupting device.

Kernel: 1. Save registers. 2. Execute driver software to read I/O device. 3. Send message. 4. Restart a process (not necessarily interrupted process).

Caller: 1. Put message pointer and destination of message into CPU registers. 2. Execute software interrupt instruction.

Kernel: 1. Save registers. 2. Send and/or receive message. 3. Restart a process (not necessarily calling process).

(a) (b)

Figure 2-34. (a) How a hardware interrupt is processed. (b)How a system call is made.

35 / 35