Lecture 08
Lecture 08
Lecture topic
Input/Output Organization
Michael Kerrisk, The LINUX Programming Interface, 1st Edition, No Starch Press ISBN-13 978-
1593272203
• Memory-Mapped I/O
• Interrupts
• Handling Multiple Devices
• Controlling Device Requests
• Input and Output: File System Information
• Shared Library
Overview
3
Processor Memory
Bus
A single-bus structure.
Memory-Mapped I/O
5
When I/O devices and the memory share the same address
space, the arrangement is called memory-mapped I/O.
Any machine instruction that can access memory can be used
to transfer data to or from an I/O device.
Move DATAIN, R0
Move R0, DATAOUT
Some processors have special In and Out instructions to
perform I/O transfer.
Interface
6
Three Major Mechanisms
7
I/O devices operate at speeds that are very much different from
that of the processor.
Keyboard, for example, is very slow.
Since the interrupt request can come at any time, it may alter
the sequence of events from that envisaged by the
programmer.
The interrupt request signal will be active until it learns that the
processor has responded to its request. This must be handled to
avoid successive interruptions.
Let the interrupt be disabled/enabled in the interrupt-service
routine.
Let the processor automatically disable interrupts before
starting the execution of the interrupt-service routine.
Handling Multiple Devices
12
Mainprogram
MOVE.L #LINE,PNTR Initializebufferpointer.
CLR EOL Clearend-of-lineindicator.
ORI.B #4,CONTR OL Set bit KEN.
MOVE #$100,SR Setprocessorpriority to1.
..
.
Interrupt-service
routine
READ MOVEM.L A0/D0,– (A7) Save registersA0,D0 onstack.
MOVEA.L PNTR,A0 Loadaddresspointer.
MOVE.B DATAIN,D0 Get input character.
MOVE.B D0,(A0)+ Storeit in memorybuffer.
MOVE.L A0,PNTR Updatepointer.
CMPI.B #$0D,D0 Check if CarriageReturn.
BNE RTRN
MOVE #1,EOL Indicateend ofline.
ANDI.B #$FB,CONTR OL Clearbit KEN.
RTRN MOVEM.L (A7)+,A0/D0 RestoreregistersD0, A0.
RTE
Direct Memory Access
21
The DMA controller provides the memory address and all the
bus signals needed for data transfer, increment the memory
address for successive words, and keep track of the number of
transfers.
DMA Procedure
22
I/O bus
Expandable
configuration
File system
31
block-oriented:
Stores information in blocks that are usually of fixed size, and
stream-oriented
Transfers data in and out as a stream of bytes, with no block
structure
Eg: Terminals, printers, communications ports, mouse
Single Buffer (Block-oriented data)
34
line-at-a-time fashion:
user input is one line at a time, with a carriage return signalling
Printer
byte-at-a-time fashion
used on forms-mode terminals when each key stroke is
significant
user process follows the producer/consumer model
Double Buffer or buffer swapping
36
A process now transfers data to (or from) one buffer while the operating system
empties (or fills) the other. This technique is known as double buffering
stream-oriented input:
line-at-a-time I/O the user process need not be suspended for input or output,
unless the process runs ahead of the double buffers
byte-at-a-time operation no particular advantage over a single buffer
Circular Buffer
37
42
using System;
using System.IO;
public sealed class Program
{
public static void Main()
{
DirectoryInfo dInfo = new DirectoryInfo(@"C:\Program Files\");
DirectoryInfo[] dirs = dInfo.GetDirectories("*", SearchOption.TopDirectoryOnly);
Console.WriteLine("{0} subdirectories:", dInfo.FullName);
foreach (DirectoryInfo subDir in dirs)
{
Console.WriteLine(" {0}", subDir.Name);
}
using System;
using System.IO;
public sealed class Program
{
public static void Main()
{
DirectoryInfo theDir = new DirectoryInfo(@"C:\Windows\");
Console.WriteLine("Directory: {0}", theDir.FullName);
foreach (FileInfo file in theDir.GetFiles())
{
Console.WriteLine("File: {0}", file.Name);
}
}
}
Determine what drives are available
45
using System;
using System.IO;
public class Program
{
public static void Main()
{
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
{
Console.WriteLine("Drive: {0}", drive.Name);
Console.WriteLine("Type: {0}", drive.DriveType);
}
}
}
Reading and Writing Files: Understanding Streams
46
A stream is a bidirectional pipe connecting a source and a destination and as such, serves
as a common way to deal with both sequential and random access to data within the .NET
Framework.
In the .NET Framework, streams begin with an abstract base class that provides the basic
interface and implementation for all streams in the Framework.
Each stream has a single data source, representing a backing store of bytes that can
read from or written to. This is why streams have a common base class: working with
data as a flow is a common way to work with data.
using System;
using System.IO;
public sealed class Program
{
public static void Main()
{
using (Stream s = new FileStream(@"d:\file.txt", FileMode.Open))
{
int read;
while ((read = s.ReadByte()) != -1)
{
Console.Write("{0} ", read);
}
}
}
}
The FileStream Class
48
using System;
using System.IO;
The FileStream class provides the basic
public sealed class Program functionality to open file streams for reading and
{ writing.
public static void Main()
{
using (Stream s = new FileStream(@"d:\file.txt", FileMode.Open))
{
s.Seek(8, SeekOrigin.Current);
Console.WriteLine(s.ReadByte());
s.Seek(0, SeekOrigin.Begin);
Console.WriteLine(s.ReadByte());
s.Seek(-1, SeekOrigin.End);
Console.WriteLine(s.ReadByte());
}
}
}
The StreamReader Class
49
The StreamReader class provides the basic functionality to write data from a derived
class. Here is an example of writing data, copying one stream to another
using System;
using System.IO;
public sealed class Program {
public static void Main() {
using (Stream from = new FileStream(@"d:\file1.txt", FileMode.Open))
using (Stream to = new FileStream(@"d:\file2.txt", FileMode.OpenOrCreate)) {
int readCount;
byte[] buffer = new byte[1024];
while ((readCount = from.Read(buffer, 0, 1024)) != 0)
{
to.Write(buffer, 0, readCount);
}
}
}
}
Return block number
50
using System;
using System.IO;
public sealed class Program
{
public static void Main()
{
using (Stream s = new FileStream(@"d:\file1.txt", FileMode.Open))
{
int read;
while ((read = s.ReadByte()) != -1)
{
Console.Write("{0} ", read);
}
}
}
}
Class Activity
51
Return the names of the files you created and put in your first and last
name 10 times in text1.txt and transfer text1.txt information to
text2.txt and print the size of each block.
Returns File content
52
using System;
using System.IO;
public sealed class Program {
public static void Main()
{
using (Stream s = new FileStream(@"d:\file1.txt",
FileMode.Open))
{
int read;
while ((read = s.ReadByte()) != -1)
{
Console.Write("{0} ", (char)read);
}
}
}
}
Understanding Readers and Writers
53
Here are two general readers and writers in the .NET Framework: text and binary.
StreamReader and StreamWriter are classes that enable you to write to and read from
streams. This is the purpose of the reader and writer classes.
using System;
using System.IO;
using System.Text;
public sealed class Program {
public static void Main() {
string s = @"Hi there
this is a multiline
text string";
StringReader sr = new StringReader(s);
while (sr.Peek() != -1) {
string line = sr.ReadLine();
Console.Write(line);
}
}
}
The BinaryReader and BinaryWriter
54
using System;
using System.IO;
public sealed class Program
{
public static void Main()
{
FileStream fs = File.Create(@"d:\file1.txt");
BinaryWriter writer = new BinaryWriter(fs);
long number = 100;
byte[] bytes = new byte[] { 10, 20, 50, 100 };
string s = "is it practical?";
writer.Write(number);
writer.Write(bytes);
writer.Write(s);
writer.Close();
}
}
Compressing Streams
55
using System;
using System.IO;
using System.IO.Compression;
public sealed class Program {
public static void Main() {
FileStream sfile = File.OpenRead(@"d:\comp.txt");
FileStream dfile = File.Create(@"d:\comp.txt.gz");
GZipStream cStream = new GZipStream(dfile, CompressionMode.Compress);
int theByte = sfile.ReadByte();
#include <math.h>
main() {
printf("exponential value %d\n", exp(-1));
}
Linking Shared Libraries
60
Generally both the code and the data addresses for each library
are explicitly defined, with the data area starting on a page
boundary a page or two after the end of the code.
This makes it possible to create minor version updates, because
updates frequently do not change the data layout, but just add or
change code.
Techniques for Minor Updates
62
68
Topics
Lecture 06
Lecture 07
Lecture 08
Michael Kerrisk, The LINUX Programming Interface, 1st Edition, No Starch Press
ISBN-13 978-1593272203