Programmer Guide > Working with Data Files > Logical Unit Numbers (LUNs)
  

Logical Unit Numbers (LUNs)
PV-WAVE logical unit numbers are in the range {–2...128}; they are divided into three groups:
* Reserved Logical Unit Numbers (–2, –1, 0)
* Logical Unit Numbers for General Use (1...99)
* Logical Unit Numbers Used by GET_LUN/FREE_LUN (100...128)
Reserved Logical Unit Numbers (–2, –1, 0)
0, –1, and –2 are special file units that are always open within PV-WAVE:
*0 (zero)—The standard input stream, which is usually the keyboard. This implies that the statement:
READ, X
*is equivalent to
READF, 0, X
*The user would then enter the values of X from the keyboard, as shown in the following statements:
READ, X
:  0.2, 0.4, 0.6
*The line preceded with the colon (:) denotes user input.
*–1 (negative 1)—The standard output stream, which is usually the workstation’s screen. This implies that the statement:
PRINT, X
*is equivalent to
PRINTF, -1, X
*The following command can be used to send a message to the screen:
PRINT, 'Hello World.'
*The following line:
Hello World.
*is sent to the workstation’s screen.
*–2 (negative 2)—The standard error stream, which is usually the workstation’s screen.
Because READ and PRINT automatically use the standard input and output streams (files) by default, basic ASCII I/O is extremely simple.
Operating System Dependencies
The reserved files units have a special meaning which is operating-system dependent, as explained in the following sections:
UNIX
The reserved LUNs are equated to stdin, stdout, and stderr respectively. This means that the normal UNIX file redirection and pipe operations work with PV-WAVE. For example, the shell command:
% wave < wave.inp > wave.out &
causes PV-WAVE to execute in the background, reading its input from the file wave.inp and writing its output to the file wave.out.
Logical Unit Numbers for General Use (1...99)
These are file units for normal interactive use. When using PV-WAVE interactively, you can select any number in this range.
The following statements show how a string, “Hello World.”, could be sent to a file named hello.dat:
; Open LUN 1 for hello.dat with write access.
OPENW, 1, 'hello.dat'
; Insert the string “Hello World.” into the file hello.dat.
PRINTF, 1, 'Hello World.'
; You're done with the file, so close it.
CLOSE, 1
Logical Unit Numbers Used by GET_LUN/FREE_LUN (100...128)
These are file units that are managed by the GET_LUN and FREE_LUN procedures. GET_LUN and FREE_LUN provide a standard mechanism for routines to obtain a LUN.
GET_LUN allocates a file unit from a pool of free units in the range {100...128}. This unit will not be allocated again until it is released by a call to FREE_LUN. Meanwhile, it is available for the exclusive use of the program that allocated it.
 
note
When writing procedures and functions, be sure not to explicitly assign file unit numbers in the range {100...128}. If a procedure or function reads or writes to an explicitly assigned file unit, there is a chance it will conflict with other routines that are using the same unit. Always use the GET_LUN and FREE_LUN procedures to manage LUNs.
Sample Usage—GET_LUN and FREE_LUN
A typical procedure that needs a file unit might be structured in the following way:
PRO demo
; Get a unique file unit and open the file.
OPENR, Unit, 'file.dat', /GET_LUN
.
.		 (Other commands go here.) 
.
; Return the file unit number. Since the file is still open, 
; FREE_LUN will automatically call CLOSE.
FREE_LUN, Unit
END
 
note
All procedures and functions that open files, including those that you write yourself, should use GET_LUN and FREE_LUN to obtain file units. Never use a file unit in the range {100...128} unless it was previously allocated with GET_LUN.

Version 2017.0
Copyright © 2017, Rogue Wave Software, Inc. All Rights Reserved.