七宗罪分别是什么| 胃大是什么原因造成的| 龟头炎用什么药膏| ldh是什么| o2o是什么意思| 白事的礼金叫什么| 烤箱能做什么美食| 白醋和陈醋有什么区别| 脾脏是人体的什么器官| 北京西单附近有什么好玩的| 2024年五行属什么| 五十年是什么婚| 孕酮低吃什么| 甲亢是什么引起的| 党参长什么样| tg是什么意思| 金银花什么时候开花| 雨花斋靠什么盈利| 双喜临门是什么生肖| 蜜蜡脱毛有什么危害吗| pd是什么意思| 黄金为什么值钱| 桑葚泡酒有什么功效| 倒模是什么意思| 什么的木筏| 石骨症是什么病| 不感冒是什么意思| 乌药别名叫什么| 孩子贫血吃什么补血最快| blingbling什么意思| 什么是个性| 痧是什么| 摸胸是什么感觉| 熟练的反义词是什么| 爱是个什么东西| 自主能力是什么意思| 530是什么意思| 缓释是什么意思| 两面性是什么意思| 智利说什么语言| 葛粉吃了有什么好处| 所向披靡是什么意思| 5.8是什么星座| 锌是什么颜色| 3月3日是什么节| 什么是嗳气有何症状| 什么是生僻字| 晚上睡觉放屁多是什么原因| 种植牙有什么危害| 老是瞌睡是什么原因| 茶减一笔是什么字| 跳楼机是什么| 88年的属什么| 小确幸是什么意思| 易烊千玺的真名叫什么| 曹操的脸谱是什么颜色| 拉水便是什么原因| 9月出生的是什么星座| 冷战什么意思| 什么是腐女| naco是什么牌子| 曹植是什么生肖| 大象的耳朵有什么作用| 规培证有什么用| 黄鼠狼为什么叫黄大仙| 断肠草长什么样| 五月份什么星座| 田七蒸瘦肉有什么功效| 酸梅汤不能和什么一起吃| 信手拈来是什么意思| 植树节什么时候| 牙膏尾部的颜色代表什么意思| 美尼尔氏综合症是什么病| 秋葵什么人不能吃| gary什么意思| 什么原因引起尿酸高| 腰闪了是什么症状| En什么意思| 人大是干什么的| 容易做梦是什么原因引起的| 6月18号是什么星座| xgrq是什么烟| 结节是什么症状| 高血糖吃什么水果最好| rhc血型阳性是什么意思| 脾胃虚弱吃什么药| 马克笔什么牌子好| 吃什么大补| 做梦梦到钱是什么预兆| 银壶一般什么价位| 1949年是什么年| 淮山和山药有什么区别| 老睡不醒是什么原因| 火鸡面为什么叫火鸡面| 身上长小红点是什么原因| 人什么意思| 总是睡不着觉是什么原因| 妇科炎症吃什么食物好| 逍遥丸是治什么的| 肛门里面疼是什么原因| 越五行属性是什么| 布洛芬治什么| 牛跟什么生肖相合| 运气是什么意思| 益母草煮鸡蛋有什么功效| 窦性心动过速是什么原因| 酒精和碘伏有什么区别| 头顶痛吃什么药效果好| 仙草是什么草| 铁剂不能与什么同服| 歇夏是什么意思| 芊字五行属什么| 绕行是什么意思| 什么教无类| 知性是什么类型的女人| 熬夜吃什么补回来| 胰腺炎吃什么药好| 无可厚非什么意思| 五塔标行军散有什么功效| 西瓜和什么食物相克| 复方板蓝根和板蓝根有什么区别| 三焦热盛是什么意思| 月经推迟是什么原因导致的| 幽门螺杆菌阳性吃什么药| 什么是性上瘾| 干巴爹什么意思| 两肺纹理增粗是什么意思| 什么水果含叶酸最多| 急性肠胃炎吃什么食物| 男性做彩超要检查什么| 周杰伦是什么星座| 腰肌劳损是什么原因引起的| 胃胀是什么原因导致的| 军官是什么意思| 救人一命胜造七级浮屠是什么意思| 长期口腔溃疡挂什么科| 什么是斜视| lucas是什么意思| 茄子是什么形状| 什么人不能吃猪肝| 什么车可以闯红灯| 羊奶有什么作用与功效| 验孕棒两条杠什么意思| 每逢佳节倍思亲的上一句是什么| 吃什么对胰腺有好处| 心率过速吃什么药| 郁郁寡欢的意思是什么| 股癣是什么样子的图片| 朗朗乾坤下一句是什么| 子宫后位是什么意思| 下焦湿热吃什么药| 红颜什么意思| 心肌炎有什么症状和表现| sahara是什么牌子| 女性腰疼是什么原因| 大肠埃希菌是什么病| 什么是干冰| 箨是什么意思| 狗拉肚子吃什么药| 刘秀和刘邦是什么关系| 橘白猫是什么品种| 早上起来心慌是什么原因| 627是什么星座| 旗舰店什么意思| 抽烟为什么会上瘾| 社保缴费基数是什么意思| 牙银肿痛吃什么药| 梦见一群羊是什么意思| 风花雪月是什么意思| 高血脂吃什么药效果好| 子母门是什么意思| 幻觉妄想状态是什么病| 水泡型脚气用什么药好| 你正在干什么用英语怎么说| 胃疼想吐是什么原因| 血虚吃什么中成药最好| 淀粉酶是什么| 胃疼吃什么消炎药| fl是什么| 复方木尼孜其颗粒治什么病| 杨贵妃长什么样| 三世诸佛是什么意思| 蒂芙尼蓝是什么颜色| 粉头是什么意思| 孝是什么| 十五的月亮十六圆是什么意思| 阴部毛变白是什么原因| 牛皮糖是什么意思| 还愿有什么讲究| 减肥吃什么油| 京东自营什么意思| 黑天天的学名叫什么| 什么叫收缩压和舒张压| 为什么高铁没有e座| 身体不适是什么意思| 小拇指发麻是什么原因| 什么颜色的猫最旺财| 穿旗袍配什么发型好看| 前列腺钙化什么意思| 梦见自己的车丢了是什么意思| 什么血型最招蚊子| 全身而退是什么意思| 乳糖不耐受不能吃什么| 先兆性流产是什么意思| 梦见在水里游泳是什么意思| 僵尸车是什么意思| 破执是什么意思| 瘊子是什么| 睡着了流口水是什么原因| 康什么大道| 右位是什么意思| 芸字五行属什么| 权倾朝野是什么意思| 里是什么结构| 前列腺增生有什么症状| 高血压有什么危害| 正月十八是什么星座| 身体湿热吃什么中成药| 人为什么要日b| crp偏高说明什么| 盐冻虾是什么意思| 有毒是什么意思| 与虎谋皮什么意思| 梦见梯子是什么意思| 毛囊炎是什么引起的| 2b铅笔和hb铅笔有什么区别| 孩子睡觉出汗多是什么原因| 流鼻血什么原因| 尿结石不能吃什么| 阿胶什么人不能吃| 银行卡睡眠状态是什么意思| 为什么拉屎会拉出血| 劳苦功高是什么意思| 头发厚适合剪什么发型| 耘字五行属什么| 化疗中的病人应该吃什么| 尿酸高适合吃什么水果| 黄瓜和什么不能一起吃| 平时血压高突然变低什么原因| 58年属什么| 花生死苗烂根用什么药| 枸杞有什么用| 痰多吃什么好化痰| 肾囊肿是什么原因引起的| 6月10日什么星座| 腹泻拉稀水是什么原因| ivory是什么意思| 蜱虫最怕什么药| 为什么女人比男人长寿| 风湿病是什么原因造成的| 绞丝旁一个奇念什么| 疑心病是什么意思| 阴道炎要用什么药| 左耳疼痛什么原因引起| 麦芽糊精是什么| 吃什么药能快速降血压| 冻感冒吃什么药| 上坟用什么水果| 什么食物含胶原蛋白最多| 北芪与黄芪有什么区别| 兵马未动粮草先行是什么意思| 血红蛋白浓度是什么意思| 酸梅汤什么人不能喝| 24度穿什么衣服合适| 百度
The Open Group Base Specifications Issue 8
IEEE Std 1003.1-2024
Copyright ? 2001-2024 The IEEE and The Open Group

NAME

pwrite, write — write on a file

SYNOPSIS

#include <unistd.h>

ssize_t pwrite(int
fildes, const void *buf, size_t nbyte,
       off_t
offset);
ssize_t write(int
fildes, const void *buf, size_t nbyte);

DESCRIPTION

The write() function shall attempt to write nbyte bytes from the buffer pointed to by buf to the file associated with the open file descriptor, fildes.

Before any action described below is taken, and if nbyte is zero and the file is a regular file, the write() function may detect and return errors as described below. In the absence of errors, or if error detection is not performed, the write() function shall return zero and have no other results. If nbyte is zero and the file is not a regular file, the results are unspecified.

On a regular file or other file capable of seeking, the actual writing of data shall proceed from the position in the file indicated by the file offset associated with fildes. Before successful return from write(), the file offset shall be incremented by the number of bytes actually written. On a regular file, if the position of the last byte written is greater than or equal to the length of the file, the length of the file shall be set to this position plus one.

On a file not capable of seeking, writing shall always take place starting at the current position. The value of a file offset associated with such a device is undefined.

If the O_APPEND flag of the file status flags is set, the file offset shall be set to the end of the file prior to each write and no intervening file modification operation shall occur between changing the file offset and the write operation.

If a write() requests that more bytes be written than there is room for (for example, the file size limit of the process or the physical end of a medium), only as many bytes as there is room for shall be written. For example, suppose there is space for 20 bytes more in a file before reaching a limit. A write of 512 bytes will return 20. The next write of a non-zero number of bytes would give a failure return (except as noted below).

If the request would cause the file size to exceed the soft file size limit for the process and there is no room for any bytes to be written, the request shall fail [XSI] [Option Start]  and the implementation shall generate a SIGXFSZ signal for the thread. [Option End]

If write() is interrupted by a signal before it writes any data, it shall return -1 with errno set to [EINTR].

If write() is interrupted by a signal after it successfully writes some data, it shall return the number of bytes written.

If the value of nbyte is greater than {SSIZE_MAX}, the result is implementation-defined.

After a write() to a regular file has successfully returned:


Write requests to a pipe or FIFO shall be handled in the same way as a regular file with the following exceptions:

When attempting to write to a file descriptor (other than a pipe or FIFO) that supports non-blocking writes and cannot accept the data immediately:

Upon successful completion, where nbyte is greater than 0, write() shall mark for update the last data modification and last file status change timestamps of the file, and if the file is a regular file, the S_ISUID and S_ISGID bits of the file mode may be cleared.

For regular files, no data transfer shall occur past the offset maximum established in the open file description associated with fildes.

If fildes refers to a socket, write() shall be equivalent to send() with no flags set.

[SIO] [Option Start] If the O_DSYNC bit has been set, write I/O operations on the file descriptor shall complete as defined by synchronized I/O data integrity completion.

If the O_SYNC bit has been set, write I/O operations on the file descriptor shall complete as defined by synchronized I/O file integrity completion. [Option End]

[SHM] [Option Start] If fildes refers to a shared memory object, the result of the write() function is unspecified. [Option End]

[TYM] [Option Start] If fildes refers to a typed memory object, the result of the write() function is unspecified. [Option End]

The pwrite() function shall be equivalent to write(), except that it writes into a given position and does not change the file offset (regardless of whether O_APPEND is set). The first three arguments to pwrite() are the same as write() with the addition of a fourth argument offset for the desired position inside the file. An attempt to perform a pwrite() on a file that is incapable of seeking shall result in an error.

RETURN VALUE

Upon successful completion, these functions shall return the number of bytes actually written to the file associated with fildes. This number shall never be greater than nbyte. Otherwise, -1 shall be returned and errno set to indicate the error.

ERRORS

These functions shall fail if:

[EAGAIN]
The file is neither a pipe, nor a FIFO, nor a socket, the O_NONBLOCK flag is set for the file descriptor, and the thread would be delayed in the write() operation.
[EBADF]
The fildes argument is not a valid file descriptor open for writing.
[EFBIG]
An attempt was made to write a file that exceeds the implementation-defined maximum file size and there was no room for any bytes to be written.
[EFBIG]
An attempt was made to write a file that exceeds the file size limit of the process, and there was no room for any bytes to be written. [XSI] [Option Start]  A SIGXFSZ signal shall also be generated for the thread. [Option End]
[EFBIG]
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 open file description associated with fildes.
[EINTR]
The write operation was terminated due to the receipt of a signal, and no data was transferred.
[EIO]
The process is a member of a background process group attempting to write to its controlling terminal, TOSTOP is set, the calling thread is not blocking SIGTTOU, the process is not ignoring SIGTTOU, and the process group of the process is orphaned. This error may also be returned under implementation-defined conditions.
[ENOSPC]
There was no free space remaining on the device containing the file.

The pwrite() function shall fail if:

[EINVAL]
The file is a regular file or block special file, and the offset argument is negative. The file offset shall remain unchanged.
[ESPIPE]
The file is incapable of seeking.

The write() function shall fail if:

[EAGAIN]
The file is a pipe or FIFO, the O_NONBLOCK flag is set for the file descriptor, and the thread would be delayed in the write operation.
[EAGAIN] or [EWOULDBLOCK]
The file is a socket, the O_NONBLOCK flag is set for the file descriptor, and the thread would be delayed in the write operation.
[ECONNRESET]
A write was attempted on a socket that is not connected.
[EPIPE]
An attempt is made to write to a pipe or FIFO that is not open for reading by any process, or that only has one end open. A SIGPIPE signal shall also be sent to the thread.
[EPIPE]
A write was attempted on a socket that is shut down for writing, or is no longer connected. In the latter case, if the socket is of type SOCK_STREAM, a SIGPIPE signal shall also be sent to the thread.

These functions may fail if:

[EIO]
A physical I/O error has occurred.
[ENOBUFS]
Insufficient resources were available in the system to perform the operation.
[ENXIO]
A request was made of a nonexistent device, or the request was outside the capabilities of the device.

The write() function may fail if:

[EACCES]
A write was attempted on a socket and the calling process does not have appropriate privileges.
[ENETDOWN]
A write was attempted on a socket and the local network interface used to reach the destination is down.
[ENETUNREACH]
A write was attempted on a socket and no route to the network is present.

The following sections are informative.

EXAMPLES

Writing from a Buffer

The following example writes data from the buffer pointed to by buf to the file associated with the file descriptor fd.

#include <sys/types.h>
#include <string.h>
...
char buf[20];
size_t nbytes;
ssize_t bytes_written;
int fd;
...
strcpy(buf, "This is a test\n");
nbytes = strlen(buf);

bytes_written = write(fd, buf, nbytes); ...

APPLICATION USAGE

None.

RATIONALE

See also the RATIONALE section in read().

An attempt to write to a pipe or FIFO has several major characteristics:

The relations of these properties are shown in the following tables:

Write to a Pipe or FIFO with O_NONBLOCK clear

Immediately Writable:

None

Some

nbyte

nbyte<={PIPE_BUF}

Atomic blocking nbyte

Atomic blocking nbyte

Atomic immediate nbyte

nbyte>{PIPE_BUF}

Blocking nbyte

Blocking nbyte

Blocking nbyte

If the O_NONBLOCK flag is clear, a write request shall block if the amount writable immediately is less than that requested. If the flag is set (by fcntl()), a write request shall never block.

Write to a Pipe or FIFO with O_NONBLOCK set

Immediately Writable:

None

Some

nbyte

nbyte<={PIPE_BUF}

-1, [EAGAIN]

-1, [EAGAIN]

Atomic nbyte

nbyte>{PIPE_BUF}

-1, [EAGAIN]

<nbyte or -1, [EAGAIN]

<=nbyte or -1, [EAGAIN]

There is no exception regarding partial writes when O_NONBLOCK is set. With the exception of writing to an empty pipe or FIFO, this volume of POSIX.1-2024 does not specify exactly when a partial write is performed since that would require specifying internal details of the implementation. Every application should be prepared to handle partial writes when O_NONBLOCK is set and the requested amount is greater than {PIPE_BUF}, just as every application should be prepared to handle partial writes on other kinds of file descriptors.

The intent of forcing writing at least one byte if any can be written is to assure that each write makes progress if there is any room in the pipe or FIFO. If the pipe or FIFO is empty, {PIPE_BUF} bytes must be written; if not, at least some progress must have been made.

Where this volume of POSIX.1-2024 requires -1 to be returned and errno set to [EAGAIN], most historical implementations return zero (with the O_NDELAY flag set, which is the historical predecessor of O_NONBLOCK, but is not itself in this volume of POSIX.1-2024). The error indications in this volume of POSIX.1-2024 were chosen so that an application can distinguish these cases from end-of-file. While write() cannot receive an indication of end-of-file, read() can, and the two functions have similar return values. Also, some existing systems (for example, Eighth Edition) permit a write of zero bytes to mean that the reader should get an end-of-file indication; for those systems, a return value of zero from write() indicates a successful write of an end-of-file indication.

Implementations are allowed, but not required, to perform error checking for write() requests of zero bytes.

The concept of a {PIPE_MAX} limit (indicating the maximum number of bytes that can be written to a pipe or FIFO in a single operation) was considered, but rejected, because this concept would unnecessarily limit application writing.

See also the discussion of O_NONBLOCK in read().

Writes can be serialized with respect to other reads and writes. If a read() of file data can be proven (by any means) to occur after a write() of the data, it must reflect that write(), even if the calls are made by different threads. A similar requirement applies to multiple write operations to the same file position. This is needed to guarantee the propagation of data from write() calls to subsequent read() calls. This requirement is particularly significant for networked file systems, where some caching schemes violate these semantics.

Note that this is specified in terms of read() and write(). The XSI extensions readv() and writev() also obey these semantics. A new "high-performance" write analog that did not follow these serialization requirements would also be permitted by this wording. This volume of POSIX.1-2024 is also silent about any effects of application-level caching (such as that done by stdio).

This volume of POSIX.1-2024 does not specify the value of the file offset after an error is returned; there are too many cases. For programming errors, such as [EBADF], the concept is meaningless since no file is involved. For errors that are detected immediately, such as [EAGAIN], clearly the pointer should not change. After an interrupt or hardware error, however, an updated value would be very useful and is the behavior of many implementations.

This volume of POSIX.1-2024 does not specify the behavior of concurrent writes to a regular file from multiple threads, except that each write is atomic (see 2.9.7 Thread Interactions with File Operations). Applications should use some form of concurrency control.

This volume of POSIX.1-2024 intentionally does not specify any pwrite() errors related to pipes, FIFOs, and sockets other than [ESPIPE].

FUTURE DIRECTIONS

None.

SEE ALSO

chmod(), creat(), dup(), fcntl(), getrlimit(), lseek(), open(), pipe(), read(), writev()

XBD <limits.h>, <sys/uio.h>, <unistd.h>

CHANGE HISTORY

First released in Issue 1. Derived from Issue 1 of the SVID.

Issue 5

The DESCRIPTION is updated for alignment with the POSIX Realtime Extension and the POSIX Threads Extension.

Large File Summit extensions are added.

The pwrite() function is added.

Issue 6

The DESCRIPTION states that the write() function does not block the thread. Previously this said "process" rather than "thread".

The DESCRIPTION and ERRORS sections are updated so that references to STREAMS are marked as part of the XSI STREAMS Option Group.

The following new requirements on POSIX implementations derive from alignment with the Single UNIX Specification:

Text referring to sockets is added to the DESCRIPTION.

The following changes were made to align with the IEEE P1003.1a draft standard:

The DESCRIPTION is updated for alignment with IEEE Std 1003.1j-2000 by specifying that write() results are unspecified for typed memory objects.

The following error conditions are added for operations on sockets: [EAGAIN], [EWOULDBLOCK], [ECONNRESET], [ENOTCONN], and [EPIPE].

The [EIO] error is made optional.

The [ENOBUFS] error is added for sockets.

The following error conditions are added for operations on sockets: [EACCES], [ENETDOWN], and [ENETUNREACH].

The writev() function is split out into a separate reference page.

IEEE Std 1003.1-2001/Cor 2-2004, item XSH/TC2/D6/146 is applied, updating text in the ERRORS section from "a SIGPIPE signal is generated to the calling process" to "a SIGPIPE signal shall also be sent to the thread".

IEEE Std 1003.1-2001/Cor 2-2004, item XSH/TC2/D6/147 is applied, making a correction to the RATIONALE.

Issue 7

The pwrite() function is moved from the XSI option to the Base.

Functionality relating to the XSI STREAMS option is marked obsolescent.

SD5-XSH-ERN-160 is applied, updating the DESCRIPTION to clarify the requirements for the pwrite() function, and to change the use of the phrase "file pointer" to "file offset".

POSIX.1-2008, Technical Corrigendum 1, XSH/TC1-2008/0742 [219], XSH/TC1-2008/0743 [215], XSH/TC1-2008/0744 [79], and XSH/TC1-2008/0745 [215] are applied.

POSIX.1-2008, Technical Corrigendum 2, XSH/TC2-2008/0401 [676,710] and XSH/TC2-2008/0402 [966] are applied.

Issue 8

Austin Group Defect 308 is applied, clarifying the handling of [EFBIG] errors.

Austin Group Defect 1330 is applied, removing obsolescent interfaces.

Austin Group Defect 1430 is applied, clarifying that requirements relating to data interleaving on pipes and FIFOs apply to write operations in other threads, not just other processes, and changing some uses of "pipe" to "pipe or FIFO".

Austin Group Defect 1669 is applied, removing XSI shading from part of the [EFBIG] error relating to the file size limit for the process.

End of informative text.

 

return to top of page

UNIX? is a registered Trademark of The Open Group.
POSIX? is a Trademark of The IEEE.
Copyright ? 2001-2024 The IEEE and The Open Group, All Rights Reserved
[ Main Index | XBD | XSH | XCU | XRAT ]

血小板偏高是什么意思 树木什么 类风湿吃什么药有效 结核抗体弱阳性什么意思 有什么四字词语
例假为什么第一天最疼 辛是什么意思 血液四项检查是什么 康熙雍正乾隆是什么关系 眼仁发黄是什么原因
阴道发白是什么原因 气虚什么症状 mpe是什么意思 狮子吃什么食物 1985年属牛是什么命
竹肠是什么部位 为什么拉屎是绿色的 菠菜不能和什么一起吃 yeezy是什么牌子 夏天流鼻血是什么原因
被银环蛇咬了有什么症状hcv9jop0ns3r.cn 糖尿病人适合吃什么水果hcv8jop0ns2r.cn 黑怕是什么意思hcv9jop5ns3r.cn 光脚走路有什么好处yanzhenzixun.com 下腹痛挂什么科gangsutong.com
美白吃什么hcv9jop8ns2r.cn 属马是什么命mmeoe.com 一什么之什么成语hcv8jop7ns7r.cn 男人下面胀痛什么原因造成呢96micro.com 为什么要活着hcv7jop5ns5r.cn
酸奶什么时候喝最好hcv8jop6ns4r.cn 踏实是什么意思hcv8jop8ns3r.cn 处女膜破了有什么影响hcv9jop2ns7r.cn 血压低说明什么hcv8jop9ns9r.cn calcium是什么意思hcv9jop6ns9r.cn
梦见已故朋友什么预兆hcv9jop5ns3r.cn 企鹅是什么动物hcv9jop0ns7r.cn 痰湿中阻吃什么中成药hcv9jop3ns1r.cn 什么纸贵hcv8jop0ns3r.cn 痛风可以吃什么水果hcv9jop5ns1r.cn
百度