85 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Groff
		
	
	
	
	
	
			
		
		
	
	
			85 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Groff
		
	
	
	
	
	
.\" Copyright (c) 1980 Regents of the University of California.
 | 
						|
.\" All rights reserved.  The Berkeley software License Agreement
 | 
						|
.\" specifies the terms and conditions for redistribution.
 | 
						|
.\"
 | 
						|
.\"	@(#)close.2	6.3 (Berkeley) 5/22/86
 | 
						|
.\"
 | 
						|
.TH CLOSE 2 "May 22, 1986"
 | 
						|
.UC 4
 | 
						|
.SH NAME
 | 
						|
close \- delete a descriptor
 | 
						|
.SH SYNOPSIS
 | 
						|
.nf
 | 
						|
.ft B
 | 
						|
#include <unistd.h>
 | 
						|
 | 
						|
int close(int \fId\fP)
 | 
						|
.ft R
 | 
						|
.fi
 | 
						|
.SH DESCRIPTION
 | 
						|
The
 | 
						|
.B close
 | 
						|
call deletes a descriptor from the per-process object
 | 
						|
reference table.
 | 
						|
If this is the last reference to the underlying object, then
 | 
						|
it will be deactivated.
 | 
						|
For example, on the last close of a file
 | 
						|
the current \fIseek\fP pointer associated with the file is lost;
 | 
						|
on the last close of a TCP/IP descriptor
 | 
						|
associated naming information and queued data are discarded;
 | 
						|
on the last close of a file holding an advisory lock
 | 
						|
the lock is released (see further
 | 
						|
.BR fcntl (2)).
 | 
						|
.PP
 | 
						|
A close of all of a process's descriptors is automatic on
 | 
						|
.IR exit ,
 | 
						|
but since
 | 
						|
there is a limit on the number of active descriptors per process,
 | 
						|
.B close
 | 
						|
is necessary for programs that deal with many descriptors.
 | 
						|
.PP
 | 
						|
When a process forks (see
 | 
						|
.BR fork (2)),
 | 
						|
all descriptors for the new child process reference the same
 | 
						|
objects as they did in the parent before the fork.
 | 
						|
If a new process is then to be run using
 | 
						|
.BR execve (2),
 | 
						|
the process would normally inherit these descriptors.  Most
 | 
						|
of the descriptors can be rearranged with
 | 
						|
.BR dup2 (2)
 | 
						|
or deleted with
 | 
						|
.B close
 | 
						|
before the
 | 
						|
.B execve
 | 
						|
is attempted, but if some of these descriptors will still
 | 
						|
be needed if the
 | 
						|
.B execve
 | 
						|
fails, it is necessary to arrange for them to be closed if the
 | 
						|
.B execve
 | 
						|
succeeds.
 | 
						|
For this reason, the call ``fcntl(d, F_SETFD, \fIflags\fR)'' is provided,
 | 
						|
that can be used to mark a descriptor "close on exec" by setting
 | 
						|
the
 | 
						|
.B FD_CLOEXEC
 | 
						|
flag:
 | 
						|
.PP
 | 
						|
.RS
 | 
						|
fcntl(d, F_SETFD, fcntl(d, F_GETFD) | FD_CLOEXEC);
 | 
						|
.RE
 | 
						|
.SH "RETURN VALUE
 | 
						|
Upon successful completion, a value of 0 is returned.
 | 
						|
Otherwise, a value of \-1 is returned and the global integer variable
 | 
						|
.B errno
 | 
						|
is set to indicate the error.
 | 
						|
.SH ERRORS
 | 
						|
.B Close
 | 
						|
will fail if:
 | 
						|
.TP 15
 | 
						|
[EBADF]
 | 
						|
\fID\fP is not an active descriptor.
 | 
						|
.SH "SEE ALSO"
 | 
						|
.BR open (2),
 | 
						|
.BR pipe (2),
 | 
						|
.BR execve (2),
 | 
						|
.BR fcntl (2).
 |