MainframeMaster

COBOL Resource Management

COBOL resource management is the practice of acquiring and releasing resources—especially files—in a controlled way. You OPEN a file before performing I/O and CLOSE it when done so that buffers are flushed, data is consistent, and system resources (buffers, control blocks, dataset access) are released. This page explains OPEN and CLOSE, access modes, the file lifecycle, and why proper resource management matters in batch and CICS.

Explain Like I'm Five: What Is Resource Management?

Think of a file as a box you borrow from the system. Before you can take things out or put things in, you have to "open" the box (OPEN). When you are done, you have to "close" the box (CLOSE) and give it back so the system can save any last changes and use the box for someone else. If you never close it, your changes might not be saved and the system runs out of boxes. Resource management is the habit of always opening before use and closing when done.

The File Lifecycle: OPEN, I/O, CLOSE

Every file used in a COBOL program follows a lifecycle. First you OPEN the file with a mode that matches what you will do (read, write, or both). Then you perform READ, WRITE, REWRITE, or DELETE as needed. Finally you CLOSE the file. Until OPEN, no I/O is valid for that file. After CLOSE, no I/O is valid until you OPEN again. The system allocates buffers and control blocks at OPEN and releases them at CLOSE. Skipping CLOSE can leave data in memory unwritten and can exhaust file resources in regions that run many tasks.

OPEN Statement and Access Modes

The OPEN statement connects the program to the file (dataset). You specify one or more files and an access mode for each. INPUT means the file is read-only: you can only READ. Use it when you are reading an existing file and will not change it. OUTPUT means the file is write-only: you can only WRITE. The file is created if it does not exist or is overwritten if it does (depending on JCL and environment). Use OUTPUT for new files or when you are building a new version. I-O (input-output) means the file is open for both reading and updating: you can READ, REWRITE, and DELETE (and sometimes WRITE for add). Use I-O when you need to update existing records (e.g. direct access). EXTEND means you will only add records to the end (WRITE); the file is created if it does not exist. Use EXTEND for appending (e.g. log files).

OPEN access modes
ModeAllowed operationsTypical use
INPUTREAD onlyRead existing file; file must exist.
OUTPUTWRITE onlyCreate new file or overwrite; file may be created or replaced.
I-OREAD, WRITE, REWRITE, DELETEUpdate existing file (direct access); file must exist for update.
EXTENDWRITE (append)Add records to end; creates file if it does not exist.
cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
PROCEDURE DIVISION. OPEN INPUT INFILE OUTPUT OUTFILE IF FS-INFILE NOT = '00' DISPLAY 'OPEN INFILE FAILED ' FS-INFILE STOP RUN END-IF IF FS-OUTFILE NOT = '00' DISPLAY 'OPEN OUTFILE FAILED ' FS-OUTFILE STOP RUN END-IF PERFORM UNTIL EOF READ INFILE ... ... WRITE OUTFILE-REC ... END-PERFORM CLOSE INFILE OUTFILE STOP RUN.

You can OPEN several files in one statement (OPEN INPUT A B C OUTPUT D). Always check file status after OPEN (and after CLOSE if you need to detect close errors). If OPEN fails, do not perform I/O on that file.

CLOSE Statement: Releasing Resources

CLOSE terminates the connection to the file. The runtime flushes any buffered data to the dataset so that all WRITEs and REWRITEs are persisted. It releases buffers, channel programs, and control blocks so the system can reuse them. In CICS, not closing files can hold FCT entries and cause resource shortages. In batch, not closing can leave the last blocks unwritten. You can CLOSE multiple files in one statement: CLOSE INFILE OUTFILE. You can specify CLOSE with REEL/UNIT for tape (or equivalent) if your environment requires it. After CLOSE, the file is no longer available for I/O until you OPEN it again. If the program exits normally, the runtime may close open files automatically in some environments, but you should not rely on that; always close explicitly when your logic is done with the file.

File Status and Error Handling

Every file can have a FILE STATUS data item. After OPEN, CLOSE, READ, WRITE, REWRITE, and DELETE, the runtime sets this item to a two-character code. '00' means success. Other values indicate conditions: end of file (often '10'), record not found, duplicate key, permanent error, and so on. Check the file status after OPEN and CLOSE so you can log or handle failures. If OPEN fails, do not proceed with I/O; report the error and exit or retry. If CLOSE fails, the data may still have been written but the resource may not have been released cleanly; log the status. Good resource management includes checking these statuses and ensuring you do not leave files in an inconsistent state.

Reopening a File

After CLOSE, you can OPEN the same file again (reopen) in the same program run. That is useful if you need two passes over the file (e.g. first pass count, second pass process) or if you close temporarily for some other reason. You cannot OPEN a file that is already open; you must CLOSE first. The same OPEN mode rules apply on reopen: use INPUT for read-only, I-O for update, etc.

Multiple Files and Resource Limits

COBOL programs can use many files (e.g. up to 255 in some implementations). Each open file consumes system resources: buffers, control blocks, and possibly dataset enqueue. In batch this is usually bounded by the number of files in the program. In CICS, the total number of open files across all tasks is limited by the region configuration. Good resource management means opening only the files you need, closing them as soon as you are done, and handling errors so you do not leave files open after a failure. If you open many files, consider closing those that are no longer needed before opening more.

Step-by-Step: Safe File Processing

  1. Define all files in the FILE-CONTROL and FD with FILE STATUS. In PROCEDURE DIVISION, OPEN each file with the correct mode (INPUT, OUTPUT, I-O, or EXTEND).
  2. Check file status after OPEN. If not '00', handle the error (message, stop, or retry) and do not perform I/O on that file.
  3. Perform READ, WRITE, REWRITE, or DELETE as needed. Check file status after each I/O where the outcome matters (e.g. end of file, duplicate key).
  4. When processing is complete, CLOSE all files you opened. Check file status after CLOSE if you need to detect close failures.
  5. If the program exits abnormally (e.g. abend), the system may close files, but do not rely on it for critical data; design so that critical updates are committed or recoverable.

Step-by-Step: Opening for Update (I-O)

  1. Ensure the file is defined for direct or relative access (not purely sequential) if your COBOL and platform require it for I-O. OPEN I-O filename.
  2. Read the record you want to update with READ. Check file status (e.g. '00' for found, '23' for not found).
  3. Modify the record in the FD buffer. Issue REWRITE to write it back. Check file status.
  4. When done with the file, CLOSE filename. Do not leave the file open longer than necessary.

Best Practices

Test Your Knowledge

Test Your Knowledge

1. Before you can READ a file you must:

  • CLOSE it
  • OPEN it with INPUT (or I-O)
  • WRITE to it
  • Define it in JCL only

2. CLOSE is important because it:

  • Opens the file
  • Flushes buffers and releases resources
  • Reads the last record
  • Defines the FD

3. To REWRITE a record you must OPEN the file with:

  • INPUT
  • OUTPUT
  • I-O
  • EXTEND