I/O Operations Three techniques are possible for I/O operations: 1) With Programmed I/O, data are exchanged between the CPU and the I/O module. The CPU executes a program that gives it direct control of the I/O operation, including sensing Device Status, sending a read/write command, and transferring the data. When the CPU issues a command to the I/O module, it must wait the I/O operation is complete. If the CPU is faster than the I/O module, this is wasteful of CPU time. 2) With Interrupt-Driven I/O, the CPU issues an I/O command, continues to execute other instructions, and is interrupted by the I/O module when the latter has completed its work. With both Programmed and Interrupt I/O, the CPU is responsible for extracting data from Main Memory for output and storing data in Main Memory for input. 3) With Direct Memory Access (DMA), the alternative, the I/O module and Main Memory exchange data directly, without CPU involvement. "Three Techniques for input of a block data": a) Programmed I/O b) Interrupt-Driven I/O c) Direct Memory Access To execute an I/O instruction, the CPU issues an address, specifying the specific I/O module and external device, and an I/O command. There are four types of I/O commands that an I/O module may receive when it is addressed by a CPU. They are classified as Control, Test, Read, and Write: 1) Control command is used to activate a peripheral and tell it what to do. 2) Test command is used to test various status conditions associated with an I/O module and its peripherals. 3) Read command causes the I/O module to obtain an item of data from the peripheral and place it in an internal buffer (Data Register: Figure 6.3). The CPU can then obtain the data by requesting that the I/O module place it on the data bus. 4) Write command causes the I/O module to take an item of data (byte or word) from the data bus and subsequently transmit that data item to the peripheral. I/O Operations With Programmed I/O, there is a close correspondence between the I/O instructions (the CPU fetches from memory) and the I/O commands (the CPU issues to an I/O module to execute the instructions). That is, the instructions are easily mapped into I/O commands, and there is often a simple one-to-one relationship. The form of the instruction depends on the way in which external devices are addressed. Example 1. Consider the following segment of a program: Print PgmMsg | . . . . | | . . . . | 200 instructions that can be executed | . . . . | before the execution of the "Print PgmMsg" | . . . . | instruction is completed | . . . . | Assume: a) PgmMsg is 80 bytes long. b) CPU executes instructions at the rate of 500 inst/sec. Note: 500 inst/sec -> 2 msec/inst 1. Given that a Programmed I/O controller moves 100 bytes/sec and that the CPU has to execute 50 instructions to move a byte: Note: 100 bytes/sec -> 10 msec/byte 50 inst x 2 msec/inst = 100 msec 1) How long will it take to execute the given program segment completely? a) Time (in sec) to complete the " Print PgmMsg" instruction = [(50 inst x 2 msec/inst)/byte + 10 msec/byte] x 80 bytes = (100 msec/byte + 10 msec/byte) x 80 bytes = (110 msec/byte) x 80 bytes = 8800 msec = 8.8 sec b) Time (in sec) to complete 200 instructions following "Print PgmMsg" = 200 inst x 2 msec/inst = 400 msec = 0.4 sec c) Total Time = (8.8 + 0.4) sec = 9.2 sec 2) What is the total idle time of the CPU? a) 10 msec/byte x 80 bytes = 800 msec = 0.80 sec b) (0.80 sec) / (9.2 sec) = 0.0869565 = 8.7% 2. Given the same CPU and an Interrupt-driven I/O controller that moves 100 bytes/sec. 1) How long will it take to execute the given program segment completely? a) For each byte in PgmMsg: |<--- Execute 50 instructions --->|<--- I/O Controller moves byte --->| |<--- 50 x 2 msec = 100 msec --->|<--- 10 msec --->| |<--- = 5 inst x 2 msec/inst --->| b) (200 inst) / (5 inst/byte) = 40 bytes: Hence, while the first 40 bytes of "PgmMsg" are moved to Output device (printed), all 200 instructions following the "Print PgmMsg" instruction are also completely executed. c) Total Time using the Interrupt-Driven I/O = (110 msec/byte) x 80 bytes = 8800 msec = 8.8 sec 2) What is the total idle time of the CPU? a) (40 bytes) x (10 msec/byte) = 400 msec = 0.40 sec b) 0.40/8.80 = 0.0454545 = 4.5% 3. Assume the same CPU and a DMA I/O Controller that moves 500 bytes/sec directly from/to Main Memory and for which the CPU has to execute 100 instructions for each I/O instruction. Note: 500 bytes/sec -> 2 msec/byte 1) How long will it take to execute the given program segment completely? a) (100 inst) x 2 msec = 200 msec = 0.20 sec b) (80 bytes) x (2 msec/byte) = 160 msec = 0.16 sec c) (160 msec) / (2 msec/inst) = 80 inst Thus, by the time period that the DMA moves the 80 bytes of "PgmMsg" to Output device, the CPU will have completed executing the first 80 of the 200 instructions following the "Print PgmMsg" instruction. Total Time using the DMA I/O Controller = (0.20 + 0.16) sec + (120 inst x 2 msec/inst) = 0.36 sec + 240 msec = 0.36 sec + 0.24 sec = 0.60 sec 2) What is the total idle time of the CPU? There is no CPU idle time. Example 2. Consider the following segment of a program: Print PgmMsg | . . . . | | . . . . | 200 non-I/O instructions that can be executed | . . . . | before the execution of the "Print PgmMsg" | . . . . | instruction is completed | . . . . | Assume: a) PgmMsg is 80 bytes long. b) CPU executes instructions at the rate of 1000 inst/sec. Note: 1000 inst/sec -> 1 msec/inst 1. Given that a Programmed I/O controller moves 200 bytes/sec and that the CPU has to execute 40 instructions to move a byte: Note: 200 bytes/sec -> 1/200 = 1000/200 msec -> 5 msec/byte 40 inst x 1 msec/inst = 40 msec 1) How long will it take to execute the given program segment completely? a) Time to complete the " Print PgmMsg" instruction = [(40 inst x 1 msec/inst)/byte + 5 msec/byte] x 80 bytes = (40 msec/byte + 5 msec/byte) x 80 bytes = (45 msec/byte) x 80 bytes = 3600 msec = 3.60 sec b) Time (in sec) to complete 200 instructions following "Print PgmMsg" = 200 inst x 1 msec/inst = 200 msec = 0.20 sec c) Total Time = (3.60 + 0.20) sec = 3.80 sec 2) What is the total idle time of the CPU? a) 5 msec/byte x 80 bytes = 400 msec = 0.40 sec b) (0.40 sec) / (3.80 sec) = 0.1052631 = 10.5% 2. Given the same CPU and an Interrupt-driven I/O controller that moves 200 bytes/sec, where CPU has to execute 40 instructions to move a byte. 1) How long will it take to execute the given program segment completely? a) For each byte in PgmMsg: |<--- Execute 40 instructions --->|<--- I/O Controller moves byte --->| |<--- 40 x 1 msec = 40 msec --->|<--- 5 msec --->| |<--- = 5 inst x 1 msec/inst --->| b) (200 inst) / (5 inst/byte) = 40 bytes: Hence, while the first 40 bytes of "PgmMsg" are moved to Output device (printed), all 200 instructions following the "Print PgmMsg" instruction are also completely executed. c) Total Time using the Interrupt-Driven I/O = (45 msec/byte) x 80 bytes = 3600 msec = 3.60 sec 2) What is the total idle time of the CPU? a) (40 bytes) x (5 msec/byte) = 200 msec = 0.20 sec b) 0.20/3.60 = 0.0555555 = 5.6% 3. Assume the same CPU and a DMA I/O Controller that moves 200 bytes/sec directly from/to Main Memory and for which the CPU has to execute 100 instructions for each I/O instruction. Note: 200 bytes/sec -> 1000/200 msec/byte = 5 msec/byte 1) How long will it take to execute the given program segment completely? a) (100 inst) x 1 msec = 100 msec = 0.10 sec <- by CPU b) (80 bytes) x (5 msec/byte) = 400 msec = 0.40 sec <- by I/O c) (200 inst) x (1 msec/inst) = 200 inst = 0.20 sec <- by CPU Thus, by the time that the DMA moves the 80 bytes of "PgmMsg" to Output device, the CPU will have completed executing all of the 200 instructions following the "Print PgmMsg" instruction. Total Time using the DMA I/O Controller = (0.10 + 0.40) sec = 0.50 msec 2) What is the total idle time of the CPU? a) 0.40 sec (processing 80 bytes by I/O) - 0.20 sec (executing 200 instructions by CPU) = 0.20 b) 0.20/0.40 = 50% Example 3. Consider the following segment of a program: Print PgmMsg | . . . . | | . . . . | 200 non-I/O instructions that can be executed | . . . . | before the execution of the "Print PgmMsg" | . . . . | instruction is completed | . . . . | Assume: a) PgmMsg is 100 bytes long. b) CPU executes instructions at the rate of 1000 inst/sec. Note: 1000 inst/sec -> 1 msec/inst 1. Given that a Programmed I/O controller moves 250 bytes/sec and that the CPU has to execute 20 instructions to move a byte: Note: 250 bytes/sec -> 1/250 = 1000/250 msec -> 4 msec/byte 20 inst x 1 msec/inst = 20 msec 1) How long will it take to execute the given program segment completely? a) Time to complete the " Print PgmMsg" instruction = [(20 inst x 1 msec/inst)/byte + 4 msec/byte] x 100 bytes = (20 msec/byte + 4 msec/byte) x 100 bytes = (24 msec/byte) x 100 bytes = 2400 msec = 2.40 sec b) Time (in sec) to complete 200 instructions following "Print PgmMsg" = 200 inst x 1 msec/inst = 200 msec = 0.20 sec c) Total Time = (2.40 + 0.20) sec = 2.60 sec 2) What is the total idle time of the CPU? a) 4 msec/byte x 100 bytes = 400 msec = 0.40 sec b) (0.40 sec) / (2.60 sec) = 0.1538461 = 15% 2. Given the same CPU and an Interrupt-driven I/O controller that moves 500 bytes/sec, where CPU has to execute 20 instructions to move a byte. 1) How long will it take to execute the given program segment completely? a) For each byte in PgmMsg: |<--- Execute 20 instructions --->|<--- I/O Controller moves byte --->| |<--- 20 x 1 msec = 20 msec --->|<--- 4 msec --->| |<--- = 4 inst x 1 msec/inst --->| b) (200 inst) / (4 inst/byte) = 50 bytes: Hence, while the first 50 bytes of "PgmMsg" are moved to Output device (printed), all 200 instructions following the "Print PgmMsg" instruction are also completely executed. c) Total Time using the Interrupt-Driven I/O = (24 msec/byte) x 100 bytes = 2400 msec = 2.40 sec 2) What is the total idle time of the CPU? a) (50 bytes) x (4 msec/byte) = 200 msec = 0.20 sec b) 0.20/2.40 = 0.0833333 = 8.3% 3. Assume the same CPU and a DMA I/O Controller that moves 250 bytes/sec directly from/to Main Memory and for which the CPU has to execute 120 instructions for each I/O instruction. Note: 250 bytes/sec -> 1000/250 msec/byte = 4 msec/byte 1) How long will it take to execute the given program segment completely? a) (120 inst) x 1 msec = 120 msec = 0.12 sec <- by CPU b) (100 bytes) x (4 msec/byte) = 400 msec = 0.40 sec <- by I/O c) (200 inst) x (1 msec/inst) = 200 inst = 0.20 sec <- by CPU Thus, by the time that the DMA moves the 100 bytes of "PgmMsg" to Output device, the CPU will have completed executing all of the 200 instructions following the "Print PgmMsg" instruction. Total Time using the DMA I/O Controller = (0.12 + 0.40) sec = 0.52 sec 2) What is the total idle time of the CPU? a) 0.40 sec (processing 80 bytes by I/O) - 0.20 sec (executing 200 instructions by CPU) = 0.20 b) 0.20/0.52 = 0.3846153 = 38%