[英]What's the use of `offset` in the version of `Atomic::cmpxchg` in Hotspot?
[英]atomic append on a file descriptor, but at what offset?
在unistd.h
使用帶有O_APPEND標志的open()會將原子寫入始終寫入文件的末尾...
這很好,但是如果我需要知道它原子地附加到文件的偏移量怎么辦?
我意識到O_APPEND經常被用於日志文件,但我實際上想知道它在原子上附加的文件中的偏移量。
我沒有看到任何明顯的方法來做到這一點..? 有人知道嗎?
謝謝
要獲取文件描述符中的當前位置,請使用偏移0
lseek()
和SEEK_CUR
。
int fd = open(...);
if (fd) {
off_t positionWhereAppendingBegins = lseek(fd, 0, SEEK_CUR);
write(...);
close(fd);
}
請注意,如果以其他方式打開描述符,即通過socket()
,這將不會給您可靠的結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.