0% found this document useful (0 votes)
9 views

Lecture 08

Uploaded by

Sen Hou
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lecture 08

Uploaded by

Sen Hou
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 69

SYSTEMS PROGRAMMING

Lecture topic

Input/Output Organization

Michael Kerrisk, The LINUX Programming Interface, 1st Edition, No Starch Press ISBN-13 978-
1593272203

Lecture 8 Dr. Mansour Esmaeilpour July 11th, 2024


Agenda
2

• Memory-Mapped I/O
• Interrupts
• Handling Multiple Devices
• Controlling Device Requests
• Input and Output: File System Information
• Shared Library
Overview
3

 Computer has ability to exchange data with other devices.


 Human-computer communication
 Computer-computer communication
 Computer-device communication
Single Bus
4

Processor Memory

Bus

I/O device 1 I/O device n

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

 Program-controlled I/O – processor polls the device.


 Interrupt
 Direct Memory Access (DMA)
Program-Controlled I/O
8

 I/O devices operate at speeds that are very much different from
that of the processor.
 Keyboard, for example, is very slow.

 It needs to make sure that only after a character is available in


the input buffer of the keyboard interface; also, this character
must be read only once.
Interrupts
9

In program-controlled I/O, the program enters a wait loop in


which it repeatedly tests the device status.

During the period, the processor is not performing any useful


computation.

However, in many situations other tasks can be performed while


waiting for an I/O device to become ready.

Let the device alert the processor.


Enabling and Disabling Interrupts
10

 Since the interrupt request can come at any time, it may alter
the sequence of events from that envisaged by the
programmer.

 Interrupts must be controlled.


Enabling and Disabling Interrupts
11

 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

 How can the processor recognize the device requesting an


interrupt?
 Given that different devices are likely to require different
interrupt-service routines, how can the processor obtain the
starting address of the appropriate routine in each case?
 (Vectored interrupts)
 Should a device be allowed to interrupt the processor while
another interrupt is being serviced?
 (Interrupt nesting)
 How should two or more simultaneous interrupt requests be
handled?
 (Daisy-chain)
Vectored Interrupts
13

 A device requesting an interrupt can identify itself by sending


a special code to the processor over the bus.
 Interrupt vector
 Avoid bus collision
Interrupt Nesting
14

 Simple solution: only accept one interrupt at a time, then disable


all others.
 Problem: some interrupts cannot be held too long.
 Priority structure
Simultaneous Requests
15
Controlling Device Requests
16

 Some I/O devices may not be allowed to issue interrupt requests


to the processor.
 At device end, an interrupt-enable bit in a control register
determines whether the device is allowed to generate an interrupt
request.
 At processor end, either an interrupt enable bit in the PS register
or a priority structure determines whether a given interrupt
request will be accepted.
Exceptions
17

 Recovery from errors


 Debugging
 Trace
 Breakpoint
 Privilege exception
Use of Interrupts in Operating Systems
18

 The OS and the application program pass control back and


forth using software interrupts.
 Supervisor mode / user mode
 Multitasking (time-slicing)
 Process – running, runnable, blocked
 Program state
Processor Examples
19
Interrupt-service routine to read an input line from a keyboard
20

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

 Think about the overhead in both polling and interrupting


mechanisms when a large block of data need to be transferred
between the processor and the I/O device.
 A special control unit may be provided to allow transfer of a
block of data directly between an external device and the main
memory, without continuous intervention by the processor –
direct memory access (DMA).

 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

 Processor sends the starting address, the number of data, and


the direction of transfer to DMA controller.
 Processor suspends the application program requesting DMA,
starts DMA transfer, and starts another program.
 After the DMA transfer is done, DMA controller sends an
interrupt signal to the processor.
 The processor puts the suspended program in the Runnable
state.
DMA Register
23
System
24
Memory Access
25

 Memory access by the processor and the DMA controller are


interwoven.
 DMA device has higher priority.
 Among all DMA requests, top priority is given to high-speed
peripherals.
 Cycle stealing
 Block (burst) mode
 Data buffer
 Conflicts
Bus Arbitration
26

 The device that is allowed to initiate data transfers on the bus at


any given time is called the bus master.

 Bus arbitration is the process by which the next device to become


the bus master is selected and bus mastership is transferred to it.
 Need to establish a priority system.

 Two approaches: centralized and distributed


Centralized Arbitration
27
Distributed Arbitration
28
Types of DMA Configurations
Inefficient
29 same bus is
shared

Single bus detached DMA

Single-bus, integrated DMA-I/O


Types of DMA Configurations
30

I/O bus

Expandable
configuration
File system
31

•symbolic file names are converted to


Identifiers that ref files thro’ file descriptors
•files can be added deleted , reorganized

•Deals with logical structure of files


•Operations open, close, read, write
•Access rights are managed

•logical references to files and records


must be converted to physical secondary
storage addresses
•Allocation of secondary storage space
and main storage buffers
I/O Buffering
32

 Why buffering is required?


 When a user process wants to read blocks of data from a disk, process waits for
the transfer
 It waits either by
 Busy waiting
 Process suspension on an interrupt
 The problems with this approach
 Program waits for slow I/O
 Virtual locations should stay in the main memory during the course of block
transfer
 Risk of single-process deadlock
 Process is blocked during transfer and may not be swapped out

The above inefficiencies can be resolved if input transfers in advance of


requests are being made and output transfers are performed some time after the
request is made. This technique is known as buffering.
Types of I/O Devices
33

 block-oriented:
 Stores information in blocks that are usually of fixed size, and

transfers are made one block at a time


 Reference to data is made by its block number

 Eg: Disks and USB keys

 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

•When a user process issues an I/O request, the OS assigns a buffer in


the system portion of main memory to the operation
Reading ahead:
Input transfers are made to the system buffer. When the transfer is complete,
the process moves the block into user space and immediately requests another
block.
When data are being transmitted to a device, they are first copied from the
user space into the system buffer, from which they will ultimately be written.
Single Buffer (Stream-oriented data)
35

 line-at-a-time fashion:
 user input is one line at a time, with a carriage return signalling

the end of a line


 output to the terminal is similarly one line at a time Eg: Line

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

Block oriented transfer : the execution time as max [C, T]

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

When more than two buffers are used then collection of


buffers is known as circular buffer with each individual
buffer being one unit of the circular buffer
Buses
38

 The primary function of a bus is to provide a communications


path for the transfer of data.
 A bus protocol is the set of rules that govern the behavior of
various devices connected to the bus as to when to place
information on the bus, assert control signals, etc.

 Three types of bus lines: data, address, control


 The bus control signals also carry timing information.
 Bus master (initiator) / slave (target)
Synchronous Bus Timing
39
Synchronous Bus Detailed Timing
40
Multiple-Cycle Transfers
41
Input and Output: File System Information, Reading, Writing, and Streams

42

Create a consoleApp and navigate to the directory where you created it


File System Management
43

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);
}

FileInfo[] files = dInfo.GetFiles();


Console.WriteLine("files:");
foreach (FileInfo file in files)
{
Console.WriteLine(" {0} ({1} bytes)", file.Name, file.Length);
}
}
}
How to Get Information about a directory
44

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.

Examine the Stream properties:

•CanRead: determines whether the stream supports reading.


•CanSeek: determines whether the stream supports seeking.
•CanTimeout: determines whether the stream can time out.
•CanWrite: determines whether the stream can be written to.
•ReadTimeout: gets or sets the stream’s timeout for read operations.
•WriteTimeout: gets or sets the stream’s position for write operations.
Reading Data
47

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();

while (theByte != -1)


{
cStream.WriteByte((byte)theByte);
theByte = sfile.ReadByte();
}
}
}
Class Activity
56

Create three binary files and zip them to the


sys_pro_last_class_activity.zip
Shared Library
57

 Non-shared v.s. Shared Library

 A library is a collection of pre-written function calls.


 Using existing libraries can save a programmer a lot of time. (e.g.,
the printf() in the C library)
 However, if a library is not shared among many programs, but
instead a copy is included in every program that uses it, the size
of each linked program will be large.
 Also, a lot of disk and memory space will be wasted by these
programs that uses non-shared libraries.
 This motivates the concept of shared library.
How Does It Work?
58

 At link time, the linker searches through libraries to find modules


that resolve undefined external symbols.
 Rather than copying the contents of the module into the output
file, the linker makes a note of what library the module come
from and puts a list of the libraries in the executable.
 When the program is loaded , startup code finds those libraries
and maps them into the program’s address space before the
program starts.
 Standard operating system file-mapping semantics automatically
share pages that are mapped read-only or copy-on-write.
Shared Library
59

#include <math.h>
main() {
printf("exponential value %d\n", exp(-1));
}
Linking Shared Libraries
60

 The most difficult aspect of static shared libraries is address


space management.
 Each shared library occupies a fixed piece of address space in
each program in which it is used.
 Different libraries have to use non-overlapping addresses if they
can be used in the same program.
 Assigning address space to libraries is a black art.
 On one hand, you want to leave some space in between them
to allow version updates.
 On the other hand, you like to put popular libraries as close
together as possible to minimize the number of page tables
needed.
Address Space Management
61

 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

 Each individual shared library exports symbols, both code and


data, and usually also imports symbols if the library depends on
other libraries.
 To allow library updates, we need a method to update a library
without changing the addresses of exported symbols.
 For code addresses, rather than exporting the address of each
routine, the library contains a table of jump instructions that
jump to all of the routines, with the addresses of the jump
instructions exported as the addresses of the routines.
 The performance cost is one extra jump. Normally this is
acceptable.
Techniques for Minor Updates
63

 For exported data, the situation is more difficult, because there


is no easy way to add a level of indirection like to one for code
addresses.
 In practice, however, exported data tend to be tables (e.g., the
File structure for the C library) of known size or a single word
value like errno.
 We can collect these data and place them at the beginning of
the data section to precede any anonymous data that are part of
individual routines.
Binding Time
64

 Shared libraries raise binding time issues that do not apply to


conventionally linked program.
 A program that uses a shared library depends on having that
shared library available when the program is run.
 One kind of error occurs when the required libraries are not
present.
 In such a case, there is no much we can do. An error message is
usually printed.
Binding Time (cont’d)
65

 With static shared libraries, symbols are still bound to addresses


at link time, but library code is not bound to the executable code
until run time.

 A static shared library cannot change very much without


breaking the programs that it is bound to.
 If we change the addresses of routine and data in the new

version of library, then the program will crash.


Binding Time (cont’d)
66

 A static shared library can sometimes be updated without


breaking the program that uses it, if the updates can be made in
a way that does not move any address in the library.
 This permits minor version updates, typically for small bug
fixes.
 Larger changes unavoidably change program addresses, which
means that a system needs multiple versions of the library.
 E.g., libc.so.4
Structure of Shared Library
67

 The shared library is an


executable format file.
 The jump table is
considered to be text,
because it is executable
code.
Final Exam information

68

Final Exam Time and date

July 25th – 5:45 pm

Topics

Lecture 06
Lecture 07
Lecture 08

Final Exam 25%


Reference
69

Michael Kerrisk, The LINUX Programming Interface, 1st Edition, No Starch Press
ISBN-13 978-1593272203

You might also like