TExcelTAD
Home › COBOL › COBOL Mainframe Ecosystem

COBOL Mainframe Ecosystem

Most production COBOL runs on IBM Z mainframes under the z/OS operating system. There, a COBOL program is one piece of a larger environment: JCL starts batch jobs, CICS runs online transactions, DB2 and VSAM store the data. You don't need a mainframe to learn COBOL, but you need to recognise these pieces to work on real systems. This lesson is a guided tour; the code in non-run blocks is what you would see on the job.

Batch and online

Batch Online
Started by A scheduler (e.g. Control-M, TWS) submitting JCL A user or system sending a transaction
Runs Minutes to hours, often at night Milliseconds per transaction
Typical work Interest calculation, statements, payroll, file transfers Balance enquiry, payment, policy update
Middleware JCL, DFSORT CICS (or IMS TM)
Output Files, reports, return codes Screens, messages, database updates

JCL: Job Control Language

JCL tells z/OS which programs to run, in which order, and which data sets (files) each program's ASSIGN names (DD names) refer to. A two-step job that sorts transactions and then runs a COBOL program:

jcl
//PAYJOB   JOB (ACCT),'NIGHTLY PAYROLL',CLASS=A,MSGCLASS=X
//*
//SORTSTEP EXEC PGM=SORT
//SORTIN   DD DSN=PROD.PAYROLL.TRANS,DISP=SHR
//SORTOUT  DD DSN=&&SORTED,DISP=(NEW,PASS),SPACE=(CYL,(5,5))
//SYSIN    DD *
  SORT FIELDS=(1,8,CH,A)
/*
//PAYSTEP  EXEC PGM=PAYROLL,COND=(4,LT)
//STEPLIB  DD DSN=PROD.LOADLIB,DISP=SHR
//TRANSIN  DD DSN=&&SORTED,DISP=(OLD,DELETE)
//PAYRPT   DD SYSOUT=*
//SYSOUT   DD SYSOUT=*
  • EXEC PGM=PAYROLL runs the compiled COBOL program from the load library in STEPLIB.
  • TRANSIN and PAYRPT match SELECT … ASSIGN TO TRANSIN and ASSIGN TO PAYRPT in the program.
  • COND=(4,LT) skips the step if an earlier step ended with a return code greater than 4.
  • DISPLAY output goes to SYSOUT.

Return codes

A COBOL program reports success or failure to JCL through the RETURN-CODE special register. By convention 0 is success, 4 a warning, 8 an error, 12 or 16 a severe error. Later steps and the scheduler decide what to do next:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. VALIDATE-BATCH.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-RECORDS-READ        PIC 9(5)    VALUE 1250.
       01  WS-RECORDS-REJECTED    PIC 9(5)    VALUE 3.
       PROCEDURE DIVISION.
           DISPLAY "Records read:     " WS-RECORDS-READ
           DISPLAY "Records rejected: " WS-RECORDS-REJECTED
           EVALUATE TRUE
               WHEN WS-RECORDS-REJECTED = 0
                   MOVE 0 TO RETURN-CODE
               WHEN WS-RECORDS-REJECTED < 10
                   MOVE 4 TO RETURN-CODE
               WHEN OTHER
                   MOVE 8 TO RETURN-CODE
           END-EVALUATE
           DISPLAY "Ending with return code " RETURN-CODE
           GOBACK.
Records read:     01250
Records rejected: 00003
Ending with return code +000000004

On Linux, RETURN-CODE becomes the process exit status (echo $? prints 4).

CICS: online transactions

CICS (Customer Information Control System) runs thousands of short transactions per second. A CICS program is a COBOL program with embedded EXEC CICS … END-EXEC commands, which a translator turns into calls before compilation. Programs are usually pseudo-conversational: they send a screen, end, and are started again when the user presses Enter, with their state kept in a COMMAREA or a channel.

cobol
           EXEC CICS RECEIVE MAP('ACCTMAP') MAPSET('ACCTSET')
                INTO(ACCTMAPI)
                RESP(WS-RESP)
           END-EXEC

           EXEC CICS READ FILE('ACCTFILE')
                INTO(ACCOUNT-RECORD)
                RIDFLD(ACCT-NUMBER)
                RESP(WS-RESP)
           END-EXEC

           IF WS-RESP = DFHRESP(NOTFND)
               MOVE 'ACCOUNT NOT FOUND' TO MSGO
           END-IF

           EXEC CICS SEND MAP('ACCTMAP') MAPSET('ACCTSET')
                FROM(ACCTMAPO) ERASE
           END-EXEC
           EXEC CICS RETURN TRANSID('ACCT')
                COMMAREA(WS-COMMAREA)
           END-EXEC

DB2: embedded SQL

DB2 is IBM's relational database. COBOL programs embed SQL between EXEC SQL and END-EXEC, using host variables (COBOL data items prefixed with :). After each statement, SQLCODE tells you the outcome: 0 success, 100 no row found, negative values errors.

cobol
           EXEC SQL
               SELECT CUST_NAME, BALANCE
                 INTO :WS-CUST-NAME, :WS-BALANCE
                 FROM CUSTOMER
                WHERE CUST_ID = :WS-CUST-ID
           END-EXEC

           EVALUATE SQLCODE
               WHEN 0     DISPLAY 'Found ' WS-CUST-NAME
               WHEN 100   DISPLAY 'No such customer'
               WHEN OTHER DISPLAY 'DB2 error ' SQLCODE
           END-EVALUATE

Reading many rows uses a cursor (DECLARE, OPEN, FETCH in a loop until SQLCODE = 100, CLOSE). The same SQLCODE logic can be practised without a database. This program imitates a keyed lookup against an in-memory table and sets an SQLCODE-style result:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. SQLCODE-STYLE.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-CUSTOMER-DATA.
           05  FILLER  PIC X(20) VALUE "100Ana Silva 0152075".
           05  FILLER  PIC X(20) VALUE "200Ben Okoro 0008810".
       01  WS-CUSTOMERS REDEFINES WS-CUSTOMER-DATA.
           05  WS-CUST OCCURS 2 TIMES INDEXED BY C-IDX.
               10  T-CUST-ID      PIC 999.
               10  T-CUST-NAME    PIC X(10).
               10  T-BALANCE      PIC 9(5)V99.
       01  SQLCODE                PIC S9(3).
       01  WS-CUST-ID             PIC 999.
       01  WS-CUST-NAME           PIC X(10).
       01  WS-BALANCE             PIC 9(5)V99.
       PROCEDURE DIVISION.
           MOVE 200 TO WS-CUST-ID
           PERFORM SELECT-CUSTOMER
           MOVE 300 TO WS-CUST-ID
           PERFORM SELECT-CUSTOMER
           STOP RUN.
       SELECT-CUSTOMER.
           SET C-IDX TO 1
           SEARCH WS-CUST
               AT END MOVE 100 TO SQLCODE
               WHEN T-CUST-ID (C-IDX) = WS-CUST-ID
                   MOVE T-CUST-NAME (C-IDX) TO WS-CUST-NAME
                   MOVE T-BALANCE (C-IDX) TO WS-BALANCE
                   MOVE 0 TO SQLCODE
           END-SEARCH
           EVALUATE SQLCODE
               WHEN 0
                   DISPLAY WS-CUST-ID ": " WS-CUST-NAME " " WS-BALANCE
               WHEN 100
                   DISPLAY WS-CUST-ID ": no such customer (SQLCODE 100)"
           END-EVALUATE.
200: Ben Okoro  00088.10
300: no such customer (SQLCODE 100)

VSAM and data sets

Mainframe files are data sets with names like PROD.PAYROLL.TRANS. Sequential data sets hold fixed or variable-length records. VSAM (Virtual Storage Access Method) provides keyed files: a KSDS (key-sequenced data set) is what a COBOL program opens as ORGANIZATION IS INDEXED, an RRDS as RELATIVE, and an ESDS as SEQUENTIAL. VSAM files are defined with the IDCAMS utility:

jcl
//DEFINE   EXEC PGM=IDCAMS
//SYSPRINT DD SYSOUT=*
//SYSIN    DD *
  DEFINE CLUSTER (NAME(PROD.CUSTOMER.MASTER) -
                  INDEXED KEYS(8 0) RECORDSIZE(100 100))
/*

EBCDIC

Mainframes use the EBCDIC character set, not ASCII. The digit 1 is X"F1" instead of X"31", and letters sort before digits. Programs that compare or sort mixed text can behave differently after a migration to Linux, and files must be converted when they move between platforms (packed and binary fields must not be converted byte by byte).

Character ASCII EBCDIC
A 41 C1
a 61 81
1 31 F1
space 20 40

Tools of the trade

  • TSO/ISPF: the classic green-screen editor and file browser.
  • SDSF: view job output and return codes.
  • Endevor or ChangeMan: source control and promotion between environments.
  • IBM Developer for z/OS, VS Code with Zowe and IBM Z Open Editor: modern IDEs connected to the mainframe.

Test yourself

What connects a COBOL ASSIGN TO TRANSIN to a real data set on z/OS?

  1. The FD entry
  2. A //TRANSIN DD DSN=… statement in the JCL
  3. The PROGRAM-ID
  4. A CICS command

What does SQLCODE = 100 mean after a DB2 SELECT or FETCH?

  1. Success
  2. No (more) rows found
  3. A syntax error
  4. A deadlock

Why can sorting give different results after moving COBOL from a mainframe to Linux?

  1. Linux sorts randomly
  2. EBCDIC and ASCII order letters and digits differently
  3. COBOL cannot sort on Linux
  4. Packed decimal is unsupported