网页资讯视频图片知道文库贴吧地图采购
进入贴吧全吧搜索

 
 
 
日一二三四五六
       
       
       
       
       
       

签到排名:今日本吧第个签到,

本吧因你更精彩,明天继续来努力!

本吧签到人数:0

一键签到
成为超级会员,使用一键签到
一键签到
本月漏签0次!
0
成为超级会员,赠送8张补签卡
如何使用?
点击日历上漏签日期,即可进行补签。
连续签到:天  累计签到:天
0
超级会员单次开通12个月以上,赠送连续签到卡3张
使用连续签到卡
01月02日漏签0天
linux吧 关注:534,488贴子:2,573,622
  • 看贴

  • 图片

  • 吧主推荐

  • 视频

  • 游戏

  • 2回复贴,共1页
<<返回linux吧
>0< 加载中...

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

  • 只看楼主
  • 收藏

  • 回复
  • 等待Gaara
  • -----r--
    4
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
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反而显得多此一举了
渣渣愚昧,恳请大神明示更多0


  • 等待Gaara
  • -----r--
    4
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
这是我的测试代码
/*******************************************************
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);
}


2026-01-02 15:07:22
广告
不感兴趣
开通SVIP免广告
  • 甘道夫_白
  • -------x
    1
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
小弟不才,前来挖坟!
pread、pwrite不是如楼主所说没有用,而是有大用。
首先pread、pwrite提出的背景是早期的UNIX没有正确地实现O_APPEND,因而引入一对原子操作,来解决append过程中的“脏写”问题。解决方法就是靠第四个参数来给每个进程(线程)分配各自的读写空间,避免它们的读写空间相互覆盖。前提是程序员清楚地知道每个线程需要多大的读写空间。
其次,如果有循环读写的需求,也需要修改第四个参数,从而改变起始位置。
总之,pread、pwrite是一对很实用的随机读写接口。


登录百度账号

扫二维码下载贴吧客户端

下载贴吧APP
看高清直播、视频!
  • 贴吧页面意见反馈
  • 违规贴吧举报反馈通道
  • 贴吧违规信息处理公示
  • 2回复贴,共1页
<<返回linux吧
分享到:
©2026 Baidu贴吧协议|隐私政策|吧主制度|意见反馈|网络谣言警示