              <<< VAXAXP::NOTES$:[NOTES$LIBRARY]VMSNOTES.NOTE;1 >>>
               -< VAX and Alpha VMS - Digital Internal Use Only >-
================================================================================
Note 1428.4          Locking memory from within a C program               4 of 4
GREGOR::OPP                                          57 lines  28-AUG-1996 10:44
                             -< A partial example >-
--------------------------------------------------------------------------------
        Excerpts from a program that locks pages into memory for OpenVMS
     are attached.  (This program contains equivalent instructions for 
     Windows NT and UNIX, which is the reason for preprocessor directives.)
     By calling sys$lckpag using lib$signal you can reduce your chances of
     hanging the system due to the consumption of physical memory.  The 
     page size code is not related to the locking; it is helpful if the 
     program needs to run on VAX and Alpha systems.

     Greg


#include <descrip.h>            // argument descriptor definitions
#include <ssdef.h>              // system services messages
#include <starlet.h>            // VMS system services library
#include <lib$routines.h>       // signal routines library
#include <syidef.h>             // GETSYI symbol defs

~       ~       ~       ~       ~       ~       ~       ~       ~       ~

const int MSIZE = 2032 * 2032 ;         // memory array size in 32-bit words;

int main (void)

   static unsigned int memory[MSIZE] ;  // single-subscript array in memory
                           
// Need integer array to hold addresses of beginning and end of the
// arrays for use by OpenVMS system service $LCKPAG.

#ifdef VMS
   int lock_pages[2] ;                  // specific to OpenVMS for WSLock
   int page_size ;                      // page size in bytes, VMS only
   int page_size_item ;                 // SYI$_PAGE_SIZE item code equiv.
#endif

~       ~       ~       ~       ~       ~       ~       ~       ~       ~

// Initialize array "pointer" for $LCKPAG system service.

#ifdef VMS
    lock_pages[0] = (int) &memory[0] ;          // WNT uses VirtualLock
    lock_pages[1] = (int) &memory[MSIZE] ;      // code above.

    page_size_item = SYI$_PAGE_SIZE ;           // assign from defined symbol
    lib$getsyi (&page_size_item, &page_size, 0, 0, 0, 0) ;
                                                // get the page size
#endif

~       ~       ~       ~       ~       ~       ~       ~       ~       ~

// Try to lock the arrays into working set memory using $LCKPAG system
// service.

#ifdef VMS
   lib$signal (sys$lckpag (lock_pages, 0, 0)) ;   // OpenVMS specific
#endif

    
