3.2. Random access I/O routines

The record-addressable random-access file I/O routines let you generate variable length, individually addressable records. The I/O library updates indexes and pointers.

Each record in a random-access file has a 1-word (64-bit) key or number indicating its position in an index table of records for the file. This index table contains a pointer to the location of the record on the device and can also contain a name of each record within the file.

Alphanumeric record keys increase CPU time compared to sequential integer record keys because the I/O routines must perform a sequential lookup in the index array for each alphanumeric key. Each record should be named a numeric value n; n is the integer that corresponds to the n th record created on the file.

The following two sets of record-addressable random-access file I/O routines are available:

Both synchronous and asynchronous MS and DR I/O can be performed on a random-access file. You can use these routines in the same program, but they must not be used on the same file simultaneously. The MS and DR packages cannot be used for tape files.

If a program uses asynchronous I/O, it must ensure that the data movement is completed before trying to access the data. Because asynchronous I/O has a larger overhead in CPU time than synchronous I/O, only very large data transfers should be done with asynchronous I/O. To increase program speed, the program must be able to do a significant amount of CPU-intensive work or other I/O while the asynchronous I/O is executing.

The MS library routines are used to perform buffered record-addressable random-access I/O. The DR library routines are used to perform unbuffered record-addressable random-access I/O.

These library routines are not internally locked to ensure single-threading; a program must lock each call to the routine if the routine is called from more than one task.

The following list describes these two packages in more detail. For details about the routines discussed in this section, see the individual man pages for each routine.

The following program example uses the MS package:


Example 3-1. MS package use

       program msio
       dimension r(512)
       dimension idx(512)
       data r/512*2.0/
       irflag=0

       call openms(1,idx,100,0,ier)

       do 100 i=1,100
         call writms(1,r,512,i,irflag,0,ier)
         if(ier.ne.0)then
           print *,"error on writms=",ier
           goto 300
         end if
100    continue

       do 200 i=1,100
         call readms(1,r,512,i,irflag,0,ier)
         if(ier.ne.0)then
           print *,"error on readms=",ier
           goto 300
         end if

200    continue
300    continue
       call closms(1,ier)
       end


The following program uses the DR package:


Example 3-2. DR package use

       program daio
       dimension r(512)
       dimension idx(512)
       data r/512*2.0/
       irflag=0
       ierrs=0

       call assign('assign -R',ier1)
       call asnunit(1,'-F mr.save.ovf1:10:200:20',ier2)
       if(ier1.ne.0.or.ier2.ne.0)then
          print *,"assign error=",ier1,ier2
          ierrs=ierrs+1
       end if

       call opendr(1,idx,100,0,ier)
       if(ier.ne.0)then
         print *,"error on opendr=",ier
         ierrs=ierrs+1
       end if

       do 100 i=1,100
         call writdr(1,r,512,i,irflag,0,ier)
         if(ier.ne.0)then
           print *,"error on writdr=",ier
           ierrs=ierrs+1
         end if
100    continue
       do 200 i=1,100
         call readdr(1,r,512,i,irflag,0,ier)
         if(ier.ne.0)then
            print *,"error on readdr=",ier
            ierrs=ierrs+1
         end if
200    continue
300    call closdr(1,ier)
       if(ier.ne.0)then
         print *,"error on readdr=",ier
         ierrs=ierrs+1
       end if
400    continue
       if(ierrs.eq.0)then
         print *,"daio passed"
       else
         print *,"daio failed"
       end if
       end