Calling UNIX/POSIX APIs

You can call standard UNIX/POSIX functions from z/OS® UNIX COBOL programs and from traditional z/OS COBOL programs by using the CALL literal statement. These functions are part of Language Environment®.

About this task

Because these are C functions, you must pass arguments BY VALUE. Pass character strings as BY VALUE pointers that point to null-terminated strings. You must use the compiler options NODYNAM and PGMNAME(LONGMIXED) when you compile programs that call these functions.
Restriction: You cannot use the >>CALLINTERFACE DYNAM directive with these APIs.

You can call the fork(), exec(), and spawn() functions from a COBOL program or from a non-COBOL program in the same process as COBOL programs. However, be aware of these restrictions:

  • From a forked process you cannot access any COBOL sequential, indexed, or relative files that were open when you issued the fork. File status code 92 is returned if you attempt such access (CLOSE, READ, WRITE, REWRITE, DELETE, or START). You can access line-sequential files that were open at the time of a fork.
  • You cannot use the fork() function in a process in which any of the following conditions are true:
    • A COBOL SORT or MERGE is running.
    • A declarative is running.
    • The process has more than one Language Environment enclave (COBOL run unit).
    • The process has used any of the COBOL reusable environment interfaces.
    • The process has ever run a VS COBOL II program.
  • With one exception, DD allocations are not inherited from a parent process to a child process. The exception is the local spawn, which creates a child process in the same address space as the parent process. You request a local spawn by setting the environment variable _BPX_ SHAREAS=YES before you invoke the spawn() function.

The exec() and spawn() functions start a new Language Environment enclave in the new UNIX process. Therefore the target program of the exec() or spawn() function is a main program, and all COBOL programs in the process start in initial state with all files closed.

Sample code for calling some of the POSIX routines is provided in the SIGYSAMP data set.

Table 1. Samples with POSIX function calls
Purpose Sample Functions used
Shows how to use some of the file and directory routines IGYTFL1
  • getcwd()
  • mkdir()
  • rmdir()
  • access()
Shows how to use the iconv routines to convert data IGYTCNV
  • iconv_open()
  • iconv()
  • iconv_close()
Shows the use of the exec() routine to run a new program along with other process-related routines IGYTEXC, IGYTEXC1
  • fork()
  • getpid()
  • getppid()
  • execl()
  • perror()
  • wait()
Shows how to get the errno value IGYTERNO, IGYTGETE
  • perror()
  • fopen()
Shows the use of the interprocess communication message routines IGYTMSQ, IGYTMSQ2
  • ftok()
  • msgget()
  • msgsnd()
  • perror()
  • fopen()
  • fclose()
  • msgrcv()
  • msgctl()
  • perror()

Related references  
XL C/C++ Run-Time Library Reference
UNIX System Services Programming: Assembler Callable Services Reference