Document revision date: 30 March 2001

## OpenVMS Programming Concepts Manual

Previous | Contents | Index

### 23.3.2 Logical I/O Operations

In logical I/O operations, data is read from and written to logically addressable units of the device. Logical operations can be performed on both block-addressable and record-oriented devices. For block-addressable devices (such as disks), the addressable units are 512-byte blocks. They are numbered from 0 to n -1 , where n is the number of blocks on the device. For record-oriented or non-block-structured devices (such as terminals), logical addressable units are not pertinent and are ignored. Logical I/O requires that one of the following conditions be met:

- The issuing process has physical I/O privilege (PHY_IO).
- The issuing process has logical I/O privilege (LOG_IO).
- The volume is mounted foreign and the volume protection mask allows access to the device.

If none of these conditions is met, the logical I/O operation is rejected by the SYS$QIO system service, which returns a condition value of SS$_NOPRIV (no privilege). Figure 23-3 illustrates the logical I/O access checks in greater detail.

### 23.3.3 Virtual I/O Operations

You can perform virtual I/O operations on both record-oriented (non-file-structured) and block-addressable (file-structured) devices. For record-oriented devices (such as terminals), the virtual function is the same as a logical function; the virtual addressable units of the devices are ignored.

For block-addressable devices (such as disks), data is read from and written to open files. The addressable units in the file are 512-byte blocks. They are numbered starting at 1 and are relative to a file rather than to a device. Block-addressable devices must be mounted and structured and must contain a file that was previously accessed on the I/O channel.

Virtual I/O operations also require that the volume protection mask allow access to the device (a process having either physical or logical I/O privilege can override the volume protection mask). If these conditions are not met, the virtual I/O operation is rejected by the QIO system service, which returns one of the following condition values:

Condition Value | Meaning
--- | ---
SS$_NOPRIV | No privilege
SS$_DEVNOTMOUNT | Device not mounted
SS$_DEVFOREIGN | Volume mounted foreign

Figure 23-4 shows the relationship of physical, logical, and virtual I/O to the driver.

Figure 23-2 Physical I/O Access Checks

Figure 23-3 Logical I/O Access Checks

Figure 23-4 Physical, Logical, and Virtual I/O

## 23.4 I/O Function Encoding

I/O functions fall into three groups that correspond to the three I/O device addressing modes (physical, logical, and virtual) described in Section 23.3. Depending on the device to which it is directed, an I/O function can be expressed in one, two, or all three modes.

I/O functions are described by 16-bit, symbolically expressed values that specify the particular I/O operation to be performed and any optional function modifiers. Figure 23-5 shows the format of the 16-bit function value.

Symbolic names for I/O function codes are defined by the $IODEF macro.

Figure 23-5 I/O Function Format

### 23.4.1 Function Codes

The low-order 6 bits of the function value are a code that specifies the particular operation to be performed. For example, the code for read logical block is expressed as IO$_READLBLK. Table 23-1 lists the symbolic values for read and write I/O functions in the three transfer modes.

Table 23-1 Read and Write I/O Functions
Physical I/O | Logical I/O | Virtual I/O
--- | --- | ---
IO$_READPBLK | IO$_READLBLK | IO$_READVBLK
IO$_WRITEPBLK | IO$_WRITELBLK | IO$_WRITEVBLK

The set mode I/O function has a symbolic value of IO$_SETMODE.

Function codes are defined for all supported devices. Although some of the function codes (for example, IO$_READVBLK and IO$_WRITEVBLK) are used with several types of devices, most are device dependent; that is, they perform functions specific to particular types of devices. For example, IO$_CREATE is a device-dependent function code; it is used only with file-structured devices such as disks and magnetic tapes. The I/O user's reference documentation provides complete descriptions of the functions and function codes.

Note You should determine the device class before performing any QIO function, because the requested function might be incompatible with some devices. For example, the SYS$INPUT device could be a terminal, a disk, or some other device. Unless this device is a terminal, an IO$_SETMODE request that enables a Ctrl/C AST is not performed.

### 23.4.2 Function Modifiers

The high-order 10 bits of the function value are function modifiers. These are individual bits that alter the basic operation to be performed. For example, you can specify the function modifier IO$M_NOECHO with the function IO$_READLBLK to a terminal. When used together, the two values are written in VAX MACRO as IO$_READLBLK!IO$M_NOECHO. This causes data typed at the terminal keyboard to be entered into the user buffer but not echoed to the terminal. Figure 23-6 shows the format of function modifiers.

Figure 23-6 Function Modifier Format

As shown in Figure 23-6, bits <15:13> are device- or function-independent bits, and bits <12:6> are device- or function-dependent bits. Device- or function-dependent bits have the same meaning, whenever possible, for different device classes. For example, the function modifier IO$M_ACCESS is used with both disk and magnetic tape devices to cause a file to be accessed during a create operation. Device- or function-dependent bits always have the same function within the same device class.

There are two device- or function-independent modifier bits: IO$M_INHRETRY and IO$M_DATACHECK (a third bit is reserved). IO$M_INHRETRY is used to inhibit all error recovery. If any error occurs and this modifier bit is specified, the operation is terminated immediately and a failure status is returned in the I/O status block (see Section 23.10). Use IO$M_DATACHECK to compare the data in memory with that on a disk or magnetic tape.

## 23.5 Assigning Channels

Before any input or output operation can be performed on a physical device, you must assign a channel to the device to provide a path between the process and the device. The Assign I/O Channel (SYS$ASSIGN) system service establishes this path.

When you write a call to the SYS$ASSIGN service, you must supply the name of the device, which can be a physical device name or a logical name, and the address of a word to receive the channel number. The service returns a channel number, and you use this channel number when you write an input or output request.

For example, the following lines assign an I/O channel to the device TTA2. The channel number is returned in the word at TTCHAN.

    #include <descrip.h>
    #include <lib$routines.h>
    #include <ssdef.h>
    #include <starlet.h>
    #include <stdio.h>
    #include <stsdef.h>

    main() {
            unsigned int status;
            unsigned short ttchan;
            $DESCRIPTOR(ttname,"TTA2:");

            /* Assign a channel to a device */
            status = SYS$ASSIGN( &ttname,   /* devnam - device name */
                                 &ttchan,   /* chan - channel number */
                                 0,         /* acmode - access mode */
                                 0,         /* mbxnam - logical name for mailbox */
                                 0 );       /* flags */
            if (!$VMS_STATUS_SUCCESS(status))
                LIB$SIGNAL(status);

     return SS$_NORMAL;
    }

To assign a channel to the current default input or output device, use the logical name SYS$INPUT or SYS$OUTPUT.

For more details on how SYS$ASSIGN and other I/O services handle logical names, see Section 23.2.5.

### 23.5.1 Using the Share Privilege with the SYS$ASSIGN and SYS$DASSGN Services

Use of SHARE privilege should be made only with caution, as applications, application protocols, and device drivers coded to expect only exclusive access can encounter unexpected and potentially errant behavior when access to the device is unexpectedly shared via use of SHARE privilege.

If you use the SHARE privilege to override the exclusivity requested by another process's call to the system service SYS$ASSIGN, and the original process then attempts to deassign its channels via explicit calls to SYS$DASSGN or via the implicit calls to SYS$DASSGN made during image or process rundown, the OpenVMS last-channel-deassign code may not operate as expected due to the assignment of the additional I/O channels to the device. The presence of these extra channels will prevent the last-channel-deassign code from releasing the ownership of the device, potentially resulting in a device owned by the process identification (PID) of a nonexistent process.

Unless its use is explicitly supported by the application, the application protocol, and the device driver, the use of SHARE privilege is generally discouraged.

## 23.6 Queuing I/O Requests

All input and output operations in the operating system are initiated with the Queue I/O Request (SYS$QIO) system service. The SYS$QIO system service permits direct interaction with the system's terminal driver. SYS$QIOs permit some operations that cannot be performed with language I/O statements and RTL routines; calls to SYS$QIO reduce overhead and permit asynchronous I/O operations. However, calls to SYS$QIO are device dependent. The SYS$QIO service queues the request and returns immediately to the caller. While the operating system processes the request, the program that issued the request can continue execution.

The format for SYS$QIO is as follows:

`SYS$QIO([efn],chan,func[,iosb][,astadr][,astprm][,p1][,p2][,p3][,p4][,p5][,p6]`

Required arguments to the SYS$QIO service include the channel number assigned to the device on which the I/O is to be performed, and a function code (expressed symbolically) that indicates the specific operation to be performed. Depending on the function code, one to six additional parameters may be required.

For example, the IO$_WRITEVBLK and IO$_READVBLK function codes are device-independent codes used to read and write single records or virtual blocks. These function codes are suitable for simple terminal I/O. They require parameters indicating the address of an input or output buffer and the buffer length. A call to SYS$QIO to write a line to a terminal may look like the following:

    #include <starlet.h>

            unsigned int status, func=IO$_WRITEVBLK;
       .
       .
       .
            status = SYS$QIO(0,             /* efn - event flag */
                            ttchan,         /* chan - channel number */
                            func,           /* func - function modifier */
                            0,              /* iosb - I/O status block */
                            0,              /* astadr - AST routine */
                            0,              /* astprm - AST parameter */
                            buffadr,        /* p1 - output buffer */
                            buflen);        /* p2 - length of message */

Function codes are defined for all supported device types, and most of the codes are device dependent; that is, they perform functions specific to a particular device. The $IODEF macro defines symbolic names for these function codes. For information about how to obtain a listing of these symbolic names, see Appendix D. For details about all function codes and an explanation of the parameters required by each, see the OpenVMS I/O User's Reference Manual.

To read from or write to a terminal with the SYS$QIO or SYS$QIOW system service, you must first associate the terminal name with an I/O channel by calling the SYS$ASSIGN system service, then use the assigned channel in the SYS$QIO or SYS$QIOW system service. To read from SYS$INPUT or write to SYS$OUTPUT, specify the appropriate logical name as the terminal name in the SYS$ASSIGN system service. In general, use SYS$QIO for asynchronous operations, and use SYS$QIOW for all other operations.

## 23.7 Synchronizing Service Completion

The SYS$QIO system service returns control to the calling program as soon as a request is queued; the status code returned in R0 indicates whether the request was queued successfully. To ensure proper synchronization of the queuing operation with respect to the program, the program must do the following:

- Test whether the operation was queued successfully.
- Test whether the operation itself completed successfully.

Optional arguments to the SYS$QIO service provide techniques for synchronizing I/O completion. There are three methods you can use to test for the completion of an I/O request:

- Specify the number of an event flag to be set when the operation completes.
- Specify the address of an AST routine to be executed when the operation completes.
- Specify the address of an I/O status block in which the system can place the return status when the operation completes.
  I/O status blocks are explained in Section 23.10.

The use of these three techniques is shown in the examples that follow. Example 23-1 shows specifying event flags.

Example 23-1 Event Flags

    #include <lib$routines.h>
    #include <starlet.h>
    unsigned int status, efn=0, efn1=1, efn=2;
       .
       .
       .
    status = SYS$QIO(efn1,...);   /* Issue 1st I/O request */
    if (!$VMS_STATUS_SUCCESS(status))
    LIB$SIGNAL( status );                 /* Queued successfully? */  (1)
       .
       .
       .
    status = SYS$QIO(efn2,...);   /* Issue second I/O request */  (2)
    if (!$VMS_STATUS_SUCCESS(status))     /* Queued successfully? */
            LIB$SIGNAL( status );
       .
       .
       .(3)
    status = SYS$WFLAND( efn,             / *Wait until both are done */
                         &mask,...(4)
                         .
                         .
                         .

1. When you specify an event flag number as an argument, SYS$QIO clears the event flag when it queues the I/O request. When the I/O completes, the flag is set.
2. In this example, the program issues two Queue I/O requests. A different event flag is specified for each request.
3. The Wait for Logical AND of Event Flags (SYS$WFLAND) system service places the process in a wait state until both I/O operations are complete. The efn argument indicates that the event flags are both in cluster 0; the mask argument indicates the flags for which the process is to wait.
4. Note that the SYS$WFLAND system service (and the other wait system services) wait for the event flag to be set; they do not wait for the I/O operation to complete. If some other event were to set the required event flags, the wait for event flag would complete too soon. You must coordinate the use of event flags carefully. (See Section 23.8 for a discussion of the recommended method for testing I/O completion.)

Example 23-2 shows specifying an AST routine.

Example 23-2 AST Routine

    #include <lib$routines.h>
    #include <starlet.h>
    #include <stsdef.h>
            unsigned int status, astprm=1;
       .
       .
       .
            status = SYS$QIO(...&ttast,   /* I/O request with AST */ (1)
                            astprm...);
            if (!$VMS_STATUS_SUCCESS( status ))   /* Queued successfully? */
                    LIB$SIGNAL( status );
       .
       .
       .
    }

    void ttast ( int astprm ) {                   /* AST service routine */ (2)

    /* Handle I/O completion */
       .
       .
       .

            return;
    }                              /* End of AST routine */

1. When you specify the astadr argument to the SYS$QIO system service, the system interrupts the process when the I/O completes and passes control to the specified AST service routine.
   The SYS$QIO system service call specifies the address of the AST routine, TTAST, and a parameter to pass as an argument to the AST service routine. When $QIO returns control, the process continues execution.
2. When the I/O completes, the AST routine TTAST is called, and it responds to the I/O completion. By examining the AST parameter, TTAST can determine the origin of the I/O request.
   When this routine is finished executing, control returns to the process at the point at which it was interrupted. If you specify the astadr argument in your call to SYS$QIO, you should also specify the iosb argument so that the AST routine can evaluate whether the I/O completed successfully.

Example 23-3 shows specifying an I/O status block.

Example 23-3 I/O Status Block

    #include <lib$routines.h>
    #include <stdio.h>
    #include <ssdef.h>
    #include <starlet.h>
    #include <stsdef.h>

       .
       .
       .
    /* I/O  status block */
            struct {
                     unsigned short iostat, iolen;
                     unsigned int dev_info;
    }ttiosb;                                                        (1)

            unsigned int status;
       .
       .
       .
            status = SYS$QIO(,..., &ttiosb, ...);   (2)
            if( !$VMS_STATUS_SUCCESS( status )) /* Queued successfully? */
                    LIB$SIGNAL( status );
       .
       .
       .
            while(ttiosb.iostat == 0) {
            /* Loop -- with delay -- until done */                  (3)

            }

            if( !$VMS_STATUS_SUCCESS( ttiosb.iostat )) {
            /* Perform error handling */
       .
       .
       .
            }

1. An I/O status block is a quadword structure that the system uses to post the status of an I/O operation. You must define the quadword area in your program. TTIOSB defines the I/O status block for this I/O operation. The iosb argument in the SYS$QIO system service refers to this quadword.
2. Instead of polling the low-order word of the I/O status block for the completion status, the program uses the preferred method of using an event flag and calling SYS$SYNCH to determine I/O completion.
3. The process polls the I/O status block. If the low-order word still contains zero, the I/O operation has not yet completed. In this example, the program loops until the request is complete.

## 23.8 Recommended Method for Testing Asynchronous Completion

Compaq recommends that you use the Synchronize (SYS$SYNCH) system service to wait for completion of an asynchronous event. The SYS$SYNCH service correctly waits for the actual completion of an asynchronous event, even if some other event sets the event flag.

To use the SYS$SYNCH service to wait for the completion of an asynchronous event, you must specify both an event flag number and the address of an I/O status block (IOSB) in your call to the asynchronous system service. The asynchronous service queues the request and returns control to your program. When the asynchronous service completes, it sets the event flag and places the final status of the request in the IOSB.

In your call to SYS$SYNCH, you must specify the same efn and I/O status block that you specified in your call to the asynchronous service. The SYS$SYNCH service waits for the event flag to be set by means of the SYS$WAITFR system service. When the specified event flag is set, SYS$SYNCH checks the specified I/O status block. If the I/O status block is nonzero, the system service has completed and SYS$SYNCH returns control to your program. If the I/O status block is zero, SYS$SYNCH clears the event flag by means of the SYS$CLREF service and calls the $WAITFR service to wait for the event flag to be set.

The SYS$SYNCH service sets the event flag before returning control to your program. This ensures that the call to SYS$SYNCH does not interfere with testing for completion of another asynchronous event that completes at approximately the same time and uses the same event flag to signal completion.

The following call to the Queue I/O Request (SYS$QIO) system service demonstrates how the SYS$SYNCH service is used:

       .
       .
       .
    #include <lib$routines.h>
    #include <starlet.h>
            unsigned int status, event_flag = 1;
            struct {
                            short int iostat, iolen;
                            unsigned int dev_info;
    }ttiosb;
       .
       .
       .
    /* Request I/O */
            status = SYS$QIO (event_flag, ..., &ttiosb ...);
            if (!$VMS_STATUS_SUCCESS(status))
                    LIB$SIGNAL( status );
       .
       .
       .
    /* Wait until I/O completes */
            status = SYS$SYNCH (event_flag, &ttiosb );
            if (!$VMS_STATUS_SUCCESS(status))
                    LIB$SIGNAL( status );
       .
       .
       .

Note The SYS$QIOW service provides a combination of SYS$QIO and SYS$SYNCH.

Previous | Next | Contents | Index

| privacy and legal statement |
5841PRO_062.HTML