foreword
This manual page is part of the POSIX Programmer's Manual. The implementation of this interface on Linux may be different (see the appropriate Linux man page for details on Linux behavior) or the interface may not be implemented on Linux.
Name
pwrite, write - write to a file
summary
#include <unistd.h>
ssize_t pwrite(int lines, const leather *buf, size_t nbyte,
offset off_t);
tamaño_twrite(intlines, const leather *buf, size_t nbyte);
description
Himhe writes() The function tries to writenbyteBytes of buffer pointed tobufto the file associated with the open file descriptor,lines.
Before and when you take any of the actions described belownbyteis null and the file is a normal file thathe writes() can detect and return errors as described below. In the absence of errors or when no error detection is performed, thehe writes() returns null and has no other results. Yesnbyteis null and the file is not a normal file, the results are not given.
In a regular file or other searchable file, the actual writing of data occurs from the position in the file specified by the associated offset filelines. Before the successful return ofhe writes(), the file offset is increased by the number of bytes actually written. In a normal file, if this incremented file offset is greater than the file length, then the file length is set to this file offset.
A non-searchable file is always written from the current position. The value of a file offset associated with such a device is undefined.
If the file status flags have the O_APPEND flag set, the file offset is set to the end of the file before each write, and no intermediate file modification operations occur between changing the file offset and writing.
It is ahe writes() requires more bytes to be written than available disk space (e.g. the process file size limit or the physical end of a medium), only as many bytes are written as there is disk space. Suppose there is room in a file for another 20 bytes before a limit is reached. A write of 512 bytes returns 20. The next write of a non-zero number of bytes will return an error (except as noted below).
If the request results in the file size exceeding the soft file size limit for the process and there is no room to write bytes, the request fails and the implementation generates the SIGXFSZ signal for the thread.
Ehe writes() is interrupted by a character before writing data, it returns -1 withErrordefined as [INTR].
Ehe writes() is interrupted by a signal after the successful writing of some data, it returns the number of bytes written.
If the value ofnbyteis greater than {SSIZE_MAX}, the result is implementation-defined.
Afterhe writes() for a successfully returned normal file:
- *
- any successarchive() of each byte location in the file modified by this write operation returns the data specified byhe writes() to that position until those byte positions are changed again.
- *
- Any subsequent successhe writes() at the same byte position in the file overwrites the data in the file.
Write requests to a pipe or FIFO are treated the same as a normal file, with the following exceptions:
- *
- There is no file offset associated with a pipe, so each write request is added to the end of the pipe.
- *
- Write requests of {PIPE_BUF} bytes or less are not mixed up with data from other processes writing to the same pipe. For writes larger than {PIPE_BUF} bytes, data on arbitrary boundaries can be mixed up with writes from other processes, regardless of whether the file status flags' O_NONBLOCK flag is set or not.
- *
- If the O_NONBLOCK flag is clear, a write request can cause the thread to block, but it usually returns after completionnbyte.
- *
- If the O_NONBLOCK indicator is set,he writes() Requests are treated differently in the following ways:
- *
- Himhe writes() The function does not block the thread.
- *
- A write request of {PIPE_BUF} or fewer bytes has the following effect: if there is enough space in the pipe,he writes() transfers all data and returns the requested number of bytes. On the other hand,he writes() does not transfer any data and returns -1 withErrorset to [AGAIN].
- *
- A write request of more than {PIPE_BUF} bytes results in one of the following events:
- *
- If at least one byte can be written, transmit what you can and return the number of bytes written. When all the data previously written to the pipe is read, at least {PIPE_BUF} bytes are transferred.
- *
- If data cannot be written, do not transfer the data and return -1 withErrorset to [AGAIN].
If you are trying to write to a file descriptor (other than pipe or FIFO) that supports non-blocking writes and cannot accept the data immediately:
- *
- If the O_NONBLOCK flag is clear,he writes() blocks the calling thread until the data can be accepted.
- *
- If the O_NONBLOCK indicator is set,he writes() does not block the thread. If some data can be written without blocking the thread,he writes() writes what it can and returns the number of bytes written. Otherwise -1 is returned and setErrorto [EA AGAIN].
After successful completion, wherenbyteis greater than 0,he writes() checked to update thest_ctimejst_mtimeFile fields and if the file is a regular file, the file mode bits S_ISUID and S_ISGID can be cleared.
Regular files do not transfer data beyond the maximum offset defined in the open file's associated descriptionlines.
Elinesrefers to a plug,he writes() is equivalent tofrom you() with no flags set.
When the O_DSYNC bit is set, I/O writes to the file descriptor complete as defined by Synchronized I/O Data Integrity Completion.
When the O_SYNC bit is set, I/O writes to the file descriptor complete as defined by Synchronized I/O File Integrity Completion.
Elinesrefers to a shared memory object, the result ofhe writes() function is not specified.
Elinesrefers to a memory object written, the result of thehe writes() function is not specified.
Elinesrefers to a FLOW, the operation ofhe writes() is determined by the values of the minima and maximanbyteRange (packet size) accepted by STREAM. These values are determined by the upper STREAM module. Yesnbytefalls within the packet size range,nbytebytes are written. Yesnbytedoes not fall within the range and the minimum packet size value is 0,he writes() divides the buffer into segments of maximum packet size before sending the data down (the last segment can contain less than the maximum packet size). Yesnbytedoes not fall within the range and the minimum value is non-zero,he writes( ) will fail withErrorset to [AREA]. Write a null buffer (nbyteis0) sends back 0 bytes with 0 to a STREAMS device. However, writing a zero-length buffer to a STREAMS or FIFO-based channel sends no message and returns 0. The process can issue I_SWROPTioctl() to allow sending zero-length messages down the pipe or FIFO.
When writing to a STREAM, data messages are created with a priority band of 0. When writing to a STREAM that is not a pipe or FIFO:
- *
- If O_NONBLOCK is clear and STREAM cannot accept data (STREAM's write queue is full due to internal flow control conditions),he writes() blocks until the data can be accepted.
- *
- If O_NONBLOCK is set and STREAM cannot accept data,he writes() returns -1 and setsErrorto [EA AGAIN].
- *
- If O_NONBLOCK is set and part of the buffer was written while a condition exists where STREAM cannot accept additional data,he writes() exits and returns the number of bytes written.
Likewise,he writes() fails if the STREAM header handled an asynchronous error before the call. In this case, the value ofErrordoes not reflect the result ofhe writes(), but reflects the above error.
Himto write() is equivalent tohe writes(), except that it writes to a specific position without changing the file pointer. The first three arguments forto write() it's the same ashe writes() with the addition of a fourth argument to move to the desired position within the file.
return value
After successful graduationhe writes() yto write() returns the number of bytes actually written to the associated filelines.This number will never be greater thannbyte. Otherwise -1 is returned andErrorconfigured to show the error.
mistake
Himhe writes() yto write() functions will fail if:
- AGAIN
- The O_NONBLOCK flag is set on the file descriptor and the thread would be delayedhe writes( ) Operation.
- EBADF
- HimlinesArgument is not a valid file descriptor open for writing.
- EFBIG
- An attempt was made to write a file that exceeded the implementation-defined maximum file size or process file size limit and there was no space to write bytes.
- EFBIG
- The file is a normal file,nbyteis greater than 0 and the start position is greater than or equal to the maximum offset defined in the description of the associated open filelines.
- EINTR
- The write operation ended due to the receipt of a signal and no data was transferred.
- YOU
- The process is a member of a group of background processes attempting to write to its controlling terminal, TOSTOP is set, the process does not ignore or block SIGTTOU, and the process's process group is orphaned. This error can also be returned under implementation-defined conditions.
- ENOSPC
- There was no free space on the device containing the file.
- EPIPE
- Attempting to write to a pipe or FIFO that is not open for reading by any process or has only one end open. A SIGPIPE signal is also sent to the thread.
- TO REACH
- The transfer request size was outside the range supported by the associated STREAMS filelines.
Himhe writes() fails if:
- EAGAIN or EWOULDBLOCK
The file descriptor is for a socket, is marked O_NONBLOCK, and would be read-only.
- ECONNRESET
- An attempt was made to write to a disconnected socket.
- EPIPE
- An attempt was made to write to a socket that is read-only or is no longer connected. In the latter case, if the socket is of type SOCK_STREAM, the SIGPIPE signal is generated for the calling process.
Himhe writes() yto write() functions can fail if:
- INDIVIDUAL SELECTION
- The STREAM or multiplexer it refers tolinesit is connected (directly or indirectly) to a multiplexer.
- YOU
- A physical I/O error has occurred.
- ENOBUFS
- There were not enough resources available on the system to perform the operation.
- ENXIO
- A request was made for a device that does not exist or the request was outside the device capabilities.
- ENXIO
- A crash occurred on the recorded STREAM.
A write operation to a STREAM file may fail if an error message is received in the STREAM header. In this case,Erroris set to the value contained in the error message.
Himhe writes() may fail if:
- EACCES
- An attempt was made to write to a socket and the calling process does not have the appropriate permissions.
- TO ENTER
- An attempt was made to write to a socket and the LAN interface used to reach the destination failed.
- ENETUNREACH
An attempt was made to write to a socket and there is no route to the network.
(Video) Linux Man Pages - A Quick Tutorial
Himto write() will fail and the file pointer will remain unchanged if:
- INDIVIDUAL SELECTION
- HimcompensateArgument is invalid. The value is negative.
- ESPIPE
- linesis assigned to a pipe or a FIFO.
The following sections are informative.
examples
Write from a buffer
The following example writes data from the buffer it points tobuffor the file associated with the file descriptorfd.
#include <sys/tipos.h>#include<string.h>...char buf[20];size_t nbytes;ssize_t bytes_escritos;int fd;...strcpy(buf, "Este é um teste\n");nbytes = strlen(buf);bytes_escritos = write(fd, buf , nbyte);...
app usage
no
fundamental reason
See also the EXPLANATION section inarchive().
An attempt to write to a pipe or FIFO has several important properties:
- *
- atomic/non-atomicNote: A write operation is atomic if the entire amount written in one operation is not interspersed with data from another process. This is useful when you have multiple writers sending data to a single reader. Applications need to know atomically the expected size of a write request. This maximum is called {PIPE_BUF}. This volume of IEEE Std 1003.1-2001 does not specify whether write requests greater than {PIPE_BUF} bytes are atomic, but requires writes of {PIPE_BUF} or fewer bytes to be atomic.
- *
- lock/immediately: Blocking is only possible when O_NONBLOCK is switched off. If there is enough disk space for all requested data to be written immediately, the implementation should do so. Otherwise the process may hang; that is, pause until enough space is available to write. The effective size of a pipe or FIFO (the maximum amount that can be written in a non-blocking operation) can vary dynamically depending on the implementation, so it's not possible to give a fixed value for it.
- *
- Complete/partial/postponed: A written request:
int fildes;size_t nbyte;ssize_t ret;char *buf;ret = write(fildes, buf, nbyte);
You can go back:
- Completely
withdrawn=nbyte
- Partial
withdrawn<nbyte
It will never happen thoughnbyte<= {PIPE_BUF}. When this happens (withnbyte> {PIPE_BUF}), this band of IEEE Std 1003.1-2001 does not guarantee atomicity, even ifwithdrawn<= {PIPE_BUF} since atomicity is guaranteed based on quantityrequested, not the crowdwritten.
- Postponed:
withdrawn=-1,Error=[ONE AGAIN]
This error indicates that a subsequent request may succeed. doesn't indicate ithe mustsuccess, even ifnbyte<= {PIPE_BUF} because if no process is reading from the pipe or FIFO, the write will never succeed. An application can usefully count the number of times [EAGAIN] is caused by a given value ofnbyte> {PIPE_BUF} and possibly do subsequent writes with a smaller value given that the effective pipe size may have decreased.
Partial and lazy writes are only possible if O_NONBLOCK is set.
The relationships of these properties are shown in the following tables:
l l l l |
If the O_NONBLOCK flag is clear, a write request will be blocked if the value that can be written immediately is less than the requested value. If the flag is set (exfktl()), a write request is never blocked.
l l l l |
There is no partial write exception when O_NONBLOCK is set. With the exception of writing to an empty pipe, this IEEE Std 1003.1-2001 volume does not specify when a partial write is performed, as it would require specifying internal implementation details. Every application must be prepared to handle partial writes when O_NONBLOCK is set and the requested amount is greater than {PIPE_BUF}, just as every application must be prepared to handle partial writes to other types of file descriptors.
The intention of forcing at least one byte to be written if one can be written is to ensure that each write proceeds if there is room in the pipe. If the pipe is empty, the {PIPE_BUF} bytes must be written; if not, at least some progress must have been made.
Where this band of IEEE Std 1003.1-2001 requires -1 to be returned andErrorset to [EAGAIN], most historical implementations return zero (with the O_NDELAY flag set, which is the historical ancestor of O_NONBLOCK, but not in this volume of IEEE Std 1003.1-2001). The error indications in this volume of IEEE Std 1003.1-2001 have been chosen so that an application can distinguish these instances from the end of the file. timehe writes() cannot receive end-of-file indication,archive() and the two functions have similar return values. Also, some existing systems (e.g. 8th edition) allow null byte writing to indicate that the reader should get an end-of-file indication; for these systems there is a return value of zerohe writes() indicates a successful writing of an end-of-file indication.
Implementations are allowed but not required to perform error checkinghe writes() fordern null Bytes an.
The concept of a {PIPE_MAX} limit (which specifies the maximum number of bytes that can be written to a pipe in a single operation) was considered but discarded because such a concept would unnecessarily limit the application's ability to write.
Also see the O_NONBLOCK discussion belowarchive().
Writes can be serialized against other reads and writes. yes onearchive() of file data can be shown (by whatever means) to occur after ahe writes() of the data should reflect thishe writes(), even if the calls are made from different processes. A similar requirement applies to multiple writes to the same file location. This is necessary to ensure the transfer of datahe writes( ) Calls for subsequentarchive( ) Calls. This requirement is particularly important for network file systems, where some caching schemes violate these semantics.
Note that this is stated in relation toarchive() yhe writes(). XSI Extensionsarchive() yTo write() also obey these semantics. This formulation would also allow for a new "high performance" recording analog that does not meet these serialization requirements. This IEEE Std1003.1-2001 volume also makes no mention of application-level caching effects (as suggested byto study).
This IEEE Std 1003.1-2001 volume does not report the file offset value after returning an error; there are many cases. For errors like [EBADF] the concept makes no sense since no files are involved. In the case of immediately recognized errors like [EAGAIN], the pointer should of course not change. However, after a failure or hardware failure, an updated value would be very useful and conforms to the behavior of many implementations.
This IEEE Std 1003.1-2001 volume does not specify the behavior of concurrent writes to a multiprocess file. Applications must use some form of concurrency control.
future directions
no
also see
chmod(),create(),Double(),fktl(),get limit(),I'm looking for(),open minded(),benefit(),to decrease(),To write(), the basic definition band of IEEE Std 1003.1-2001,<limits.h>,<beehive.h>,<sys/uio.h>,<unistd.h>
Copyright ©
Portions of this text have been reprinted and in electronic format from IEEE Std 1003.1, 2003 Edition, Information Technology Standard: Interface for Portable Operating Systems (POSIX), The Open Group Base Specifications, Edition 6, Copyright ©2001-2003 Institute of Electrical and Electronic Engineers Electronics, Inc and The Open Group. In the event of any inconsistency between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the reference document. The original pattern is available online athttp://www.opengroup.org/unix/online.html.