CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

32
CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    219
  • download

    6

Transcript of CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

Page 1: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

CSCI2413 Lecture 9

Operating Systems

Input/Output systems

phones off (please)

Page 2: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 2

Lecture Outline

• Device Management– block devices and character devices– Clocks and Timers– device controllers

• Principles of I/O programming– direct memory access– Interrupt/ polling

• Disk Scheduling Concepts– Scheduling policies

Page 3: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 3

Device Management

• Device management is the part of the operating system that’s responsible for directly manipulating the hardware devices– there are several approaches to implementing I/O

• I/O polling• interrupt driven I/O• direct memory access (DMA)

• There are many different types of I/O device and different I/O methods may be suited to each– the exact solution depends on both the operating

system and the underlying hardware

Page 4: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 4

Devices

• General types include• storage devices (disks, tapes)• transmission devices (network cards, modems)• human-interface devices (screen, keyboard, mouse)

• A device communicates with a system by sending signals over a cable or ‘through the air’– the connection point is termed a port– a common set of wires used is a bus

• A controller operates a port, a bus, or a device

Page 5: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 5

Block and Character Devices

• Block devices include disk drives– Commands include read, write, seek – Raw I/O or file-system access– Memory-mapped file access possible

• Character devices include keyboards, mice, serial ports– Commands include get, put– Libraries layered on top allow line editing

Page 6: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 6

Clocks and Timers

• Provide current time, elapsed time, timer

• If programmable interval time used for timings, periodic interrupts

• ioctl (on UNIX) covers odd aspects of I/O such as clocks and timers

Page 7: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 7

PC System Structure

PCI bus

processor

bridge/memorycontroller

memory

cache

IDE disk controller

disk a

disk b

disk c

disk d

graphics controller

monitor

expansion bus

expansion bus interface keyboard

serialport

parallelport

SCSI controller

SC

SI b

us

dev

dev

dev

Page 8: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 8

I/O Ports

• How do a processor and controller communicate?– the controller has one or more registers for data and control

signals– the processor reads or writes bit patterns in the controller

registers

• A special I/O port address bus can be used– special processor instructions read/write I/O addresses

• Alternatively, sections of regular address space can be reserved for such controller registers– this is called memory-mapped I/O– normal instructions read/write memory addresses

Page 9: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 9

PC I/O Ports Example

• I/O address space I/O address range device000-00F DMA controller

020-021 interrupt controller

040-043 system timer

200-20F game controller

2F8-2FF serial port (second.)

320-32F hard disk controller

378-37F parallel port

3D0-3DF graphics controller

3F0-3F7 floppy disk controller

3F8-3FF serial port (prim.)

• Main memory address space

mem address range device000A0000-000BFFFF XVGA screen contents

Page 10: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 10

Direct I/O with Polling

• An I/O port typically consists of four registersstatus read by host to indicate e.g. busy, error, data available

control written by host to start command or change state/mode

data-in read by the host to get input from the device

data-out written by the host to send output to the device

• A typical communications protocol is

while ( status(port) == BUSY ) ;control(port)= READ;while ( status(port) != DATA ) ;char= data-in(port);

INPUTwhile ( status(port) == BUSY ) ;byte-out(port)= char;control(port)= WRITE;while ( status(port) == BUSY ) ;

OUTPUT

– such busy waiting can consume a lot of processor time

Page 11: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 11

Interrupt-driven I/O• Interrupts can be used to alert the processor when a

device becomes available (usually with data)

CPUI/O

controller1. device driver

initiates I/O

executing other instructions

2. initiates I/O

3. input ready, output complete, or error

4. CPU receives IRQtransfers control tointerrupt handler

5. interrupt handlerprocesses data,

returns from interrupt

6. CPU resumesprocessing of

interrupted task

Page 12: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 12

Pentium Interrupt Vectors

• Interrupts 0 to 31 are non-maskable– they cannot be ignored– signal error conditions

• Interrupts 32 to 255 are maskable– general purpose– e.g. device-generated

interrupts

vector number description0 divide by zero1 debug exception2 null interrupt3 breakpoint4 overflow5 bound range6 invalid opcode7 device not available8 double fault9 coprocessor overrun10 invalid task state segment11 segment not present12 stack fault13 general protection14 page fault15 (reserved, do not use)16 floating-point error17 alignment check18 machine check

19-31 (reserved, do not use)32-255 maskable interrupts

Page 13: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 13

Direct Memory Access

• Bypasses CPU to transfer data directly between I/O device and memory

• In order to initiate a DMA I/O transfer– the processor writes DMA command block to memory

• contains pointer to data block, data size and an identifier for the destination device

– a single I/O request is made to the DMA controller– the DMA controller chip reads the command block and

reads/writes data between physical device & memory

Page 14: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 14

A DMA Example

PCI bus

IDE disk controller

disk

disk

disk

disk

processor

CPU memory busDMA/bus/interrupt

controllermemory

cache

bufferx

1. device driver asked for disk data block to be read into memory

2. device driver tellsdisk controller to readn bytes to address x

3. disk controller initiates DMA transfer

4. disk controller sends n bytes in sequence to DMA controller

5. DMA controller transfers bytes to memory at address x

6. when transfer is is completed DMA sends interrupt

Page 15: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 15

I/O Software

• Key concept in I/O design is device independence

• The user level software (applications) should not need to know details of the hardware devices

• Ideally, all devices should appear identical to applications

– input may be from the keyboard, hard disk or from another application

– output may go to the user’s screen, a floppy disk, a hard disk

Page 16: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 16

I/O Device Characteristics

• I/O devices vary in many different waysaspect variation example

data-transfer modeblock

characterdisk

terminal

access methodsequential

randommodem

CD-ROM

transfer schedulesynchronous (regular timing)

asynchronous (irregular timing)tape

keyboard

device speed

latencyseek time

transfer ratedelay between operations

transfer directionread onlywrite onlyread-write

CD-ROMgraphics controller

disk

Page 17: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 17

Device Drivers

• All the device dependent code resides in the device drivers– each driver handles one device

• or one class of closely related devices

• The kernel issues the device driver with high level I/O requests (e.g. read / write)

• The device driver knows of the device controller registers and issues the appropriate commands

• If the command requires waiting– the device driver blocks until an interrupt is received

• Device driver must run in kernel (privileged) mode

Page 18: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 18

Buffering

• A buffer is a memory area storing data while being transferred between a device and an application

• Buffering is done for three reasons– to cope with speed mismatches

• e.g. downloading a file to hard disk via a modem

– to adapt between devices with different transfer sizes• e.g. packets between network devices

– to provide cleaner kernel-application interface• e.g. a write system call copies application data into a kernel

buffer so that application can continue & alter its data

• In double-buffering one buffer provides work area while another buffer is being transferred to device

Page 19: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 19

UNIX Mechanisms

• At the system call level UNIX provides a uniform device driver abstraction– read, write, (seek)– ioctl: input/output control

• At the user level UNIX represents device names in the regular file-system name space– the /dev directory contains devices

Page 20: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 20

Disk Scheduling Concepts

• The operating system is generally responsible for using the hardware efficiently

• Whenever a process needs I/O to or from a disk, it issues a system call to the operating system– the system call request specifies

• whether the operation is input or output• what the disk address for the operation is• what the memory address of the transfer is• the number of bytes to be transferred

• If the drive (and its controller) are free, the request can be serviced immediately– if not, the request will have to wait in a pending queue

Page 21: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 21

Disk Scheduling Policies• First-Come, First-Served (FCFS)

– Process requests sequentially– Fair to all processes

• Priority by Process (PRI)– Goal is not to optimize disk use but to meet other objectives– Short batch jobs may have higher priority– Provide good interactive response time

• Last-Come, First-Served (LIFO)– Good for transaction processing systems

• The device is given to the most recent user so there should be little arm movement

– Possibility of starvation since a job may never regain the head of the line

• Shortest Seek Time First (SSTF)– Select the disk I/O request that requires the least movement of the disk

arm from its current position– Always choose the minimum Seek time

Page 22: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 22

Disk Scheduling Policies• SCAN

– Arm moves in one direction only, satisfying all outstanding requests until it reaches the last track in that direction

– Direction is reversed

• C-SCAN– Restricts scanning to one direction only– When the last track has been visited in one direction, the arm is

returned to the opposite end of the disk and the scan begins again

• N-step-SCAN– Segments the disk request queue into sub-queues of length N– Sub-queues are processed one at a time, using SCAN– New requests added to other queue when queue is processed

• FSCAN– Two queues– One queue is empty for new request

Page 23: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 23

First-Come, First-Served

• As in process scheduling, the simplest algorithm is to keep a queue of all pending requests and service them in the order they joined the queue– this is (again) called first-come, first-served (FCFS)– also referred to as first-In, first-Out (FIFO)

Page 24: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 24

FCFS Example• Consider a queue, for a disk with 200 cylinders,

with requests for I/O to blocks on cylinders– 98, 183, 37, 122, 14, 124, 65, 67– the disk heads are initially positioned at cylinder 53

– a total head movement of 640 cylinders has occurred

0 14 37 53 65 67 98 122124 183 199

Page 25: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 25

Shortest-Seek(-Time)-First

• The shortest-seek-first (or shortest-seek-time-first) algorithm improves the FCFS situation by trying to reduce the head movement distance by sorting the pending I/O requests according to closeness– on a hard disk the seek time is proportional to distance

• Note that this algorithm is similar to SJF– it too may result in starvation

• if near blocks join the queue then blocks that are far away will never get serviced (particularly if the disk is busy)

Page 26: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 26

– the total head movement has been reduced to 236

0 14 37 53 65 67 98 122124 183 199

SS(T)F Example

• The same queue as for FCFS– 98, 183, 37, 122, 14, 124, 65, 67– the disk heads are initially positioned at cylinder 53

Page 27: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 27

SCAN

• The disk arm starts at one end of the disk, and moves toward the other end, servicing requests until it gets to the other end of the disk, where the head movement is reversed and servicing continues.

• Sometimes called the elevator algorithm.• Illustration shows total head movement of 208

cylinders.

Page 28: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 28

SCAN (Cont.)

Page 29: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 29

IDE (Drive controller)

• The best choice of algorithm may depend on the internal physical drives characteristics– how can an operating system know about these?

• PC’s have a further refinement to this scheme– the integrated/intelligent drive electronics (IDE)

controller has an on-board processor, its own block mapping table and cache memory

• The OS block mapping from 1 .. N into cylinders, heads and sectors no longer corresponds to the actual physical C,H & S of the hard drive– it is a logical mapping

Page 30: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 30

Modern (E)IDE

user applications

files, directories, etc

high-level file system

logical blocks (1 .. N)

basic file system

logical cylinders, heads and sectors

BIOS interface

physical cylinders, heads and sectors

IDE interface

• There may be no correspondence between the logical C,H & S map and the physical disk– there is little / no point in

implementing complex scheduling algorithms

– requests are supplied to IDE controller in FCFS

– the intelligent controller sorts out reading and implements caching

Page 31: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 31

SCSI

• There are other types of drive controllers used in modern PC’s– the main of which is the small computer system

interface (SCSI) bus• used more widely in non IBM-PC (esp. UNIX, Macs)• originally designed just for hard disk drives, but now extended

to CD-ROM’s, scanners, faxes, etc.

• A SCSI interface can be incorporated into PC’s using a SCSI controller card– the commands are issued at a higher level using

logical block numbers (not physical location)– so disk scheduling algorithms are also not required

Page 32: CSCI2413 Lecture 9 Operating Systems Input/Output systems phones off (please)

© De Montfort University, 2004/5 CSCI2413 - L9 32

Summary• A substantial fraction of any operating system is concerned with I/O. I/O can be

accomplished in one of three ways: programmed I/O, in which the CPU inputs or outputs each byte or word and waits in a loop until it can send the next one, the CPU polls the device (as with sending a file to printer) to see if it is ready to accept another byte or word (this process is called polling) ; second: Interrupt driven I/O, in which the CPU starts an I/O transfer and goes off to do something else until interrupted, signalling I/O completion; third: DMA, in which a separate chip manages the complete transfer of a block of data, interrupted only when the entire block has been transferred.

• Disk-Scheduling is one of the most widely used approaches to improve disk I/O performances. Disk caching is also a very useful concept (find out).

• Selecting a Disk-Scheduling Algorithm• SSTF is common and has a natural appeal• SCAN and C-SCAN perform better for systems that place a heavy load on the disk.• Performance depends on the number and types of requests.• Requests for disk service can be influenced by the file-allocation method.• The disk-scheduling algorithm should be written as a separate module of the

operating system, allowing it to be replaced with a different algorithm if necessary.• Either SSTF or LOOK is a reasonable choice for the default algorithm.