【疑问求助】lseek函数和pwrite函数《APUE》

JasonLeaster 2014-02-05 06:05:30
pwrite是atomic operation的产物
操作1.对于单进程操作来先调用lseek 然后调用write
操作2.直接调用pwrite
APUE上说是等价的,只要操作1在调用lseek和write之间,不被打断,1,2就是等价的
原话:
Calling pread is equivalent to calling lseek followed by a call to read,with the
following exceptions.
•There is no way to interrupt the two operations that occur when we call pread.
•The current file offset is not updated.

Calling pwrite is equivalent to calling lseek followed by a call to write,with similar
exceptions.

In general, the term atomic operation refers to an operation that might be composed
of multiple steps. If the operation is performed atomically ,either all the steps are performed (on success) or none are performed (on failure).

APUE没有给出示例代码
我自己练习时候发现了问题
ssize_t pwrite(int fd ,const void *buf,size_tnbytes ,off_t offset);
这个是pwrite的原型
观察,最后一个参数,offset,这个变量怎么得到呢?除了lseek返回offset还能怎样
但是pwrite从逻辑上讲又是lseek+write
这个时候是没有offset参数来源的,必须在pwrite之前调用一个lseek得到当前offset,以此来作为pwrite的参数
但是问题来了,如果这样调用的话,那么pwrite就显得很没意义,因为都是lseek+write或则lseek+pwrite
pwrite反而显得多此一举了

渣渣愚昧,恳请大神明示
...全文
208 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
arthurzhuyong 2014-02-05
  • 打赏
  • 举报
回复
$man pwrite ........ pwrite() writes up to count bytes from the buffer starting at buf to the file descriptor fd at offset offset. The file offset is not changed. ............
JasonLeaster 2014-02-05
  • 打赏
  • 举报
回复
引用 4 楼 arthurzhuyong 的回复:
我不可以指定一个定值offset,这里offset是动态的 APUE不是讲File Hole 了么, 打开一个文件, 我打算每隔10M写两个字符. 没offset如何实现?
我说过了啊。。。是动态的offset,你还是没看我的代码。。。。。。。。。Just read the fucking source code --linus torvalds
arthurzhuyong 2014-02-05
  • 打赏
  • 举报
回复
我不可以指定一个定值offset,这里offset是动态的 APUE不是讲File Hole 了么, 打开一个文件, 我打算每隔10M写两个字符. 没offset如何实现?
JasonLeaster 2014-02-05
  • 打赏
  • 举报
回复
引用 2 楼 arthurzhuyong 的回复:
off_t lseek(int fd, off_t offset, int whence);
ssize_t write(int fd, const void *buf, size_t count);

//offset你可以指定值的.
lseek(fd, 16384, SEEK_SET)
你仔细看看我的代码,你没明白我的意图 我的意思是动态的获取文本结尾的offset,而不是自己手动的去填写offset 从STDIN多次写入文本,这样文本大小都会改变,文本末尾的offset每次都会变 我不可以指定一个定值offset,这里offset是动态的 你可以把我的源代码copy运行 程序的目的我在一开始的注释里面有讲
arthurzhuyong 2014-02-05
  • 打赏
  • 举报
回复
off_t lseek(int fd, off_t offset, int whence);
ssize_t write(int fd, const void *buf, size_t count);

//offset你可以指定值的.
lseek(fd, 16384, SEEK_SET)
JasonLeaster 2014-02-05
  • 打赏
  • 举报
回复
这是我的测试代码 /******************************************************* Code writer:EOF Code date: 2014.02.05 Code purpose: Read some characters from the standard input and then write them into the end of file"text.t" Touch me: if this code somewhere is not very well,please touch me e-mail:jasonleaster@gmail.com *******************************************************/ #include"apue.h" #include"unistd.h" #include"fcntl.h" #define BUFFSIZE 4096 int main() { int file_descriptor = 0; int bytes = 0; off_t off_set = 0; char buf[BUFFSIZE]; if((file_descriptor = open("./text.t",O_RDWR,O_APPEND)) < 0) { printf("open error\nprocess end\n"); return 0; } else { //while((bytes = pread(STDIN_FILENO,buf,BUFFSIZE-1,SEEK_CUR) >= 0)) while((bytes = read(STDIN_FILENO,buf,BUFFSIZE-1)) > 0) { if((off_set = lseek(file_descriptor,0,SEEK_END)) < 0) { printf("lseek error\n"); } // if(write(file_descriptor,buf,bytes) != bytes) if(pwrite(file_descriptor,buf,bytes,off_set) != bytes) { printf("pwrite error\nprocess end\n"); return 0; } } } exit(0); }

23,217

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧