1 Outline Principles of I/O hardware Principles of I/O software I/O software layers Disks Clocks

Preview:

Citation preview

1

Outline

Principles of I/O hardware Principles of I/O software I/O software layers Disks Clocks

2

Types of Disks

Magnetic disks Hard disks and floppy disks Reads/writes are equally fast Ideal secondary memory Highly reliable storage

Optical disks CD-ROM, CD-R: 600MB DVD: 4.7-17GB

Structure of Magnetic Disks

Direction of arm motion

Read/write head (1 per surface)

Surface 0

Surface 1

Track

Sector

Cylinder

1. Cylinders tracks sectors

2. Seek time and rotational delay

4

Cost of Read / Write A Disk Block

Seek time The time to move the arm to the proper

cylinder Dominate the other two times for most disks E.g., 0.8 msec for adjacent cylinders

Rotational delay The time for the proper sector to rotate under

the head E.g, 0.03 msec for adjacent sectors

Actual data transfer time E.g., 17 sec for one sector

5

Specifications of Disks

Geometry Physical geometry: more sectors in outer

cylinders To OS: same number of sectors for all

cylinders: x cylinders, y heads, and z sectors.

Logical block addressing Disk sectors are numbered consecutively

from 0 to some maximum value

6

Disk Formatting

Preamble

Data ECC

A disk sector

• Preamble: recognize the start of the sector. It also contains the cylinder and sector numbers.

• Data: most disks use 512-byte sectors

• ECC: can be used to recover from read errors

• Gap between sectors

Gap

7

Cylinder Skew The position of sector 0 on each track is

offset from the previous track. This offset is called cylinder skew.

Allow the disk to read multiple tracks in one continuous operation without losing data

01

2

3

0

1

2

30

12

3

3

0

1

2

Direction of disk

rotation

Direction of disk

rotation

No skew 1-sector skew

Sector Interleaving• Consider a controller with one sector buffer.

A request of reading two consecutive sectors. When the controller is busy with transferring one sector of data to memory, the next sector will fly by the head.

• Solution: sector interleaving

1

23

0

4

5

67

4

15

0

2

6

37

3

61

0

4

7

25

No interleaving

Single interleaving

Double interleaving

9

Disk Arm Scheduling Algorithm Time required to read/write a disk

block Seek time Rotational delay Actual data transfer time

For most disks, the seek time dominates the other two times.

How to reduce the mean seek time? Assume we know the cylinder numbers

of all the pending requests.

10

Optimize Seek Time Fist-come, first-served:

Accept requests one at a time and carry them out in that order

Little can be done Shortest seek first

Handle the closest request next The arm will tend to oscillate between two

cylinders Starvation: with a heavily loaded disk,

requests for cylinders at either extreme get poor service

11

Elevator Algorithm

Keep moving the arm in the same direction until there are no more outstanding requests in that direction, then switch the direction. Upper bound: twice of the number of

cylinders

An Example

1 5 0 7 2 3 6 4Request order

Cylinder Number

1 6 9 13

19

Initial position

8 16

18

FCFS: cylinder 811316196189, total 59 motions

SSF: cylinder 896113161819, total 27 motions

EA: cylinder 891316181961, total 29 motions

Assume the direction is initially UP.

13

Stable Storage Model No disk works all the time with no

errors Achievable: either correctly writes

the data or does nothing, leaving the existing data intact.

Possible errors (model assumptions): Incorrect write: detected by ECC Correctly written can go bad. However,

ignore the chance that two independent drives have the same sector going bad

14

Model Hardware: a pair of identical disks

Corresponding blocks works together to form one error-free block

At any time, for any pair of blocks, at least one is in good condition.

Target: If no error, the corresponding blocks on

both drives are the same If error occurs, at least one block can

provide the correct data (either new or old)

15

Model: Stable Writes/Reads Stable writes

Write the block in drive 1read and verify Keep writing until it successes

Write the block in drive 2read and verify Stable reads

Read the block from drive 1. If incorrect (detected by examining ECC), the corresponding block in drive 2 must contain the correct data. Why?

Stable writes leave two good copies Both copies go bad: impossible (assumption 2)

16

Model: Crash Recovery Compare corresponding blocks

Both good: nothing is done One is bad: overwrite the bad copy Both good but different: block 1

overwrites block2. Why?

Old Old

Disk1 2

Crash

Bad

Old

Disk1 2

Crash

New Old

Disk1 2

Crash

New

Bad

Disk1 2

Crash

New New

Disk1 2

Crash

17

Outline

Principles of I/O hardware Principles of I/O software I/O software layers Disks Clocks

18

Clocks Essential for multi-programming system

Maintain the time of day Prevent processes from monopolizing the CPU

Hardware components: Crystal oscillator: generate high frequency

periodic signal, typically several hundred MHz Counter: value is decreased by 1 when a

signal is generated. It causes a CPU interrupt when it gets to zero.

Holding register: programmable. Used to control the frequency of interrupt.

19

Operation Modes One-shot mode

Copy the value of the holding register into the counter

Decrease the counter at each pulse (signal) Cause an interrupt if it gets zero Stop

Square-wave mode After causing the interrupt, the holding

register is automatically copied into the counter, and the whole process is repeated again.

20

Summary Hardware Principle

Device controller: between devices and OS

Memory mapped I/O Vs. I/O port number DMA Vs. Interrupt

Software Principle Programmed I/O: waste CPU time Interrupts: overheads DMA: DMA controller may not drive the

device at full speed

21

Summary (Cont.)

Four layers of I/O software Interrupt handlers: context switch,

wake up driver when I/O completed Device drivers: set up device registers,

issue commands, check status and errors

Device-independent software: naming, protection, buffering, allocating

User-space software: make I/O call, format I/O, spooling

22

Summary (Cont.)

Disks Structure: cylindertracksector Optimize seek time: elevator

algorithm Stable storage

Clocks One-shot and square-wave modes

CMPT 300 Operating System I

Chapter 6 File Systems

24

Outline

Files Directories File system implementation

25

Store Information in Memory?

How to store a very large amount of information? All account info in a bank >> main memory size

How to survive the termination of processes? Store your program when the PC is turned off

How to share information among processes Share airline reservation info among multiple

processes Share information around the world?

26

Files: Long-term Storages Files: info on disk/other external media

Persistent, huge capacity, shared by multiple processes

File system: the part of OS dealing with files Structure, name, access, protection, …

User interface of file systems How do files appear to users? Naming rules and protection File operations

27

File Naming Strings as file names

Letters, digits Special characters are also permitted in some system

File name length, usually less than 255 Case sensitive? Processes access files using file names

File name extensions: file_stem.extension Indicates the type of a file

E.g., code.c, code.o, code.c.Z Register extensions with programs

Specify a program to open files with a specific extension

Convention & Enforcement

28

File Structure An unstructured sequence of bytes

Most widely used, e.g., UNIX and Windows User programs impose meaning of files

A sequence of fixed-length records Records have internal structure Read/write in records Not used in any current general-purpose system

A tree of records Search records by keys Used on some large mainframe computers

29

Examples of File Structures

Byte sequence

Name: JackSINAddressAccount #Name: CaroleSINAddressAccount #Name: NicolaSINAddressAccount #Name: AndrewSINAddressAccount #

Record sequence

Tree

Ant

Fox

Pig

Cat

CowDog

Goat Lion Owl

Hen Ibis Lamb

1 byte1 record

30

File Types Regular files – store user’s information

ASCII files (text file): lines of text Can be displayed and printed as is. E.g., source code file

Binary files: binary streams Internal structure know to programs Object file, executable code. Word file?

Directories – maintained by system Maintaining the structure of the file system

UNIX special files: modeling I/O devices Character special files: serial I/O devices Block special files: disks/block devices

31

File Access Sequential access

Read all the bytes in order from the beginning Rewind if read again

Random access files Read the bytes/records by specifying positions Applications: database, etc. All the files are random access nowadays

How to specify the starting point for reading Use seek operation to set the current position

Roll forward/backward for n bytes

32

File Attributes Attribute Meaning

Protection Who can access the file and in what way

Password Password needed to access the file

Creator ID of the person who created the file

Owner Current owner

Read-only flag Read/write or read only

Hidden flag Normal file or the file does not display in listings

System flag Normal file or system file

Archive flag The file has been backed up or not

Random access flag

Sequential access only or random access

ASCII/binary flag ASCII file or binary file

Temporary flag Normal file or file will be deleted on process exit

Lock flags Unlocked or locked

Record length Number of bytes in a record

Key position Offset of the key within each record

Key length Number of bytes in the key field

Creation time Data and time the file was created

Time of last access

Date and time the file was last accessed

Time of last change

Date and time the file was last changed

Current size Number of bytes in the file

Maximum size Number of bytes the file may grow to

File protection and access

Flags control/enable some specific property

Used in file with records having a key

Time stamps

Size

33

File Operations Store and retrieve information System calls relating to files

Open: a process must open a file first before using it

Open mode: read, write, append, modify, etc Close it after you use it

Read: read data starting from the current position Write data

Seek: specify where to take the data Other calls

Delete, append, get attributes, set attributes, rename How to copy a file using these system calls?

34

Outline

Files Directories File system implementation

35

Organization: Single-level / Two-level

Single-level: one directory for all the files Not good for huge amount of files Not good for multi-user system

Two-level: userdirectory How to access a file? A large number of files from one user,

inconvenientroot

User A:File mymails

User B:File mymails

root

User A User B

File mymails File mymails

36

Hierarchical Directory Systems

A general hierarchy: a tree of directories

root

directory

file

directory directory

directory directory

directory

file file

file

file

file

file

User directory

37

Path Names Mechanism to locate files Absolute path name

Path starting from the root directory E.g., /usr/fran/mailbox. ‘/’ is path separator

Relative path name Relative to the current working directory

E.g., if working directory is /usr/fran, then /usr/fran/mailbox = mailbox

Each process has its own working directory Current directory “.” and parent directory “..”

E.g., ../cindy/mailbox, ./mailbox

38

Directory Operations (UNIX) Create: a directory is created

Empty except ‘.’ and “..” entries Delete, rename a directory

Link (hard link) Allow a file to appear in more than one

directory One copy of a file, multiple directory entries

Unlink A directory entry is removed Link count > 0

Yes: remove the file (free the i-node and data blocks) No: keep the file

39

Outline

Files Directories File system implementation

40

File System Layout Disk is divided up into several partitions

Each partition has one file system MBR – master boot record

boot the computer & contain the partition table

Partition table Starting & ending addresses of each partition One partition is marked as active

Within each partition Boot block – first block, a program loads the

OS Superblock – key parameters about the file

sys.MBR

Partition 1 Partition 2

Partition 3

Partition 4

Boot block

Super block

Free space mgmt

I-nodes

Root dir

Files and directories

41

Implementing Files Key issue: how to keep track of which

disk sectors go with which file? E.g., block size= 512B, file size=2014B, so

where are these 2014/514=4 blocks on disk?

Many methods Contiguous allocation Linked list allocation I-nodes Each one has its own pros and cons

42

Contiguous Allocation Store each file as a contiguous run of

disk sectors Advantages

Simple to implement Each file has two numbers, starting address &

length Read performance is excellent

Drawback Holes. Relocate if file grows.

Free blocks

43

Linked List Allocation Keep all the blocks as a linked list

Only the address of the first block is stored

Every sector can be used, no holes Random access is extremely slow The amount of data storage in a sector

is no longer a power of two Read a block of data requires accessing

two sectors File block 0

File block 1

File block 2

0

File block 3

Physical sector 4 7 2 10

44

File Allocation Table (FAT) Move the pointer from each block to a

table in memory FAT– shared by all the files Indexed by physical sector number Each entry contains the number of next block E.g., 47210

Random access is much easier FAT is in main memory

FAT must be held in main memory 20 GB disk, 1 KB/block, 4 bytes/entry 80MB FAT

Size of FAT is linear to size of disk

0

1

2 10

3

4 7

5

6

7 2

8

9

10 -1

11

12

13

14

15

First block

45

Index Nodes (I-nodes)

An i-node lists the attributes and disk addresses of the file’s blocks

Only when a file is open, its i-nodes should be loaded into memory Much smaller than FAT Irrelevant to size of diskFile attributes

Address of disk block 0

Address of disk block 1

Address of disk block 2

Address of disk block 3

Address of block of pointers

Disk block containing

additional disk addresses

46

Implementing Directories Directory system: map the ASCII file

name onto the info needed to locate the data Directory entry

Where are the attributes stored? In the directory entry (MS-DOS/Windows) In the i-nodes (UNIX)Games Attribute

s

Mail Attributes

News Attributes

Work Attributes

DOS/Windows

Games

Mail

News

Work

File attributes

Address of disk block 0

Address of disk block 1

i-node

UNIX

47

Locate A File: /usr/ast/mbox

1 .1 ..

4 bin

7 dev

14 lib

9 etc

6 usr

8 tmp

Attr.

132

…..

6 .1 ..

19

dick

30

erik

51

jim

26

ast

45

bal

Attr.

406

…..

26 .6 ..

64 grants

92 books

60 mbox

81 simix

17 src

root I-node 6 is for /usr

Block 132 is /usr dir.

I-node 26 is for /usr/ast

Block 406 is /usr/ast dir.

Looking up usr yields i-node 6

/usr is in block 132

/usr/ast is i-node 26

/usr/ast is in block 406 /usr/ast/mbox

is i-node 60

Recommended