Download 7.1. Computer System Structures

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
Transcript
Operating System Principles
AUA CIS331
Albert Minasyan
Computer System Structures
Handout Content:
I/O structure
o I/O hardware
 Isolated I/O ports
 Memory mapped I/O
o I/O software
 Polling, Poll handshaking
 Interrupt
o DMA
o Blocking, Non-Blocking I/O
o Buffering
Textbook Silberschatz, Chapter 2
1
Operating System Principles
AUA CIS331
Albert Minasyan
I/O Structure
A modern, general-purpose computer system consists of a CPU and a number of device controllers
that are connected through a common bus that provides access to shared memory. Each device
controller is in charge of a specific type of device (for example, disk drives, audio devices, and video
displays). Depending on the controller, there may be more than one attached device. For instance, the
small computer-systems interface (SCSI) controller can have seven or more devices attached to it.
A device controller maintains some local buffer storage and a set of special-purpose registers. The
device controller is responsible for moving the data between the peripheral devices that it controls and
its local buffer storage.
The CPU and the device controllers can execute concurrently, competing for memory cycles. To
ensure orderly access to the shared memory, a memory controller is provided whose function is to
synchronize access to the memory.
Typically, operating systems have a device driver for each device controller. This device driver
understands the device controller and presents a uniform interface to the device to the rest of the
operating system.
Memory Controller
General purpose computer system
•
A modern, general-purpose computer system
consists of
• CPU
• number of device controllers
They are connected through a common bus
BUS also provides access to shared memory
•
•
•
•
•
•
The device controller is responsible for
moving the data between the peripheral
devices that it controls and the BUS.
Each device controller is in charge of a
specific type of device
• Many same type of devices can be
connected through the same
controller (SCSI, USB)
The CPU and the device controllers can communicate directly and exchange information
• Between Device Controller and CPU registers.
The CPU and the device controllers2.2.1.
can execute
concurrently, competing for memory
I/O Hardware
access (Direct Memory Access to Memory by Device Controller).
• Memory controller’s function is to synchronize access to the memory
2
Operating System Principles
AUA CIS331
Albert Minasyan
Memory-mapped I/O and isolated I/O are two complementary methods of performing input/output
between the CPU and peripheral devices in a computer.
An alternative approach is using dedicated I/O processors — commonly known as channels on mainframe
computers — that execute their own instructions.
Despite the incredible variety of I/O devices we need only a few concepts to understand
how the devices are attached and how the software can control the hardware.
There are two ways of information exchange between the host and I/O devices:
• Through the Isolated I/O ports
• Through the Memory Mapped I/O Ports
The information exchange could be done using one of below methods by the software:
• Polling or programmed I/O
• Interrupts
How can the processor give commands and data to a controller to accomplish an I/O transfer? The
short answer is that the controller has one or more registers for data and control signals. The processor
communicates with the controller by reading and writing bit patterns in these registers. One way in
which this communication can occur is through the use of special I/O instructions that specify the
transfer of a byte or word to an I/O port address. The I/O instruction triggers bus lines to select the
proper device and to move bits into or out of a device register.
Information exchange through the I/O ports
How can the processor give commands and data to a controller to accomplish an I/O transfer ?
•
•
•
The controller has one or more registers for data and control signals.
These registers (we call them I/O ports) could be accessed by unique addresses – I/O port
addresses.
The processor communicates with the controller by reading and writing bit patterns in these
registers
CPU
Bus
I/O ports
378-37F
I/O ports
320-32F
HDD
controller
Printer
controller
I/O ports
060-064
Keyboard
controller
An I/O port typically consists of four registers, called the (1) status, (2) control, (3) data-in, and (4)
data-out registers.


The Data-Input Register is read by the host to get input.
The Data-Output Register is written by the host to send output.
3
Operating System Principles
AUA CIS331


Albert Minasyan
The Status Register contains bits that can be read by the host. These bits indicate states, such
as whether the current command has completed, whether a byte is available to be read from
the data-in register, and whether a device error has occurred.
The Control Register can be written by the host to start a command or to change the mode of
a device. For instance, a certain bit in the control register of a serial port chooses between
full-duplex and half-duplex communication, another bit enables parity checking, a third bit
sets the word length to 7 or 8 bits, and other bits select one of the speeds supported by the
serial port.
The data registers are typically 1 to 4 bytes in size. Some controllers have FIFO chips that can hold
several bytes of input or output data to expand the capacity of the controller beyond the size of the
data register. A FIFO chip can hold a small burst of data until the device or host is able to receive those
data.
An I/O port typically consists of 4 registers, called the
(1) status,
(2) control,
(3) data-in
(4) data-out
The Data-Input Register is read by the host to get the input data.
f.e. the pressed key’s value from the keyboard
The Data-Output Register is written by the host to send output data. (f.e. LED’s value of
keyboard – NumLock, ScrollLock, CapsLock.)
The Status Register contains bits that can be read by the host. These bits indicate states,
such as
 whether the current command has completed,
 whether a byte is available to be read from the data-in register,
 and whether a device error has occurred.
The Control Register can be written by the host
 to start a command or
 to change the mode of a device
o one of the bits could define the full-duplex or half-duplex communication
o another bit parity checking
o a third bit sets the word length to 7 or 8 bits
o other bits select one of the speeds supported by the serial port
Status
Register(s)
Control
Register(s)
Data Input
Register(s)
Data Output
Register(s)
Some Logic
Keyboard Controller
4
Operating System Principles
AUA CIS331
Albert Minasyan
Isolated I/O ports
One way in which this communication can occur is through the use of special I/O instructions
in
al, 64h ;Read keyboard status register from 0x64 port.
out 60h, al ;send the command to keyboard to port 0x60
In this case we use the Isolated I/O ports.
The below Figure shows the general map of Isolated I/O port addresses for PCs.
Alternatively, the device controller can support memory mapped I/O. In this case, the devicecontroller registers are mapped into the address space of the processor. The CPU executes I/O requests
using the standard data-transfer instructions to read and write the device-controller registers.
The graphics controller has I/O ports for basic control operations, but the controller has a large
memory mapped region to hold screen contents. The process sends output to the screen by writing
data into the memory-mapped region. The controller generates the screen image based on the contents
of this memory. This technique is simple and convenient to use. Moreover, writing millions of bytes to
the graphics memory is faster than issuing millions of I/O instructions. But the ease of writing to a
memory-mapped I/O controller is offset by a disadvantage. Because a common type of software fault
is a write through an incorrect pointer to an unintended region of memory, a memory-mapped device
register is vulnerable to accidental modification. Of course, protected memory helps to reduce this
risk.
Some systems use both techniques. For instance, PCs use I/O instructions to control some devices and
memory-mapped I/O to control others.
5
Operating System Principles
AUA CIS331
Albert Minasyan
Memory Mapped Input / Output


The device-controller registers are mapped into the address space of the processor.
The CPU executes I/O requests using the standard data-transfer (memory access) instructions
to read and write the device-controller registers.

The graphics controller has I/O ports for basic control operations, but the controller has a large
memory mapped region to hold screen contents.
The controller generates the screen image based on the contents of this memory.

RAM
FFFF FFFF
E000 0000
Write patterns
FF- white pixel
00 - black pixel
CPU
Video RAM
buffer
FF- white pixel, 00 - black pixel
Video
0000 0000
Signals
Continuously Retrieve
Controller
patterns and
Transform to Signals
Separate Video RAM.
•
•
•
•
•
The address space is the same (main RAM’s address space
But physically the Video RAM is placed in the Video Controller
Does not take any amount of memory from the existing RAM
Not Separated VRAM – You have 2GB memory – 0.5 GB is lost for VRAM
Separated VRAM – You have 2GB memory + 0.5 GB VRAM
Most forms of video RAM are dual-ported, which means that
• while the processor is writing a new image to video RAM
• the display is reading from video RAM to refresh its current display content.
6
Operating System Principles
AUA CIS331
Albert Minasyan
Advantage: The Memory Mapped I/O technique is simple and convenient to use.
Moreover, writing millions of bytes to the graphics memory is faster than issuing millions of
I/O instructions.
Disadvantage: Because a common type of software fault is a write through an incorrect pointer
to an unintended region of memory, a memory-mapped device register is vulnerable to
accidental modification. Of course, protected memory helps to reduce this risk.
Video RAM (VRAM) means in general all forms of random access memory (RAM) used to store
image data for a computer display. All types of video RAM are special arrangements of dynamic
RAM (DRAM). Video RAM is really a buffer between the computer processor and the display and is
often called the frame buffer.
When images are to be sent to the display, they are first read by the processor as data from some form
of main (non-video) RAM and then written to video RAM.
From video RAM (the frame buffer), the data is converted by a RAM digital-to-analog converter
(RAMDAC) into analog signals that are sent to the display presentation mechanism such as a cathode
ray tube (CRT).
Most forms of video RAM are dual-ported, which means that
 while the processor is writing a new image to video RAM
 the display is reading from video RAM to refresh its current display content.
The dual-port design is the main difference between main RAM and video RAM.
7
Operating System Principles
AUA CIS331
Albert Minasyan
I/O handling by the software
Polling or Programmed I/O
The complete protocol for interaction between the host and a controller can be intricate, but the basic
handshaking notion is simple. We explain handshaking with an example. Assume that 2 bits are used
to coordinate the producer-consumer relationship between the controller and the host. The controller
indicates its state through the busy bit in the status register. (Recall that to set a bit means to write a 1
into the bit and to clear a bit means to write a 0 into it.) The controller sets the busy bit when it is
busy working and clears the busy bit when it is ready to accept the next command.
The host signals its wishes via the command-ready bit in the command register. The host sets the
command-ready bit when a command is available for the controller to execute. For this example, the
host writes output through a port, coordinating with the controller by handshaking as follows.
1.
2.
3.
4.
5.
6.
7.
The host repeatedly reads the busy bit until that bit becomes clear.
The host sets the write bit in the command register and writes a byte into the data-out register.
The host sets the command-ready bit.
When the controller notices that the command-ready bit is set, it sets the busy bit.
The controller reads the command register and sees the write command.
It reads the data-out register to get the byte and does the I/O to the device.
The controller clears the command-ready bit, clears the error bit in the status register to indicate
that the device I/O succeeded, and clears the busy bit to indicate that it is finished.
This loop is repeated for each byte.
Status
Control
Busy bit
Write bit,
Command
ready bit
Data Intput
Register(s)
Data Output
Byte
Some Logic
Keyboard Controller
Polling or Programmed I/O
Advantage: This technique is straightforward and simple and could be used when there is no
performance requirements or I/O devices are very fast and not too many.
Disadvantage: CPU waits the I/O device in a loop and polling becomes inefficient when it is
attempted repeatedly yet rarely finds a device to be ready for service, while other useful CPU
processing remains undone.
Alternative: In such instances, it may be more efficient to arrange for the hardware controller to
notify the CPU when the device becomes ready for service, rather than to require the CPU to
poll repeatedly for an I/O completion. The hardware mechanism that enables a device to notify
the CPU is called an interrupt.
8
Operating System Principles
AUA CIS331
Albert Minasyan
In step 1, the host is busy-waiting or polling: it is in a loop, reading the status register over and over
until the busy bit becomes clear. If the controller and device are fast, this method is a reasonable one.
But if the wait may be long, the host should probably switch to another task. How, then, does the host
know when the controller has become idle? For some devices, the host must service the device
quickly, or data will be lost. For instance, when data are streaming in on a serial port or from a
keyboard, the small buffer on the controller will overflow and data will be lost if the host waits too
long before returning to read the bytes.
In many computer architectures, three CPU-instruction cycles are sufficient to poll a device:
• read a device register,
• logical-and to extract a status bit,
• and branch if not zero.
Clearly, the basic polling operation is efficient.
But polling becomes inefficient when it is attempted repeatedly yet rarely finds a device to be ready
for service, while other useful CPU processing remains undone. In such instances, it may be more
efficient to arrange for the hardware controller to notify the CPU when the device becomes ready for
service, rather than to require the CPU to poll repeatedly for an I/O completion. The hardware
mechanism that enables a device to notify the CPU is called an interrupt.
Poll Handshaking
9
Operating System Principles
AUA CIS331
Albert Minasyan
I/O Interrupts
If the CPU uses polling to watch the control bit, constantly looping to see whether the device is ready,
this method of operation is called programmed I/O (PIO). If the CPU does not poll the control bit,
but instead receives an interrupt when the device is ready for the next byte, the data transfer is said to
be interrupt driven.
The basic interrupt mechanism works as follows





The CPU hardware has a wire called the Interrupt-Request Line
CPU senses that line after executing every instruction.
The device controller raises an interrupt by asserting a signal on the interrupt request line
The CPU catches the interrupt and dispatches it to the interrupt handler
And the handler clears the interrupt by servicing the device.
CPU
Bus
I/O ports
378-37F
I/O ports
320-32F
Interrupt
request
HDD
controller
Printer
controller
I/O ports
060-064
Keyboard
controller
When the CPU detects that a controller has asserted a signal on the line, the CPU performs a state save
and jumps to the Interrupt-Handler Routine at a fixed address in memory. The interrupt handler
determines the cause of the interrupt, performs the necessary processing, performs a state restore, and
executes a return from interrupt instruction to return the CPU to the execution state prior to the interrupt.
We say that the device controller raises an interrupt by asserting a signal on the interrupt request line,
the CPU catches the interrupt and dispatches it to the interrupt handler, and the handler clears the
interrupt by servicing the device.
10
Operating System Principles
AUA CIS331
Albert Minasyan
The Figure below summarizes the interrupt-driven I/O cycle.
1.
User process in CPU
requests I/O operation
for I/O device
2. I/O device starts
I/O operation
4. CPU interrupts and
processes the I/O result.
3. I/O device finished I/O
operation. Interrupt
request is sent to CPU
Interrupt time line for a single process doing output.
11
Operating System Principles
AUA CIS331
Albert Minasyan
The Efficiency of an Interrupt Driven System
Interrupts introduce a considerable amount of complexity to a software system. One might ask if
using interrupts is really worth the trouble. The answer of course, is yes. Why else would people
use interrupts if they were proven not to be worthwhile? However, interrupts are like many other
nifty things in computer science – they have their place; if you attempt to use interrupts in an
inappropriate fashion they will only make things worse for you.
The following sections explore the efficiency aspects of using interrupts. As you will soon
discover, an interrupt driven system is usually superior despite the complexity.
However, this is not always the case. For many systems, alternative methods provide better
performance.
Interrupt Driven I/O vs. Polling
The whole purpose of an interrupt driven system is to allow the CPU to continue
processing instructions while some I/O activity occurs. This is in direct contrast to a polling
system where the CPU continually tests an I/O device to see if the I/O operation is complete. In
an interrupt driven system, the CPU goes about its business and the I/O device interrupts it when
it needs servicing. This is generally much more efficient than wasting CPU cycles polling a device
while it is not ready.
The serial port is a perfect example of a device that works extremely well with interrupt driven I/O.
You can start a communication program that begins downloading a file over a modem. Each time
a character arrives, it generates an interrupt and the communication program starts up, buffers
the character, and then returns from the interrupt. In the meantime, another program (like a word
processor) can be running with almost no performance degradation since it takes so little time to
process the serial port interrupts.
Contrast the above scenario with one where the serial communication program continually polls
the serial communication chip to see if a character has arrived. In this case the CPU spends all of
its time looking for an input character even though one rarely (in CPU terms) arrives. Therefore,
no CPU cycles are left over to do other processing like running your word processor.
Suppose interrupts were not available and you wanted to allow background downloads while
using your word processing program. Your word processing program would have to test the input
data on the serial port once every few milliseconds to keep from losing any data. Can you
imagine how difficult such a word processor would be to write? An interrupt system is the clear
choice in this case.
If downloading data while word processing seems far fetched, consider a more simple case – the
PC’s keyboard. Whenever a keypress interrupt occurs, the keyboard ISR reads the key pressed
and saves it in the system type ahead buffer for the moment when the application wants to read
the keyboard data. Can you imagine how difficult it would be to write applications if you had to
constantly poll the keyboard port yourself to keep from losing characters? Even in the middle of a
long calculation? Once again, interrupts provide an easy solution
12
Operating System Principles
AUA CIS331
Albert Minasyan
Direct Memory Access (DMA)
Above forms of Programmed I/O (PIO) or Interrupt-driven I/O are fine for moving small amounts of
data but can produce high overhead when used for bulk data movement such as disk I/O.
Many computers avoid burdening the main CPU with PIO by offloading some of this work to a
special-purpose processor called a Direct-Memory-Access (DMA) Controller.
The system bus consists of 3 main groups of lines:
• Address Bus: Transfers the memory address or the device I/O port address
• Data Bus: Transfers the data between CPU registers, Memory and Device Controllers’
Registers. The data is accompanied with the Address.
• Control Bus: Signals on these lines show the availability or the direction of sending the Data
or the Address. Many lines show the Availability or Absent of some events in the system.
To initiate a DMA transfer, the host writes a DMA command block into memory. This block
contains a pointer to the source of a transfer, a pointer to the destination of the transfer, and a count
of the number of bytes to be transferred. The CPU writes the address of this command block to the
DMA controller, then goes on with other work. The DMA controller proceeds to operate the
memory bus directly, placing addresses on the bus to perform transfers without the help of the main
CPU.
When the entire transfer is finished, the DMA controller interrupts the CPU.
When the DMA controller seizes the memory bus, the CPU is momentarily prevented from accessing
main memory although it can still access data items in its primary and secondary caches.
Although this Cycle-Stealing can slow down the CPU computation, offloading the data-transfer work
to a DMA controller generally improves the total system performance. Some computer architectures
use physical memory addresses for DMA, but others perform Direct Virtual Memory Access (DVMA)
using virtual addresses that undergo translation to physical addresses. DVMA can perform a transfer
between two memory-mapped devices without the intervention of the CPU or the use of main
memory.
DMA operations can be performed in either burst or single-cycle mode. Some DMA controllers
support both.
In burst mode, the DMA controller keeps control of the bus until all the data buffered by the
requesting device has been transferred to memory (or when the output device buffer is full, if writing
to a peripheral).
In single-cycle mode, the DMA controller gives up the bus after each transfer. This minimizes the
amount of time that the DMA controller keeps the processor off of the memory bus, but it requires that
13
Operating System Principles
AUA CIS331
Albert Minasyan
the bus request/acknowledge sequence be performed for every transfer. This overhead can result in a
drop in overall system throughput if a lot of data needs to be transferred.






Programmed I/O (PIO) or Interrupt-driven I/O are fine for moving small amounts of data but
can produce high overhead when used for bulk data movement such as disk I/O.
Many computers avoid burdening the main CPU with PIO by offloading some of this work to
a special-purpose processor called a Direct-Memory-Access (DMA) Controller.
To initiate a DMA transfer, the host writes a DMA command block into memory. This block
contains:
o a pointer to the source of a transfer
o a pointer to the destination of the transfer
o a count of the number of bytes to be transferred.
The CPU writes the address of this command block to the DMA controller, then goes on
with other work.
The DMA controller proceeds to operate the memory bus directly to perform transfers
without the help of the main CPU.
o When the DMA controller seizes the memory bus, the CPU is momentarily
prevented from accessing main memory although it can still access data items in its
primary and secondary caches.
When the entire transfer is finished, the DMA controller interrupts the CPU.
DMA Advantage: CPU does not take care of data transfer.
DMA Problems: Hardware implementation of DMA is needed.
DMA modes
In burst mode, the DMA controller keeps control of the bus until all the data buffered by the
requesting device has been transferred to memory (or when the output device buffer is full, if
writing to a peripheral).
In single-cycle mode, the DMA controller gives up the bus after each transfer.
Advantages, Disadvantages of Burst and Single-Cycle modes
Burst mode: CPU is isolated from the bus longer time but large data could be transferred fast.
Single-Cycle mode: CPU is not isolated too much from the bus but large data transfer causes
the overhead of bus requests.
14
Operating System Principles
AUA CIS331
Albert Minasyan
Blocking and Nonblocking, Synchronous and Asynchronous I/O
To start an I/O operation, the CPU loads the appropriate registers within the device controller. The
device controller, in turn, examines the contents of these registers to determine what action to take.
For example, if it finds a read request, the controller will start the transfer of data from the device to
its local buffer. Once the transfer of data is complete, the device controller informs the CPU that it has
finished its operation. It accomplishes this communication by triggering an interrupt.
This situation will occur, in general, as the result of a user process requesting I/O. Once the I/O is
started, several courses of action are possible.
Blocking or Synchronous I/O
When an application issues a Blocking system
call, the execution of the application is
suspended.
It resumes execution when receives the values
returned by the system call.
Disadvantage: As the physical actions
performed by I/O devices are generally
asynchronous-the I/O takes unpredictable
amount of time.
Advantage: Nevertheless, most operating
systems use blocking system calls for the
application interface, because blocking
application code is easier to understand than
nonblocking application code.
Non-Blocking I/O,
Asynchronous I/O
A nonblocking call does not halt the execution
of the application for an extended time. Instead,
it returns quickly, with a return value that
indicates how many bytes were transferred.
An asynchronous call returns immediately,
without waiting for the I/O to complete. The
application continues to execute its code. The
completion of the I/O at some future time is
communicated to the application.
The difference between nonblocking and
asynchronous system calls is that a
nonblocking read() returns immediately with
whatever data are available-the full number of
bytes requested, fewer, or none at all. An
asynchronous read () call requests a transfer that
will be performed in its entirety but will
complete at some future time.
Disadvantage: Complexity of implementation.
Advantage: The program initiated I/O runs in parallel with the I/O
process.
15
Operating System Principles
AUA CIS331
Albert Minasyan
When an application issues a Blocking system call, the execution of the application is suspended. The
application is moved from the operating system's run queue to a wait queue. After the system call
completes, the application is moved back to the run queue, where it is eligible to resume execution.
When it resumes execution, it will receive the values returned by the system call.
The physical actions performed by I/O devices are generally asynchronous-they take a varying or
unpredictable amount of time. Nevertheless, most operating systems use blocking system calls for
the application interface, because blocking application code is easier to understand than
nonblocking application code.
Some user-level processes need NonBlocking I/O. One example is a user interface that receives
keyboard and mouse input while processing and displaying data on the screen. Another example is a
video application that reads frames from a file on disk while simultaneously decompressing and
displaying the output on the display.
One way an application writer can overlap execution with I/O is to write a multithreaded application.
Some threads can perform blocking system calls, while others continue executing.
Some operating systems provide nonblocking I/O system calls. A nonblocking call does not halt the
execution of the application for an extended time. Instead, it returns quickly, with a return value
that indicates how many bytes were transferred.
An alternative to a nonblocking system call is an asynchronous system call. An asynchronous call
returns immediately, without waiting for the I/O to complete. The application continues to execute
its code. The completion of the I/O at some future time is communicated to the application, either
through the setting of some variable in the address space of the application or through the triggering
of a signal or software interrupt or a call-back routine that is executed outside the linear control
flow of the application.
The difference between nonblocking and asynchronous system calls is that a nonblocking read()
returns immediately with whatever data are available-the full number of bytes requested, fewer, or
none at all. An asynchronous read () call requests a transfer that will be performed in its entirety but
will complete at some future time.
Interrupt before or after the Data Transfer.
Interrupt before Data Transfer is started
• The key is pressed on keyboard
• Interrupt is requested by keyboard controller to notify the OS that the key value is ready to be
taken from the keyboard.
• Interrupt is serviced by OS (application)
• Key value is taken from the Keyboard Controller Port (Data Transfer)
Interrupt after Data Transfer is finished
• The application requests data to read (write) to the HDD
• I/O is started and the information flow to the destination is completed
• Interrupt request is sent from HDD controller to notify about the completion the CPU.
• The Interrupt is serviced by OS (Application).
• Application knows that the data is transferred (arrived or is sent) and could take the
appropriate action.
16
Operating System Principles
AUA CIS331
Albert Minasyan
17
Operating System Principles
AUA CIS331
Albert Minasyan
Buffering
One way in which the I/O system improves the efficiency of the computer is by using storage space in
main memory or on disk via techniques called buffering.
A Buffer is a memory area that stores data being transferred between two devices or between a device
and an application.
Buffering is done for three reasons.
One reason is to cope with a speed mismatch between the producer and consumer of a data stream.
Suppose, for example, that a file is being received via modem for storage on the hard disk. The
modem is about a thousand times slower than the hard disk. So a buffer is created in main memory to
accumulate the bytes received from the modem. When an entire buffer of data has arrived, the buffer
can be written to disk in a single operation. Since the disk write is not instantaneous and the modem
still needs a place to store additional incoming data, two buffers are used. After the modem fills the
first buffer, the disk write is requested. The modem then starts to fill the second buffer while the first
buffer is written to disk By the time the modem has filled the second buffer, the disk write from the
first one should have completed, so the modem can switch back to the first buffer while the disk writes
the second one. This Double Buffering decouples the producer of data from the consumer, thus
relaxing timing requirements between them.
A second use of buffering is to provide adaptations for devices that have different data-transfer
sizes. Such disparities are especially common in computer networking, where buffers are used widely
for fragmentation and reassembly of messages. At the sending side, a large message is fragmented into
small network packets. The packets are sent over the network, and the receiving side places them in a
reassembly buffer to form an image of the source data.
A third use of buffering is to support copy semantics for application I/O. An example will clarify the
meaning of "copy semantics." Suppose that an application has a buffer of data that it wishes to write to
disk. It calls the write() system call providing a pointer to the buffer and an integer specifying the
number of bytes to write. After the system call returns, what happens if the application changes the
contents of the buffer? With the Copy Semantics the version of the data written to disk is guaranteed
to be the version at the time of the application system call, independent of any subsequent changes in
the application's buffer. A simple way in which the operating system can guarantee copy semantics is
for the write () system call to copy the application data into a kernel buffer before returning control to
the application. The disk write is performed from the kernel buffer, so that subsequent changes to the
application buffer have no effect.
One way in which the I/O system improves the efficiency of the computer is by using
storage space in main memory or on disk via techniques called buffering.
A Buffer is a memory area that stores data being transferred between two devices or between
a device and an application.
Buffering is done for three reasons:
 To cope with a speed mismatch between the producer and consumer of a data
stream.
 To provide adaptations for devices that have different data-transfer sizes.
 To support copy semantics for application I/O.
18
Operating System Principles
AUA CIS331
Albert Minasyan
Speed Mismatch
Slow
transfer
Modem
Fast transfer
Buffer
Hard
Disk
Buffer 2
For Double Buffering
Different data-transfer sizes
Large file to transfer
Buffer
Network Card
Large file after transfer
Small packets
Network Card
Buffer
Support Copy Semantics
Do not write the data
on disk from the
App. Buffer
Application
Buffer
But make a
Copy of the data
before the
writing on disk.
Kernel
Buffer
Hard
Disk
19
Operating System Principles
AUA CIS331
Albert Minasyan
Using the tools.
Windows: Device Manager - to look at I/O devices, I/O ports and Interruption Number
Memory
mapped
I/O ports
Interrupt
number
I/O ports
20