IBM Support

Example of Using the C API usleep()

Troubleshooting


Problem

This document shows how to use the C API usleep() to delay a job for less than a second.

Resolving The Problem

This document shows how to use the C API usleep() to delay a job for less than a second.

The function usleep() is a C API that suspends the current process for the number of microseconds passed to it. It can be used for delaying a job. DLYJOB works well if you are looking to delay a job for more than a second. If you need to delay the job for less than a second, however, you must use the usleep() API. The usleep() API is contained in the service program QSYS/QP0SSRV1. The prototype is as follows:

int usleep( useconds_t useconds );  

Where useconds_t is of type unsigned integer. It returns an integer; 0 if successful, -1 if unsuccessful. It can accept anywhere from 0 to 999999 microseconds.

Here are some examples of coding the API in CLLE, RPGLE, and C. In these examples, the job is delayed for 500,000 microseconds, or 0.5 seconds.

Caution: The following code snippets are examples only. IBM accepts no responsibility for their correctness.
    • - CLLE



      PGM
                   DCL        VAR(&VALUE) TYPE(*UINT) LEN(4) VALUE(500000)
                   DCL        VAR(&RETURNED) TYPE(*INT) LEN(4)
                   CALLPRC    PRC('usleep') PARM((&VALUE *BYVAL)) +
                                RTNVAL(&RETURNED)
      ENDPGM 


      Name the source member USLEEP in the source physical file QCLLESRC. Compile using the following statements:

      CRTCLMOD MODULE(USLEEP) SRCFILE(QCLLESRC)
      CRTPGM PGM(USLEEP) BNDSRVPGM((QP0SSRV1))

      • - RPGLE




         DUSleep           PR            10I 0 Extproc('usleep')
             D MSeconds                      10U 0 Value

             DMSeconds         S             10U 0 Inz(500000)
             DreturnStatus     S             10I 0

              /Free

                returnStatus = USleep(MSeconds);
                *inLr = *on;
              /End-Free 

        Name the source member USLEEP in the source physical file QRPGLESRC. Compile using the following statements:

        CRTRPGMOD MODULE(USLEEP) SRCFILE(QRPGLESRC)
        CRTPGM PGM(USLEEP) BNDSRVPGM((QP0SSRV1))

        • - C



          #include <unistd.h>

          void main()
          {
              unsigned int mSeconds = 500000;
              int returnCode;

              returnCode = usleep(mSeconds);
              return;
          } 

          Name the source member USLEEP in the source physical file QCSRC. Compile using the following statements:

          CRTCMOD MODULE(USLEEP) SRCFILE(QCSRC)
          CRTPGM PGM(USLEEP)

        [{"Type":"MASTER","Line of Business":{"code":"LOB57","label":"Power"},"Business Unit":{"code":"BU058","label":"IBM Infrastructure w\/TPS"},"Product":{"code":"SWG60","label":"IBM i"},"Platform":[{"code":"PF012","label":"IBM i"}],"Version":"7.1.0"}]

        Historical Number

        560965705

        Document Information

        Modified date:
        18 December 2019

        UID

        nas8N1012283