It's Time for a Nap

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Many developers use a CPU-intensive DO loop to "delay" an interactive or batch application for an estimated period. This practice is, in a word, wrong! Here's an example routine. Note the "delay" code highlighted in gray. This technique eats up CPU cycles for no other reason than to wait for a given interval. (Please don't try this at home!)

     C                   EXFMT     CUSTMAINT
     C                   DOU       NOT %ERROR()
     C     CUSTNO        CHAIN(E)  CUSTREC 
     C                   if        %ERROR()
     C                   if        %status() = REC_LOCK
     C                   callp     sndstsmsg('Record locked. +
     C                                        Retry in 15 seconds')
     C                   DO        15000             
     C                   eval      x = x + 1
     C                   enddo
     C                   iter
     C                   endif
     C                   endif
     C                   leave
     C                   enddo

Why not just use the DLYJOB CL command? Well, using CL commands in general is becoming more and more taboo lately. I think this is due to two advances:

  • RPG IV's EXTFILE keyword on the File specification eliminates the need for, in my estimate, some 80% of the OVRDBF commands.
  • There isn't much you can do in CL that can't be done in RPG IV using APIs or a good class library such as RPG xTools, and OPNQRYF has been all but replaced by embedded SQL.

The sleep() function suspends a job for a specified number of seconds. If the job receives an end request, such as ENDSBS, ENDJOB, or PWRDWNSYS, the function returns immediately and indicates how many seconds remain in the sleep request.

The sleep API is really just a C runtime function that is callable from RPG IV. In RPG xTools, we've masked the sleep() subprocedure in the DLYJOB() RPG xTools procedure. Actually, xTools uses an MI instruction to provide even more control, but if you want the same behavior as the sleep() API, RPG xTools also includes the sleep() prototype.

To use SLEEP in your own RPG IV program, you need to do two things:

  • Specify the binding directory QC2LE in the BNDDIR keyword on the Header specification in the RPG IV source code. Remember to specify QC2LE in all uppercase letters and enclose it in apostrophes. (See example below.)
  • Include the prototype for the sleep() C runtime function. I've included it at the end of this article, and it is also available on the RPGIV.com downloads page.

There are two versions of sleep, with subtle but important differences:

  • sleep accepts the number of seconds to delay the job.
  • usleep accepts the number of microseconds to delay the job.

Here are the prototypes for the two versions of sleep.

     BNDDIR('QC2LE')
     D sleep           PR            10U 0 ExtProc('sleep')
     D   seconds                     10U 0 value
 
     D usleep          PR            10U 0 ExtProc('usleep')
     D  microSecs                    10U 0 value

 

Using Sleep in RPG IV

The following illustrates using sleep() to wait 15 seconds before retrying a database I/O operation after a record lock is detected.

     C                   EXFMT     CUSTMAINT
     C                   DOU       NOT %ERROR()
     C     CUSTNO        CHAIN(E)  CUSTREC 
     C                   if        %ERROR()
     C                   if        %status() = REC_LOCK
     C                   callp     sndstsmsg('Record locked. +
     C                                        Retrying in 15 seconds')
     C                   callp     sleep(15)
     C                   iter
     C                   endif
     C                   endif
     C                   leave
     C                   enddo

Do the right thing, and stop wasting CPU cycles.

Bob Cozzi is a programmer/consultant, writer/author, and software developer of the RPG xTools, a popular add-on subprocedure library for RPG IV. His book The Modern RPG Language has been the most widely used RPG programming book for nearly two decades. He, along with others, speaks at and runs the highly-popular RPG World conference for RPG programmers.

BOB COZZI

Bob Cozzi is a programmer/consultant, writer/author, and software developer. His popular RPG xTools add-on subprocedure library for RPG IV is fast becoming a standard with RPG developers. His book The Modern RPG Language has been the most widely used RPG programming book for more than a decade. He, along with others, speaks at and produces the highly popular RPG World conference for RPG programmers.


MC Press books written by Robert Cozzi available now on the MC Press Bookstore.

RPG TnT RPG TnT
Get this jam-packed resource of quick, easy-to-implement RPG tips!
List Price $65.00

Now On Sale

The Modern RPG IV Language The Modern RPG IV Language
Cozzi on everything RPG! What more could you want?
List Price $99.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: