close()--Close File or Socket Descriptor


  Syntax
 #include <unistd.h>

 int close(int fildes);

  Service Program Name: QP0LLIB1

  Default Public Authority: *USE

  Threadsafe: Conditional; see Usage Notes.

The close() function closes a descriptor, fildes. This frees the descriptor to be returned by future open() calls and other calls that create descriptors.

When the last open descriptor for a file is closed, the file itself is closed. If the link count of the file is zero at that time, the space occupied by the file is freed and the file becomes inaccessible.

close() unlocks (removes) all outstanding byte locks that a job has on the associated file.

When all file descriptors associated with a pipe or FIFO special file are closed, any data remaining in the pipe or FIFO is discarded and internal storage used is returned to the system.

When fildes refers to a socket, close() closes the socket identified by the descriptor.

For information about the exit point that can be associated with close(), see Integrated File System Scan on Close Exit Programs.


Parameters

fildes
(Input) The descriptor to be closed.

Authorities

No authorization is required. Authorization is verified during open(), creat(), or socket().


Return Value


Error Conditions

If close() is not successful, errno usually indicates one of the following errors. Under some conditions, errno could indicate an error other than those listed here.

Additionally, if interaction with a file server is required to access the object, errno could indicate one of the following errors:


Error Messages

The following messages may be sent from this function:



Usage Notes

  1. This function will fail with error code [EBADF] when fildes is a scan descriptor that was passed to one of the scan-related exit programs. See Integrated File System Scan on Open Exit Programs and Integrated File System Scan on Close Exit Programs for more information.

  2. This function will fail with error code [ENOTSAFE] when all the following conditions are true:

    • Where multiple threads exist in the job.
    • The object on which this function is operating resides in a file system that is not threadsafe. Only the following file systems are threadsafe for this function:

      • "Root" (/)
      • QOpenSys
      • User-defined
      • QNTC
      • QSYS.LIB
      • Independent ASP QSYS.LIB
      • QOPT
      • Network File System
      • QFileSvr.400

  3. When a socket descriptor is closed, the system tries to send any queued data associated with the socket.

    • For AF_INET sockets, depending on whether the SO_LINGER socket option is set, queued data may be discarded.

      Note: For these sockets, the default value for the SO_LINGER socket option has the option flag set off (the system attempts to send any queued data with an infinite wait time).

  4. A socket descriptor being shared among multiple processes is not closed until the process that issued the close() is the last process with access to the socket.

Related Information


Example

The following example uses close().

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

main() {
  int fd1, fd2;
  char out[20]="Test string",
       fn[]="test.file",
       in[20];
  short write_error;

  memset(in, 0x00, sizeof(in));

  write_error = 0;

  if ( (fd1 = creat(fn,S_IRWXU)) == -1)
    perror("creat() error");
  else if ( (fd2 = open(fn,O_RDWR)) == -1)
    perror("open() error");
  else {
    if (write(fd1, out, strlen(out)+1) == -1) {
      perror("write() error");
      write_error = 1;
    }
    close(fd1);
    if (!write_error) {
      if (read(fd2, in, sizeof(in)) == -1)
        perror("read() error");
      else printf("string read from file was: '%s'\n", in);
    }
    close(fd2);
  }
}

Output:

string read from file was: 'Test string'

API introduced: V3R1

[ Back to top | UNIX-Type APIs | APIs by category ]