cs 342 – operating systems spring 2003 © ibrahim korpeoglu bilkent university1 input/output – 2...

18

Click here to load reader

Upload: ashlie-greer

Post on 06-Jan-2018

215 views

Category:

Documents


0 download

DESCRIPTION

CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 3 Goals of I/O Software Uniform Naming  Access to all files and devices should be done by a uniform naming scheme. /usr/home/ali/project.c is a pathname and filename that specifies a file which can be read and written. /mnt/floppy is a pathname that specifies the floppy drive which can be read and written.  Therefore pathnames are uniform naming schemes that can be used to refer to any device or file.

TRANSCRIPT

Page 1: CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Input/Output – 2 I/O Software CS 342 – Operating Systems Ibrahim Korpeoglu

CS 342 – Operating SystemsSpring 2003

© Ibrahim KorpeogluBilkent University 1

Input/Output – 2I/O Software

CS 342 – Operating SystemsIbrahim KorpeogluBilkent UniversityDepartment of Computer Engineering

Page 2: CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Input/Output – 2 I/O Software CS 342 – Operating Systems Ibrahim Korpeoglu

CS 342 – Operating SystemsSpring 2003

© Ibrahim Korpeoglu Bilkent University 2

Goals of I/O Software

Device Independence It should be possible to write programs that can

access any I/O device without having to specify the device in advance and without knowing the internal details of the device. A program reading a file should be able to read that file

From a floppy drive From a hard disk From a tape drive From a CD-ROM

The programmer should not remodify its program for each device

Page 3: CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Input/Output – 2 I/O Software CS 342 – Operating Systems Ibrahim Korpeoglu

CS 342 – Operating SystemsSpring 2003

© Ibrahim Korpeoglu Bilkent University 3

Goals of I/O Software

Uniform Naming Access to all files and devices should be done by

a uniform naming scheme. /usr/home/ali/project.c is a pathname and filename that

specifies a file which can be read and written. /mnt/floppy is a pathname that specifies the floppy drive

which can be read and written. Therefore pathnames are uniform naming

schemes that can be used to refer to any device or file.

Page 4: CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Input/Output – 2 I/O Software CS 342 – Operating Systems Ibrahim Korpeoglu

CS 342 – Operating SystemsSpring 2003

© Ibrahim Korpeoglu Bilkent University 4

Goals of I/O Software

Error handling Errors need to be handled at the lower layer possible

At the controller if possible At the device drivers of possible Then at the application

Example: reading a disk block Controller computes the checksum. Detect the errors. Tries to

correct them if it can. If controller can not correct a block error, device driver

requests the disk controller to retrieve the same block again, hoping that this time the block will not be erroneous.

Page 5: CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Input/Output – 2 I/O Software CS 342 – Operating Systems Ibrahim Korpeoglu

CS 342 – Operating SystemsSpring 2003

© Ibrahim Korpeoglu Bilkent University 5

Goals of I/O Software

Synchronous versus Asynchronous Transfers I/O hardware operates in an asynchronous manner.

CPU issues a request to read a block. Disk controller retries the block and then interrupts the CPU Asynchronous I/O is interrupt driven.

Programs are easily written of they use synchronous I/O A read() function in a program will block until the data is

available. Application programmers can not easily program using

interrupts. Synchronous I/O is blocking I/O

Operating System should implement synchronous I/O model for applications using the underlying asynchronous I/O model of the hardware.

Page 6: CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Input/Output – 2 I/O Software CS 342 – Operating Systems Ibrahim Korpeoglu

CS 342 – Operating SystemsSpring 2003

© Ibrahim Korpeoglu Bilkent University 6

Goals of I/O Software

Buffering Operating System should buffer data

The speed at which application reads or writes data can not always match the speed at which the data is transferred to or from devices

Sharing or Devices Some devices are sharable by many users at the same

time: hard disk. Some devices are dedicates to a single user while he/she

is using the device: tape, printer, etc. OS should manage these devices and deal with deadlocks

that are the result of using dedicated devices.

Page 7: CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Input/Output – 2 I/O Software CS 342 – Operating Systems Ibrahim Korpeoglu

CS 342 – Operating SystemsSpring 2003

© Ibrahim Korpeoglu Bilkent University 7

Programming Input and Output

Three methods Programmed I/O Interrupt driven I/O I/O using DMA

Page 8: CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Input/Output – 2 I/O Software CS 342 – Operating Systems Ibrahim Korpeoglu

CS 342 – Operating SystemsSpring 2003

© Ibrahim Korpeoglu Bilkent University 8

Programmed I/O

In programmed I/O, CPU is always busy with I/O until I/O gets completed. Fine for single process systems

MS-DOS Embedded systems

But not a good approached for multi-programing and time-sharing systems

Page 9: CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Input/Output – 2 I/O Software CS 342 – Operating Systems Ibrahim Korpeoglu

CS 342 – Operating SystemsSpring 2003

© Ibrahim Korpeoglu Bilkent University 9

Programmed I/O - Example

Assume a program that wants to print a string to a printer.

Assume string is “ABCDEFGH” 8 character long string

Assume printer has 1 byte of data buffer to put the character that needs to be printed.

Page 10: CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Input/Output – 2 I/O Software CS 342 – Operating Systems Ibrahim Korpeoglu

CS 342 – Operating SystemsSpring 2003

© Ibrahim Korpeoglu Bilkent University 10

Application Program

PrintedPage

(empty)

Kernel

BUS

Printer Controller

Main Memory

User Address Space

Kernel Address Space

A B C D E F G H

Application Buffer

Kernel Buffer

Stringto be printed

datastatus

REG

REG

REG

CPUregisters

CPU

Controller registers

Page 11: CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Input/Output – 2 I/O Software CS 342 – Operating Systems Ibrahim Korpeoglu

CS 342 – Operating SystemsSpring 2003

© Ibrahim Korpeoglu Bilkent University 11

Application Program

PrintedPage

(empty)

Kernel

BUS

Printer Controller

Main Memory

A B C D E F G H

Data is copied into Kernel Buffer

datastatus

REG

REG

REG

CPU

Page 12: CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Input/Output – 2 I/O Software CS 342 – Operating Systems Ibrahim Korpeoglu

CS 342 – Operating SystemsSpring 2003

© Ibrahim Korpeoglu Bilkent University 12

Application Program

Kernel

BUS

Printer Controller

Main Memory

A B C D E F G H

datastatus

REG

REG

CPU

A

(1)(2)

(3-4)(3-4)

REGA

A

Page 13: CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Input/Output – 2 I/O Software CS 342 – Operating Systems Ibrahim Korpeoglu

CS 342 – Operating SystemsSpring 2003

© Ibrahim Korpeoglu Bilkent University 13

Application Program

Kernel

BUS

Printer Controller

Main Memory

A B C D E F G H

datastatus

REG

REG

CPU

A

(3-4)

REG

A

A

Page 14: CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Input/Output – 2 I/O Software CS 342 – Operating Systems Ibrahim Korpeoglu

CS 342 – Operating SystemsSpring 2003

© Ibrahim Korpeoglu Bilkent University 14

Application Program

Kernel

BUS

Printer Controller

Main Memory

A B C D E F G H

datastatus

REG

REG

CPU

A

REGB

B

(1)(2)

B(3-4)

B

(3-4)

Page 15: CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Input/Output – 2 I/O Software CS 342 – Operating Systems Ibrahim Korpeoglu

CS 342 – Operating SystemsSpring 2003

© Ibrahim Korpeoglu Bilkent University 15

Application Program

Kernel

BUS

Printer Controller

Main Memory

A B C D E F G H

datastatus

REG

REG

CPU

A

REGC

C

B

C

C

Page 16: CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Input/Output – 2 I/O Software CS 342 – Operating Systems Ibrahim Korpeoglu

CS 342 – Operating SystemsSpring 2003

© Ibrahim Korpeoglu Bilkent University 16

Application Program

Kernel

BUS

Printer Controller

Main Memory

A B C D E F G H

datastatus

REG

REG

CPU

A

REGD

D

B

D

C D

Page 17: CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Input/Output – 2 I/O Software CS 342 – Operating Systems Ibrahim Korpeoglu

CS 342 – Operating SystemsSpring 2003

© Ibrahim Korpeoglu Bilkent University 17

Application Program

Kernel

BUS

Printer Controller

Main Memory

A B C D E F G H

datastatus

REG

REG

CPU

A

REGE

E

B

E

C DE

Page 18: CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Input/Output – 2 I/O Software CS 342 – Operating Systems Ibrahim Korpeoglu

CS 342 – Operating SystemsSpring 2003

© Ibrahim Korpeoglu Bilkent University 18

How would we implement copy in kernel!

/* buffer is user buffer p is kernel buffer count is number of bytes to be copied */copy_from_user(buffer, p, count){

for (i=0; i < count; ++i) /* loop in every character */{

while (*printer_status_register != READY){}; /*busy waiting – loop until ready*/*printer_data_register = p[i]; /* output one

character */}return_to_user();

}