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, ILE RPG IV, and ILE/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))

  • - ILE RPG IV 
    **FREE
    Dcl-Pr USleep Int(10) Extproc('usleep');
         MSeconds        Uns(10)         Value;
    End-Pr;
    
    Dcl-S MSeconds        Uns(10)         Inz(500000);
    Dcl-S returnStatus    Int(10);
         returnStatus = USleep(MSeconds);
         *inLr = *on;   


    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))

  • - ILE/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":"LOB68","label":"Power HW"},"Business Unit":{"code":"BU070","label":"IBM Infrastructure"},"Product":{"code":"SWG60","label":"IBM i"},"ARM Category":[{"code":"a8m3p000000hB4rAAE","label":"API"},{"code":"a8m0z0000000CHtAAM","label":"Programming ILE Languages"}],"ARM Case Number":"","Platform":[{"code":"PF012","label":"IBM i"}],"Version":"All Versions"}]

Historical Number

560965705

Document Information

More support for:
IBM i

Component:
API, Programming ILE Languages

Software version:
All Versions

Operating system(s):
IBM i

Document number:
686545

Modified date:
21 November 2024

UID

nas8N1012283

Manage My Notification Subscriptions