InputOutput 2 IO Software CS 342 Operating Systems

  • Slides: 18
Download presentation
Input/Output – 2 I/O Software CS 342 – Operating Systems Ibrahim Korpeoglu Bilkent University

Input/Output – 2 I/O Software CS 342 – Operating Systems Ibrahim Korpeoglu Bilkent University Department of Computer Engineering CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 1

Goals of I/O Software n Device Independence q It should be possible to write

Goals of I/O Software n Device Independence q 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. n A program reading a file should be able to read that file q q n 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 CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 2

Goals of I/O Software n Uniform Naming q Access to all files and devices

Goals of I/O Software n Uniform Naming q Access to all files and devices should be done by a uniform naming scheme. n n q /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. CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 3

Goals of I/O Software n Error handling q Errors need to be handled at

Goals of I/O Software n Error handling q Errors need to be handled at the lower layer possible n n n q At the controller if possible At the device drivers of possible Then at the application Example: reading a disk block n n 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. CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 4

Goals of I/O Software n Synchronous versus Asynchronous Transfers q I/O hardware operates in

Goals of I/O Software n Synchronous versus Asynchronous Transfers q I/O hardware operates in an asynchronous manner. n n n q Programs are easily written of they use synchronous I/O n n n q CPU issues a request to read a block. Disk controller retries the block and then interrupts the CPU Asynchronous I/O is interrupt driven. 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. CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 5

Goals of I/O Software n Buffering q Operating System should buffer data n n

Goals of I/O Software n Buffering q Operating System should buffer data n n 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 q q q 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. CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 6

Programming Input and Output n CS 342 – Operating Systems Spring 2003 Three methods

Programming Input and Output n CS 342 – Operating Systems Spring 2003 Three methods q Programmed I/O q Interrupt driven I/O q I/O using DMA © Ibrahim Korpeoglu Bilkent University 7

Programmed I/O n In programmed I/O, CPU is always busy with I/O until I/O

Programmed I/O n In programmed I/O, CPU is always busy with I/O until I/O gets completed. q Fine for single process systems n n q MS-DOS Embedded systems But not a good approached for multi-programing and time-sharing systems CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 8

Programmed I/O - Example n n Assume a program that wants to print a

Programmed I/O - Example n n Assume a program that wants to print a string to a printer. Assume string is “ABCDEFGH” q n 8 character long string Assume printer has 1 byte of data buffer to put the character that needs to be printed. CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 9

String to be printed Main Memory Application Buffer Application Program User Address Space A

String to be printed Main Memory Application Buffer Application Program User Address Space A CPU registers B C D E F G H Kernel Buffer Kernel Address Space REG Kernel REG BUS status Printer Controller CS 342 – Operating Systems Spring 2003 Printed Page (empty) data Controller registers © Ibrahim Korpeoglu Bilkent University 10

Main Memory Application Program Data is copied into Kernel Buffer CPU A B C

Main Memory Application Program Data is copied into Kernel Buffer CPU A B C D E F G H REG Kernel REG BUS status Printed Page (empty) data Printer Controller CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 11

Main Memory Application Program CPU A REG A C D E F G H

Main Memory Application Program CPU A REG A C D E F G H (1) (2) REG B Kernel A (3 -4) BUS status (3 -4) data A Printer Controller CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 12

Main Memory Application Program CPU A B C D E F G H REG

Main Memory Application Program CPU A B C D E F G H REG A Kernel REG A (3 -4) BUS status data Printer Controller CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 13

Main Memory Application Program CPU A C (1) (2) REG B D E F

Main Memory Application Program CPU A C (1) (2) REG B D E F G H Kernel A B (3 -4) BUS status data B (3 -4) Printer Controller CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 14

Main Memory Application Program CPU A B C D E F G H REG

Main Memory Application Program CPU A B C D E F G H REG C Kernel REG A B C REG BUS status data C Printer Controller CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 15

Main Memory Application Program CPU A B C D E F G H REG

Main Memory Application Program CPU A B C D E F G H REG D Kernel REG A B C D REG BUS status data D Printer Controller CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 16

Main Memory Application Program CPU A B C D E F G H REG

Main Memory Application Program CPU A B C D E F G H REG E Kernel REG BUS status A B C D E data E Printer Controller CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 17

How would we implement copy in kernel! /* buffer is user buffer p is

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(); } CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University 18