| Skip Navigation Links | |
| Exit Print View | |
![]() | man pages section 2: System Calls Oracle Solaris 11 Information Library |
- write on a file
#include <unistd.h>ssize_twrite(intfildes,const void *buf,size_tnbyte);
ssize_tpwrite(intfildes,const void *buf,size_tnbyte,off_toffset);
#include <sys/uio.h>ssize_twritev(intfildes,const struct iovec *iov,intiovcnt);
Thewrite() function attempts to writenbyte bytes from the buffer pointedto bybuf to the file associated with the open file descriptor,fildes.
Ifnbyte is 0,write() will return 0 and have no otherresults if the file is a regular file; otherwise, the results areunspecified.
On a regular file or other file capable of seeking, the actualwriting of data proceeds from the position in the file indicated bythe file offset associated withfildes. Before successful return fromwrite(), thefile offset is incremented by the number of bytes actually written. On aregular file, if this incremented file offset is greater than the lengthof the file, the length of the file will be set tothis file offset.
If theO_SYNC bit has been set, write I/O operations on thefile descriptor complete as defined by synchronized I/O file integrity completion.
Iffildes refers to a socket,write() is equivalent tosend(3SOCKET) withno flags set.
On a file not capable of seeking, writing always takes place startingat the current position. The value of a file offset associatedwith such a device is undefined.
If theO_APPEND flag of the file status flags is set, thefile offset will be set to the end of the file priorto each write and no intervening file modification operation will occur betweenchanging the file offset and the write operation.
For regular files, no data transfer will occur past the offset maximumestablished in the open file description withfildes.
Awrite() to a regular file is blocked if mandatory file/record lockingis set (seechmod(2)), and there is a record lock owned byanother process on the segment of the file to be written:
IfO_NDELAY orO_NONBLOCK is set,write() returns-1 and setserrno toEAGAIN.
IfO_NDELAY andO_NONBLOCK are clear,write() sleeps until all blocking locks are removed or thewrite() is terminated by a signal.
If awrite() requests that more bytes be written than thereis room for—for example, if the write would exceed the process filesize limit (seegetrlimit(2) andulimit(2)), the system file size limit, orthe free space on the device—only as many bytes as there isroom for will be written. For example, suppose there is space for 20bytes more in a file before reaching a limit. Awrite() of512-bytes returns 20. The nextwrite() of a non-zero number of bytesgives a failure return (except as noted for pipes and FIFO below).
Ifwrite() is interrupted by a signal before it writes any data,it will return -1 witherrno set toEINTR.
Ifwrite() is interrupted by a signal after it successfully writes somedata, it will return the number of bytes written.
Ifwrite() exceeds the process file size limit, the application generates aSIGXFSZ signal, whose default behavior is to dump core.
After awrite() to a regular file has successfully returned:
Any successfulread(2) from each byte position in the file that was modified by that write will return the data specified by thewrite() for that position until such byte positions are again modified.
Any subsequent successfulwrite() to the same byte position in the file will overwrite that file data.
Write requests to a pipe or FIFO are handled the same asa regular file with the following exceptions:
There is no file offset associated with a pipe, hence each write request appends to the end of the pipe.
Write requests of{PIPE_BUF} bytes or less are guaranteed not to be interleaved with data from other processes doing writes on the same pipe. Writes of greater than{PIPE_BUF} bytes may have data interleaved, on arbitrary boundaries, with writes by other processes, whether or not theO_NONBLOCK orO_NDELAY flags are set.
IfO_NONBLOCK andO_NDELAY are clear, a write request may cause the process to block, but on normal completion it returnsnbyte.
IfO_NONBLOCK andO_NDELAY are set,write() does not block the process. If awrite() request forPIPE_BUF or fewer bytes succeeds completelywrite() returnsnbyte. Otherwise, ifO_NONBLOCK is set, it returns-1 and setserrno toEAGAIN or ifO_NDELAY is set, it returns0. Awrite() request for greater than{PIPE_BUF} bytes transfers what it can and returns the number of bytes written or it transfers no data and, ifO_NONBLOCK is set, returns-1 witherrno set toEAGAIN or ifO_NDELAY is set, it returns0. Finally, if a request is greater thanPIPE_BUF bytes and all data previously written to the pipe has been read,write() transfers at leastPIPE_BUF bytes.
When attempting to write to a file descriptor (other than a pipe,a FIFO, a socket, or a stream) that supports nonblocking writes andcannot accept the data immediately:
IfO_NONBLOCK andO_NDELAY are clear,write() blocks until the data can be accepted.
IfO_NONBLOCK orO_NDELAY is set,write() does not block the process. If some data can be written without blocking the process,write() writes what it can and returns the number of bytes written. Otherwise, ifO_NONBLOCK is set, it returns-1 and setserrno toEAGAIN or ifO_NDELAY is set, it returns0.
Upon successful completion, wherenbyte is greater than 0,write() will markfor update thest_ctime andst_mtime fields of the file, and ifthe file is a regular file, theS_ISUID andS_ISGID bits ofthe file mode may be cleared.
For streams files (seeIntro(2) andstreamio(7I)), the operation ofwrite() isdetermined by the values of the minimum and maximumnbyte range (“packetsize”) accepted by the stream. These values are contained in the topmoststream module, and can not be set or tested from user level. Ifnbyte falls within the packet size range,nbyte bytes are written. Ifnbyte does not fall within the range and the minimum packet sizevalue is zero,write() breaks the buffer into maximum packet size segmentsprior to sending the data downstream (the last segment may besmaller than the maximum packet size). Ifnbyte does not fallwithin the range and the minimum value is non-zero,write() fails andsetserrno toERANGE. Writing a zero-length buffer (nbyte is zero) toa streams device sends a zero length message with zero returned.However, writing a zero-length buffer to a pipe or FIFO sends nomessage and zero is returned. The user program may issue theI_SWROPTioctl(2) to enable zero-length messages to be sent across thepipe or FIFO (seestreamio(7I)).
When writing to a stream, data messages are created with a priorityband of zero. When writing to a socket or to a streamthat is not a pipe or a FIFO:
IfO_NDELAY andO_NONBLOCK are not set, and the stream cannot accept data (the stream write queue is full due to internal flow control conditions),write() blocks until data can be accepted.
IfO_NDELAY orO_NONBLOCK is set and the stream cannot accept data,write() returns-1 and setserrno toEAGAIN.
IfO_NDELAY orO_NONBLOCK is set and part of the buffer has already been written when a condition occurs in which the stream cannot accept additional data,write() terminates and returns the number of bytes written.
Thewrite() andwritev() functions will fail if the stream head hadprocessed an asynchronous error before the call. In this case, thevalue oferrno does not reflect the result ofwrite() orwritev()but reflects the prior error.
Thepwrite() function is equivalent towrite(), except that it writes intoa given position and does not change the file offset (regardless ofwhetherO_APPEND is set). The first three arguments topwrite() are the sameaswrite(), with the addition of a fourth argumentoffset for thedesired position inside the file.
Thewritev() function performs the same action aswrite(), but gathers theoutput data from theiovcnt buffers specified by the members of theiov array:iov[0],iov[1], …,iov[iovcnt - 1]. Theiovcnt buffer is valid ifgreater than 0 and less than or equal to{IOV_MAX}. SeeIntro(2)for a definition of{IOV_MAX}.
Theiovec structure contains the following members:
void *iov_base;size_t iov_len;
Eachiovec entry specifies the base address and length of an areain memory from which data should be written. Thewritev() functionalways writes all data from an area before proceeding to the next.
Iffildes refers to a regular file and all of theiov_lenmembers in the array pointed to byiov are 0,writev() willreturn 0 and have no other effect. For other file types,the behavior is unspecified.
If the sum of theiov_len values is greater thanSSIZE_MAX, theoperation fails and no data is transferred.
Upon successful completion,write() returns the number of bytes actually written tothe file associated withfildes. This number is never greater thannbyte.Otherwise,-1 is returned, the file-pointer remains unchanged, anderrno is set toindicate the error.
Upon successful completion,writev() returns the number of bytes actually written. Otherwise, it returns-1, the file-pointer remains unchanged, anderrno isset to indicate an error.
Thewrite(),pwrite(), andwritev() functions will fail if:
Mandatory file/record locking is set,O_NDELAY orO_NONBLOCK is set, and there is a blocking record lock; an attempt is made to write to a stream that can not accept data with theO_NDELAY orO_NONBLOCK flag set; or a write to a pipe or FIFO ofPIPE_BUF bytes or less is requested and less thannbytes of free space is available.
Thefildes argument is not a valid file descriptor open for writing.
The write was going to go to sleep and cause a deadlock situation to occur.
The user's quota of disk blocks on the file system containing the file has been exhausted.
An attempt is made to write a file that exceeds the process's file size limit or the maximum file size (seegetrlimit(2) andulimit(2)).
The file is a regular file,nbyte is greater than 0, and the starting position is greater than or equal to the offset maximum established in the file description associated withfildes.
A signal was caught during the write operation and no data was transferred.
The process is in the background and is attempting to write to its controlling terminal whoseTOSTOP flag is set, or the process is neither ignoring nor blockingSIGTTOU signals and the process group of the process is orphaned.
Enforced record locking was enabled and{LOCK_MAX} regions are already locked in the system, or the system record lock table was full and the write could not go to sleep until the blocking record lock was removed.
Thefildes argument is on a remote machine and the link to that machine is no longer active.
During a write to an ordinary file, there is no free space left on the device.
An attempt is made to write to a streams with insufficient streams memory resources available in the system.
A hangup occurred on the stream being written to.
An attempt is made to write to a pipe or a FIFO that is not open for reading by any process, or that has only one end open (or to a file descriptor created bysocket(3SOCKET), using typeSOCK_STREAM that is no longer connected to a peer endpoint). ASIGPIPE signal will also be sent to the thread. The process dies unless special provisions were taken to catch or ignore the signal.
The transfer request size was outside the range supported by the streams file associated withfildes.
Thewrite() andpwrite() functions will fail if:
Thebuf argument points to an illegal address.
Thenbyte argument overflowed anssize_t.
Thepwrite() function fails and the file pointer remains unchanged if:
Thefildes argument is associated with a pipe or FIFO.
Thewrite() andwritev() functions may fail if:
The stream or multiplexer referenced byfildes is linked (directly or indirectly) downstream from a multiplexer.
A request was made of a non-existent device, or the request was outside the capabilities of the device.
A hangup occurred on the stream being written to.
A write to a streams file may fail if an error messagehas been received at the stream head. In this case,errnois set to the value included in the error message.
Thewritev() function may fail if:
Theiovcnt argument was less than or equal to 0 or greater than {IOV_MAX}; one of theiov_len values in theiov array was negative; or the sum of theiov_len values in theiov array overflowed anssize_t.
Thepwrite() function has a transitional interface for 64-bit file offsets. Seelf64(5).
Seeattributes(5) for descriptions of the following attributes:
|
Intro(2),chmod(2),creat(2),dup(2),fcntl(2),getrlimit(2),ioctl(2),lseek(2),open(2),pipe(2),ulimit(2),send(3SOCKET),socket(3SOCKET),attributes(5),lf64(5),standards(5),streamio(7I)
Copyright © 2011, Oracle and/or its affiliates. All rights reserved.Legal Notices | ![]() ![]() |