Você está na página 1de 9

Isis software documentation Input and output ports This document explains the standard input and output

facilities in Isis. Input from and output to any endpoint is handled using the same mechanism known as a port, which allows the same procedures to be used for reading, writing, and other operations, regardless of where the data is coming from or going to. You can create ports that connect to disk files, memory buffers, internet sites, multicast networks, or serial ports, just to name a few. In Isis, a port is represented as a list of several items, which includes the port type, internal identity information, and the procedures used to operate on the port. It will usually not be necessary to look at anything in this list, and of course, nothing in the list should ever be modified manually. Use only the port manipulation routines described below. Default input and output ports default-input-port # the default input port (stdin) default-output-port # the default output port (stdout) (current-input-port) # return the current input port (current-output-port) # return the current output port (set-input-port port) # change the current input port (set-output-port port) # change the current output port The default input and output ports are usually your keyboard and your terminal on your screen. Whatever you type on your keyboard is available for reading from the default input port, and whatever you write to the default output port appears on your screen. Technically, the default input port and output ports refer to the "standard input" and "standard output" files of the Isis process. The current input and output ports indicate the places Isis will read and write data to when no particular port is specified in the routines described later (for example, the print function prints messages to the current output port). current-input-port and current-output-port allow you to find out what the current input and output ports are. When Isis is started, the current input and output ports are the same as the default input and output ports, but you can change them using set-input-port and set-output-port. Opening ports <a name="open"> (open-input filename) # open a disk file for input (open-output filename) # open a disk file for output (open-update filename) # open a disk file for input and output (new-string-port name string) # create new port connected to a string (new-memory-port name size address freeflag) # create new port connected to memory buffer <tt>open-input</tt>, <tt>open-output</tt>, and <tt>openupdate</tt> open a disk file for reading, writing, or both. A port is returned, or <tt>Null</tt> if there was a problem accessing the file. </a> <a name="open"> (set iport (open-input "inputfile.dat")) (set oport (open-output "outputfile.dat")) </a>

new-string-port creates a port that points to a specified string (which is copied into a memory buffer), and new-memory-port creates a port that points directly to a buffer in memory. A string name is expected for each, to be used for identification and in error messages. new-memory-port expects the size of the memory buffer in bytes, the address of the buffer, and a booleanfreeflag indicating whether the buffer should be automatically freed when the port is closed. Both routines return the newly created port, valid for reading or writing. <a name="open"> (set size 32768) (set buf (allocate-memory size)) (set mport (new-memory-port "mybuf" size buf False)) </a> In addition to disk files and memory buffers, you may open ports to TCP connections, serial ports, multicast networks, unix pipes, and other devices. Descriptions of these ports appears in other sections of the documentation. Closing ports <a name="open"> (close port) # close port and deallocate associated resources When a port is no longer needed, it should be closed so that resources associated with the port may be reclaimed. </a> Reading and writing data <a name="open"> (read-string port) # read string up to a newline character (read port) # read CODED Isis values (read-raw port numbytes address) # read raw bytes to memory address (write-string port valval ...) # write values in human readable format (write port valval ...) # write CODED Isis values (writeraw port numbytes address) # write raw bytes from memory address (read-string) # same as read-string from the current input port (print valval ...) # same as write-string to current output port Isis allows you to read and write data on ports in three different formats: strings, coded Isis values, and raw bytes. There is a separate pair of read and write functions for each format. If data is written in one format, it should usually be read back with the corresponding read function for the same format. </a> read-string and write-string read and write human-readable strings. read-string reads characters from the port up to the next newline character and returns a string (without the newline in it), or Null if no more data is available. write-string writes one or more strings, characters, and other Isis values in a human-readable format, and returns the total number of bytes written to the port, or Null if an error occurred. Values are written exactly as they would be displayed in Isis, except that strings and characters are written without their quotes, and a newline character must be given explicitly if it is desired. <a name="open"> (set oport (open-output "test.txt")) (write-string oport "The number of the day is: " (random) newline) (write-string oport "Thank you and goodbye." newline) (close oport) (set iport (openinput "test.txt")) (while (!= Null (set aline (read-string iport))) (print aline newline)) (close iport) </a> Plain read and write receive and transmit Isis values in a compact coded form that is not readable by humans. These routines are most useful for sending and receiving Isis values over a network or in disk files, where other programs will not have to access the data. Any type of value except procedures and addresses may be read or written. read reads one value from the given port and returns it, or Null if no

more data is available. write writes one or more values to the given port, and returns the total number of bytes written, or Null if an error occurred. A description of the protocol used in coding values can be obtained from Stefan. <a name="open"> (set oport (open-output "test.data")) (write oport (random) (random) (random)) (close oport) (set iport (open-input "test.data")) (print "The three secret numbers are: " (read iport) (read iport) (read iport) newline) (close iport) </a> read-raw and write-raw receive and transmit blocks of raw data in memory buffers. Each expects the port, an integer number of bytes, and the address of the memory buffer. Each also returns the total number of bytes read or written, or Null if an error occurred. These functions are most useful for transfering large blocks of raw data, like images or audio, in the most time and space efficient manner. <a name="open"> (set outbuf (allocate-memory 50)) # allocate a memory buffer (outbuf c-byte (makeseries 0 50 1)) # put something in the buffer (set oport (open-output "test.raw")) (write-raw oport 50 outbuf) # write the entire buffer (close oport) (set inbuf (allocate-memory 50)) # allocate a second buffer (set iport (open-input "test.raw")) (read-raw iport 50 inbuf) # read 50 bytes into it (close iport) (display (inbuf [c-byte 50])) # display the contents </a> When read-string is called with no argument, it reads a string from the current input port (usually the keyboard). Similarly, the print function prints values to the current output port (usually the screen). These functions are most useful as a way of requesting a line of input from the user and writing messages that the user will see on the screen. See above for more information about how to query or change the current input and output ports. <a name="open"> (while True (begin (print "Please enter a number: ") (set numstr (read-string)) (print "The square root of your number is: " (sqrt (evalnumstr)) newline))) </a> Port position <a name="open"> (tell port) # return the current position in port (seek port pos) # seek to a particular position in port <tt>tell</tt> returns the current read/write position in the port in bytes relative to the beginning of the file, or <tt>Null</tt> if it is not known or if there is an error. <tt>seek</tt> sets the current read/write position. It returns <tt>True</tt> if successful, and <tt>False</tt> if not. </a> Port status <a name="pending"> (pending port) # return True if data is waiting to be read at port (pending port microsecs) # return True if data pending or False if time limit reached (read-ready port) # same as pending (read-ready port microsecs) # same as pending (write-ready port) # return True if data may be written to port (write-ready port microsecs) # return True if data may be written or False if time limit reached <tt>pending</tt> returns <tt>True</tt> if data is waiting to be read from the specified port, or <tt>False</tt> otherwise. If a second argument is given, the function waits up to the specified number of microseconds, and returns immediately with <tt>True</tt> if data is pending, or <tt>False</tt> if the time limit was reached. <tt>read-ready</tt> is exactly the same as <tt>pending</tt>. </a>

write-ready performs the converse operation for writing. It returns True if the port is ready to accept data, or False if not or if the optional time limit has been reached. This routine is typically useful to prevent a program from blocking when, in TCP networking applications for example, a outgoing connection is hung and momentarily cannot accept more data. Port queries <a name="pending"> (input-port? port) # return True if port supports input (output-port? port) # return True if port supports output These routines check if the given port supports input or output. The result is either <tt>True</tt> or <tt>False</tt>. </a> <a name="pending"> -&gt; (input-port? (current-input-port)) True -&gt; (output-port? (current-inputport)) False </a> Port utilities <a name="utilities"> (read-text port numchars) # read up to numchars, return string (read-all-text port) # read entire port, return string (read-data port numbytes) # read up to numbytes, return [buflen] (readall-data port) # read entire port, return [buflen] (transfer-data inportoutportnumbytes) # transfer numbytes from inport to outport (transfer-all-data inportoutport) # transfer entire inport to outport<tt>read-text</tt> reads up to the specified number of characters from the given port and returns a string. <tt>read-all-text</tt> reads as many characters as possible from the port and returns a string. Both functions return <tt>Null</tt> if there is an error. </a> read-data and read-all-data are similar, except they read raw data. The return value is a list of two items: the address of a newly-allocated memory buffer, and an integer indicating the number of bytes stored in that buffer (which may be smaller than the number of bytes requested). You are responsible for deallocating the memory buffer when you no longer need it. If there is an error, the returned number of bytes will be 0. transfer-data and transfer-all-data read bytes from one port and write them to another. The number of bytes actually transferred is returned, or 0 if there was an error. <a name="utilities"> (set copy-file (proc (innameoutname) (local (infileoutfile) (begin (set infile (openinput inname)) (set outfile (open-output outname)) (if (and infileoutfile) (transfer-all-data infileoutfile)) (close infile) (close outfile))))) </a> Port list internals <a name="utilities"> port-name # string, port name port-type # string, port type port-inflag # boolean, input enabled on port? port-outflag # boolean, output enabled on port? port-internal-id # internal id of port, or Null if not needed port-close-proc # procedures for manipulating this port port-read-proc portreadline-proc port-write-proc port-tell-proc port-seek-proc port-read-ready-proc port-write-ready-proc port-configure-proc These constants represent indices in the port descriptor list that you can use to get

certain information about a port. The most useful item to most users is the probably the name of the port, which you can obtain by indexing into the port list with <tt>port-name</tt>. </a> <a name="utilities"> -&gt; ((current-input-port) port-name) "Standard Input" -&gt; ((current-outputport) port-name) "Standard Output" </a> Creating new kinds of ports <a name="utilities"> (new-port name type inflagoutflag internal-id close-proc read-procreadline-proc write-proc tell-proc seek-proc read-ready-proc write-ready-proc configure-proc) # create a new port <tt>new-port</tt> creates a new port. <tt>name</tt> and <tt>type</tt> should be strings. <tt>inflag</tt> and <tt>outflag</tt> should be booleans that indicate whether the port will support input or output or both. <tt>internal-id</tt> can be any value that you want to use as an internal identifier to the port. The rest of the arguments will be the procedures that are used to manipulate the port. The procedures that you design for the port should be defined in the following way, where <tt>id</tt> will be the internal-id that you specify in <tt>new-port</tt>: <xmp> (close id) # close and free resources, return True if successful (read id n addr) # read n bytes into addr, return num bytes actually read (readline id n addrstopval) # read n bytes or up to stopval, return num bytes actually read (write id n addr) # write n bytes from addr, return num bytes actually written (tell id) # return current read/write position, or Null if N/A (seek id pos) # set current read/write position, return True if successful (read-ready id microsecs) # return True if data can be read or False if time limit reached (write-ready id microsecs) # return True if data can be written or False if time limit reached (configure id argarg ...) # perform user-defined operations For example, below is the definition of new-memory-port, which uses new-port to create a new port. The procedures for manipulating the port are defined within the scope of a local environment that includes bindings needed for the internal operations of the port. Talk to Stefan for more information on creating new types of ports.

<a name="utilities"> (set new-memory-port (proc (name buflenmembuffreeflag) (local (bufpos) (begin (set bufpos 0) (new-port name "MEMORY" [ buflenmembuffreeflag ] True True # close function (proc (id) (begin (if freeflag (free membuf)) True)) # read function (proc (id size ptr) (local (readsize) (begin (set readsize (min size (- buflenbufpos))) (if readsize (copy-memory readsize (+ membufbufpos) ptr)) (set bufpos (+ bufposreadsize)) readsize))) # readline function (proc (id size ptrendval) (local (readsizeval going) (begin (set readsize 0) (set going True) (while (and (&lt; bufposbuflen) going) (begin (set val ((+ membufbufpos) c-byte)) ((+ ptrreadsize) c-byte val) (set readsize (+ readsize 1)) (set bufpos (+ bufpos 1)) (if (= valendval) (set going False)))) readsize))) # write function (proc (id size ptr) (local (writesize) (begin (set writesize (min size (- buflenbufpos))) (if writesize (copy-memory writesizeptr (+ membufbufpos))) (set bufpos (+ bufposwritesize)) writesize))) # tell function (proc (id) bufpos) # seek function (proc (id pos) (set bufpospos)) # read-ready function (proc (id time) (not (= bufposbuflen))) # write-ready function (proc (id time) (= bufposbuflen)) # configure function (procargs Null) ))))) </a>

Requirements: Scripts: (load "port-utilities.isis")

Input/output From Wikipedia, the free encyclopedia Jump to: navigation, search

"I/O" redirects here. For other uses, see I/O (disambiguation). For uses of the term input-output in economics, see Input-output model. For the medical term, see Toileting#Input and output. This article does not cite any references or sources. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed. (June 2010)

In computing, input/output, or I/O, refers to the communication between an information processing system (such as a computer), and the outside world, possibly a human, or another information processing system. Inputs are the signals or data received by the system, and outputs are the signals or data sent from it. The term can also be used as part of an action; to "perform I/O" is to perform an input or output operation. I/O devices are used by a person (or other system) to communicate with a computer. For instance, a keyboard or a mouse may be an input device for a computer, while monitors and printers are considered output devices for a computer. Devices for communication between computers, such as modems and network cards, typically serve for both input and output.

Note that the designation of a device as either input or output depends on the perspective. Mouse and keyboards take as input physical movement that the human user outputs and convert it into signals that a computer can understand. The output from these devices is input for the computer. Similarly, printers and monitors take as input signals that a computer outputs. They then convert these signals into representations that human users can see or read. For a human user the process of reading or seeing these representations is receiving input. These interactions between computers and humans is studied in a field called human computer interaction.

In computer architecture, the combination of the CPU and main memory (i.e. memory that the CPU can read and write to directly, with individual instructions) is considered the brain of a computer, and from that point of view any transfer of information from or to that combination, for example to or from a disk drive, is considered I/O. The CPU and its supporting circuitry provide memory-mapped I/O that is used in low-level computer programming in the implementation of device drivers. An I/O algorithm is one designed to exploit locality and perform efficiently when data reside on secondary storage, such as a disk drive. Interface I/O Interface is required whenever the I/O device is driven by the processor. The interface must have necessary logic to interpret the device address generated by the processor. Handshaking should be

implemented by the interface using appropriate commands like (BUSY,READY,WAIT), and the processor can communicate with I/O device through the interface. If different data formats are being exchanged, the interface must be able to convert serial data to parallel form and vice-versa. There must be provision for generating interrupts and the corresponding type numbers for further processing by the processor if required A computer that uses memory-mapped I/O accesses hardware by reading and writing to specific memory locations, using the same assembly language instructions that computer would normally use to access memory. Higher-level implementation Higher-level operating system and programming facilities employ separate, more abstract I/O concepts and primitives. For example, most operating systems provide application programs with the concept of files. The C and C++ programming languages, and operating systems in the Unix family, traditionally abstract files and devices as streams, which can be read or written, or sometimes both. The C standard library provides functions for manipulating streams for input and output.

In the context of the ALGOL 68 programming language, the input and output facilities are collectively referred to as transput. The ALGOL 68 transput library recognizes the following standard files/devices: stand in, stand out, stand errors and stand back.

An alternative to special primitive functions is the I/O monad, which permits programs to just describe I/O, and the actions are carried out outside the program. This is notable because the I/O functions would introduce side-effects to any programming language, but now purely functional programming is practical. Addressing mode There are many ways through which data can be read or stored in the memory. Each method is an addressing mode, and has its own advantages and limitations. There are many type of addressing modes such as direct addressing, indirect addressing, immediate addressing, index addressing, based addressing, based-index addressing, implied addressing, etc. Direct addressing In this type of address of the data is a part of the instructions itself. When the processor interprets the instruction, it gets the memory address from where it can be read/written the required information. For example: [1]

MOV register, [address] ; to read MOV [address], register ; to write similarly IN register, [address] ; to read as input OUT [address], register ; to write as output Here the address operand points to a memory location which holds the data and copies it into/from the specified register. A pair of brackets is a dereference operator. Indirect addressing According to the above example, the address can be stored in another register. Therefore, the instructions will have the register representing the address. So to fetch the data, the instruction must be interpreted appropriate register selected. The value of the register will be used for addressing appropriate memory location and then data will be read/written. This addressing method has an advantage against the direct mode that the register value is changeable so the appropriate memory location can also be dynamically selected. Port-mapped I/O Port-mapped I/O usually requires the use of instructions which are specifically designed to perform I/O operations.

Você também pode gostar