COBOL Tutorial

Progress0 of 0 lessons

COBOL System Programming

COBOL system programming involves techniques for writing COBOL programs that effectively interact with mainframe system services, manage memory efficiently, optimize file operations, and achieve optimal performance. While COBOL is primarily designed for business applications, understanding system programming concepts enables you to create efficient, well-performing applications that work seamlessly with the mainframe operating system and leverage system-level features.

Understanding COBOL System Programming

System programming in COBOL focuses on optimizing how your programs interact with the mainframe operating system, manage system resources, and achieve optimal performance. Key aspects include:

  • Memory Management: Efficient use of storage sections and memory optimization techniques
  • File Optimization: Optimizing I/O operations through proper blocking and file organization
  • System Interfaces: Interfacing with system services, control blocks, and system APIs
  • Performance Tuning: Techniques for improving program efficiency and resource utilization
  • Resource Management: Proper management of system resources and avoiding resource exhaustion

Memory Management in COBOL

Effective memory management is crucial for system programming. COBOL provides three main storage sections, each serving different purposes in program design and execution.

COBOL Storage Sections
Storage SectionPurposeInitializationCommon Use Case
WORKING-STORAGE SECTIONVariables that persist throughout program executionInitialized once at program startProgram-level variables, constants, work areas
LOCAL-STORAGE SECTIONVariables reinitialized on each program invocationReinitialized each time program is calledTemporary work areas, reentrant program data
LINKAGE SECTIONParameters passed from other programs or systemsPassed from calling programProgram parameters, system interfaces, shared data

WORKING-STORAGE SECTION

The WORKING-STORAGE SECTION is used for variables that need to persist throughout the program's execution. These variables are initialized once when the program starts and retain their values across procedure calls within the same program execution.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
DATA DIVISION. WORKING-STORAGE SECTION. *> Program-level variables that persist 01 WS-PROGRAM-CONTROL. 05 WS-PROGRAM-NAME PIC X(8) VALUE 'SYSPROG'. 05 WS-EXECUTION-COUNT PIC 9(6) VALUE ZERO. 05 WS-START-TIME PIC X(8). 05 WS-END-TIME PIC X(8). *> Constants and configuration 01 WS-CONSTANTS. 05 WS-MAX-RECORDS PIC 9(6) VALUE 100000. 05 WS-TIMEOUT-VALUE PIC 9(4) VALUE 3000. 05 WS-SUCCESS-CODE PIC X(2) VALUE '00'. *> Work areas that persist across calls 01 WS-WORK-AREAS. 05 WS-ACCUMULATOR PIC S9(15)V99 COMP-3 VALUE ZERO. 05 WS-RECORD-COUNT PIC 9(8) VALUE ZERO. 05 WS-ERROR-COUNT PIC 9(6) VALUE ZERO.

Key Points:

  • Variables are initialized once at program start
  • Values persist across all procedure calls in the program
  • Use for program-level state, counters, accumulators, and configuration
  • Memory is allocated for the entire program execution

LOCAL-STORAGE SECTION

The LOCAL-STORAGE SECTION is used for variables that should be reinitialized each time the program is invoked. This is particularly important for reentrant programs and when you need fresh values on each program call.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
DATA DIVISION. LOCAL-STORAGE SECTION. *> Variables reinitialized on each program call 01 LS-TEMPORARY-WORK. 05 LS-TEMP-BUFFER PIC X(1000). 05 LS-CALCULATION-AREA PIC S9(15)V99 COMP-3. 05 LS-INDEX PIC 9(4). 05 LS-SUB PIC 9(4). *> Reentrant program data 01 LS-PROCESSING-DATA. 05 LS-INPUT-RECORD PIC X(200). 05 LS-OUTPUT-RECORD PIC X(200). 05 LS-PROCESSING-FLAG PIC X VALUE 'N'. 88 LS-PROCESSING VALUE 'Y'. 88 LS-NOT-PROCESSING VALUE 'N'.

Key Points:

  • Variables are reinitialized each time the program is called
  • Essential for reentrant programs that can be called concurrently
  • Use for temporary work areas that should start fresh on each call
  • Prevents data contamination between program invocations

LINKAGE SECTION

The LINKAGE SECTION defines parameters and data structures that are passed to the program from other programs or systems. This section does not allocate memory; it describes the structure of data passed from the caller.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
DATA DIVISION. LINKAGE SECTION. *> Parameters passed from calling program 01 LK-PROGRAM-PARAMETERS. 05 LK-FUNCTION-CODE PIC X. 88 LK-READ-FUNCTION VALUE 'R'. 88 LK-WRITE-FUNCTION VALUE 'W'. 05 LK-RECORD-KEY PIC X(20). 05 LK-RECORD-LENGTH PIC 9(4) COMP. 05 LK-RETURN-CODE PIC 9(2). *> System interface structure 01 LK-SYSTEM-INTERFACE. 05 LK-COMMAND PIC X(8). 05 LK-PARAMETER-COUNT PIC 9(2). 05 LK-PARAMETER-AREA PIC X(200). 05 LK-RESPONSE-CODE PIC 9(4). PROCEDURE DIVISION USING LK-PROGRAM-PARAMETERS. MAIN-PROCEDURE. *> Use parameters passed from caller IF LK-READ-FUNCTION PERFORM READ-RECORD ELSE IF LK-WRITE-FUNCTION PERFORM WRITE-RECORD END-IF *> Set return code for caller MOVE 00 TO LK-RETURN-CODE GOBACK.

Key Points:

  • No memory is allocated by the program
  • Describes the structure of data passed from the caller
  • Used for program-to-program communication
  • Essential for system interfaces and API implementations

Memory Optimization Techniques

COBOL provides several clauses and techniques for optimizing memory usage and improving program efficiency. Understanding these techniques is essential for system programming.

Memory Optimization Techniques
TechniquePurposeBenefitUse Case
REDEFINES ClauseShare memory location between data itemsMemory optimization, multiple data interpretationsControl block access, data parsing, memory reuse
SYNCHRONIZED ClauseAlign data on natural memory boundariesImproved access efficiency, proper alignmentPerformance optimization, system structure alignment
USAGE ClauseSpecify data storage formatStorage efficiency, processing optimizationBinary data, packed decimal, computational efficiency
BLOCK CONTAINSOptimize file blockingReduced I/O operations, improved performanceLarge file processing, batch operations

REDEFINES Clause

The REDEFINES clause allows multiple data items to share the same memory location, enabling different interpretations of the same data. This is particularly useful for accessing system control blocks and optimizing memory usage.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
DATA DIVISION. WORKING-STORAGE SECTION. *> System control block structure 01 WS-CONTROL-BLOCK. 05 WS-BLOCK-HEADER. 10 WS-BLOCK-TYPE PIC X(4). 10 WS-BLOCK-LENGTH PIC 9(4) COMP. 10 WS-BLOCK-FLAGS PIC X(2). 05 WS-BLOCK-DATA PIC X(100). *> Redefine to access as raw bytes 01 WS-RAW-BYTES REDEFINES WS-CONTROL-BLOCK. 05 WS-BYTE-ARRAY PIC X OCCURS 110 TIMES INDEXED BY WS-BYTE-INDEX. *> Redefine to access as binary 01 WS-BINARY-VIEW REDEFINES WS-CONTROL-BLOCK. 05 WS-BINARY-HEADER. 10 WS-BIN-TYPE PIC 9(8) COMP. 10 WS-BIN-LENGTH PIC 9(8) COMP. 10 WS-BIN-FLAGS PIC 9(4) COMP. 05 WS-BIN-DATA PIC X(100). PROCEDURE DIVISION. PROCESS-CONTROL-BLOCK. *> Access as structured data MOVE 'CTRL' TO WS-BLOCK-TYPE MOVE 110 TO WS-BLOCK-LENGTH *> Access as raw bytes for system operations PERFORM VARYING WS-BYTE-INDEX FROM 1 BY 1 UNTIL WS-BYTE-INDEX > 110 *> Process each byte IF WS-BYTE-ARRAY(WS-BYTE-INDEX) = X'00' *> Handle null byte END-IF END-PERFORM *> Access as binary for calculations COMPUTE WS-BIN-LENGTH = WS-BIN-LENGTH + 10.

Important Considerations:

  • REDEFINES items share the same memory location
  • Changes to one view affect all views of the same data
  • Use carefully to avoid unintended data corruption
  • Excellent for system-level data structure manipulation

SYNCHRONIZED Clause

The SYNCHRONIZED clause ensures that data items are aligned on natural memory boundaries, which can improve access efficiency on certain hardware architectures. This is important for performance optimization and proper data alignment when interfacing with system-level structures.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
DATA DIVISION. WORKING-STORAGE SECTION. *> Synchronized data for proper alignment 01 WS-ALIGNED-DATA. 05 WS-ALIGNED-FIELD-1 PIC S9(9) COMP SYNC. 05 WS-ALIGNED-FIELD-2 PIC S9(9) COMP SYNC. 05 WS-ALIGNED-FIELD-3 PIC S9(9) COMP SYNC. *> Non-synchronized for comparison 01 WS-NON-ALIGNED-DATA. 05 WS-NON-ALIGNED-1 PIC S9(9) COMP. 05 WS-NON-ALIGNED-2 PIC S9(9) COMP. 05 WS-NON-ALIGNED-3 PIC S9(9) COMP. *> System structure with alignment 01 WS-SYSTEM-STRUCTURE. 05 WS-STRUCT-ID PIC 9(4) COMP SYNC. 05 WS-STRUCT-LENGTH PIC 9(4) COMP SYNC. 05 WS-STRUCT-POINTER PIC S9(9) COMP SYNC. 05 WS-STRUCT-FLAGS PIC X(4) SYNC.

Benefits of SYNCHRONIZED:

  • Improves access efficiency on certain hardware
  • Ensures proper alignment for system structures
  • Can improve performance for computational fields
  • Important when interfacing with system-level data

File Handling Optimization

Optimizing file operations is crucial for system programming. Proper file blocking, organization, and I/O techniques can significantly improve program performance.

BLOCK CONTAINS Clause

The BLOCK CONTAINS clause defines the number of records per block or the size of each block, which can significantly impact I/O performance by reducing the number of I/O operations.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT INPUT-FILE ASSIGN TO 'INPUT.DATA' ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL FILE STATUS IS WS-FILE-STATUS. DATA DIVISION. FILE SECTION. FD INPUT-FILE RECORDING MODE IS F RECORD CONTAINS 100 CHARACTERS BLOCK CONTAINS 10 RECORDS. *> 10 records per block *> Alternative: BLOCK CONTAINS 1000 CHARACTERS 01 INPUT-RECORD PIC X(100). WORKING-STORAGE SECTION. 01 WS-FILE-STATUS PIC X(2). 88 WS-FILE-OK VALUE '00'. 88 WS-FILE-EOF VALUE '10'. PROCEDURE DIVISION. MAIN-PROCEDURE. OPEN INPUT INPUT-FILE PERFORM UNTIL WS-FILE-EOF READ INPUT-FILE AT END SET WS-FILE-EOF TO TRUE NOT AT END PERFORM PROCESS-RECORD END-READ END-PERFORM CLOSE INPUT-FILE STOP RUN. PROCESS-RECORD. *> Process each record DISPLAY 'Processing: ' INPUT-RECORD(1:50).

Blocking Considerations:

  • Larger blocks reduce I/O operations but use more memory
  • Optimal block size depends on record size and system configuration
  • Consider system buffer sizes and available memory
  • Test different block sizes to find optimal performance

RECORDING MODE

The RECORDING MODE clause specifies the format of records in a file, which affects how records are stored and accessed.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
FILE SECTION. *> Fixed-length records FD FIXED-FILE RECORDING MODE IS F RECORD CONTAINS 200 CHARACTERS BLOCK CONTAINS 20 RECORDS. 01 FIXED-RECORD PIC X(200). *> Variable-length records FD VARIABLE-FILE RECORDING MODE IS V RECORD CONTAINS 50 TO 500 CHARACTERS BLOCK CONTAINS 10 RECORDS. 01 VARIABLE-RECORD PIC X(500). *> Undefined records (for special cases) FD UNDEFINED-FILE RECORDING MODE IS U BLOCK CONTAINS 1000 CHARACTERS. 01 UNDEFINED-RECORD PIC X(1000).

Recording Mode Options:

  • F (Fixed): All records have the same length
  • V (Variable): Records can have different lengths
  • U (Undefined): Records have no predefined structure

System Interfaces and Services

COBOL programs can interface with system services through various mechanisms, enabling interaction with system-level functions and control blocks.

Calling System Services

COBOL programs can call system services and Assembly routines to perform system-level operations.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-SYSTEM-CALL. 05 WS-FUNCTION-CODE PIC X(8) VALUE 'GETTIME'. 05 WS-PARAMETER-COUNT PIC 9(2) VALUE 01. 05 WS-PARAMETER-AREA PIC X(100). 05 WS-RETURN-CODE PIC 9(4) COMP. 01 WS-SYSTEM-TIME. 05 WS-CURRENT-DATE PIC X(8). 05 WS-CURRENT-TIME PIC X(8). LINKAGE SECTION. 01 LK-SYSTEM-INTERFACE. 05 LK-COMMAND PIC X(8). 05 LK-PARAM-COUNT PIC 9(2). 05 LK-PARAM-AREA PIC X(100). 05 LK-RETURN-CODE PIC 9(4) COMP. PROCEDURE DIVISION. CALL-SYSTEM-SERVICE. *> Call system service to get current time MOVE 'GETTIME' TO WS-FUNCTION-CODE MOVE 1 TO WS-PARAMETER-COUNT MOVE WS-SYSTEM-TIME TO WS-PARAMETER-AREA CALL 'SYSTEM_SERVICE' USING WS-SYSTEM-CALL IF WS-RETURN-CODE = 0 DISPLAY 'Current Date: ' WS-CURRENT-DATE DISPLAY 'Current Time: ' WS-CURRENT-TIME ELSE DISPLAY 'Error calling system service: ' WS-RETURN-CODE END-IF.

Performance Optimization Best Practices

Effective system programming requires attention to performance optimization. Consider these best practices:

Memory Management

  • Use appropriate storage sections for different data needs
  • Minimize WORKING-STORAGE for large arrays that are only used temporarily
  • Use LOCAL-STORAGE for reentrant programs
  • Leverage REDEFINES for memory optimization when appropriate
  • Consider SYNCHRONIZED for performance-critical computational fields

File Operations

  • Optimize block sizes based on record size and system configuration
  • Choose appropriate file organization (Sequential, Indexed, Relative)
  • Minimize file opens and closes
  • Use appropriate access modes (Sequential, Random, Dynamic)
  • Consider buffering strategies for large file operations

System Calls

  • Minimize system calls and batch them when possible
  • Cache system information when appropriate
  • Handle system call errors properly
  • Use efficient system interfaces
  • Document system dependencies clearly

Exercises

Practice these exercises to reinforce your understanding of COBOL system programming:

Exercise 1: Memory Section Analysis

Analyze a COBOL program and identify which data items should be in:

  • WORKING-STORAGE SECTION (persistent program data)
  • LOCAL-STORAGE SECTION (reinitialized on each call)
  • LINKAGE SECTION (parameters from caller)

Exercise 2: File Blocking Optimization

For a file with 100-byte fixed records:

  • Calculate optimal block size for different scenarios
  • Consider system buffer sizes and memory constraints
  • Test different blocking strategies

Exercise 3: REDEFINES Implementation

Create a data structure that can be accessed as:

  • A structured control block with named fields
  • An array of bytes for raw manipulation
  • A binary view for computational operations

Summary

COBOL system programming enables you to create efficient, well-performing applications that work effectively with the mainframe operating system. Key points to remember:

  • Use WORKING-STORAGE for persistent program data, LOCAL-STORAGE for reentrant programs, and LINKAGE for parameters
  • REDEFINES allows multiple data interpretations of the same memory location
  • SYNCHRONIZED ensures proper data alignment for performance optimization
  • BLOCK CONTAINS optimizes file I/O by reducing the number of I/O operations
  • RECORDING MODE (F, V, U) affects how records are stored and accessed
  • System services can be accessed through Assembly routines and system APIs
  • Performance optimization requires attention to memory, file operations, and system calls
  • Best practices include proper error handling, documentation, and testing

Understanding COBOL system programming techniques helps you create efficient applications that make optimal use of system resources and achieve excellent performance in mainframe environments.

Test Your Knowledge

1. What is the primary purpose of COBOL system programming?

  • To write operating system code
  • To enable COBOL programs to interact with system services, manage memory efficiently, and optimize performance
  • To convert COBOL to other languages
  • To simplify COBOL syntax

2. Which COBOL section is used for variables that persist throughout program execution?

  • LINKAGE SECTION
  • WORKING-STORAGE SECTION
  • LOCAL-STORAGE SECTION
  • FILE SECTION

3. What does the REDEFINES clause allow in COBOL?

  • To rename data items
  • To allow multiple data items to share the same memory location
  • To redefine program structure
  • To change data types

4. How do COBOL programs typically access system control blocks?

  • Directly through COBOL statements
  • Indirectly by calling Assembly routines or using system services
  • Through SQL statements
  • They cannot access control blocks

5. What is the purpose of the BLOCK CONTAINS clause?

  • To define record structure
  • To define the number of records per block or block size for I/O optimization
  • To specify file organization
  • To set record length

Related Pages