读/ $ P $垫系统调用线程安全 [英] thread safety of read/pread system calls

查看:111
本文介绍了读/ $ P $垫系统调用线程安全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有)阅读(与几个查询/ $ P $垫()系统调用在多线程环境

I have few queries related to read()/pread() system calls in a multithreaded environment

我使用的是基于FreeBSD的的Mac OSX,如果以任何方式帮助
我只使用在读模式下这个文件,而不是读/写
而语言是C / C ++

I am using Mac-OSX which is freeBsd based , if that helps in any way I am only using this file in read mode,and not read/write And the language is c/c++

假设我们有一个磁盘文件
AAAABBBBCCCCDDDEEEE ....

Suppose we have a file on disk AAAABBBBCCCCDDDEEEE....

和4字母适合于该文件的一个页面

and 4 alphabets fit on one page of the file

所以第1页:AAAA

So Page1:AAAA

第2页:BBBB
.....等等

Page2:BBBB ..... and so on

现在我开始从两个不同的线程读取系统调用使用相同的文件描述符
我的内涵是从线程1,从线程2,第二页阅读第一页..等等。

now i initiate a read system call from two different threads with the same file descriptor my intension is to read first page from thread 1, second page from thread 2,..and so on.

阅读(FD,浅黄色,sizeof的(页));

read(fd,buff,sizeof(page));

从man页面我很。据了解,读也将递增文件指针,所以肯定我将要得到这样

From the man page i am given to understand that read will also increment the file pointer ,so definitely i am gonna get garbled responses like

ABCC AB | BB ..等(没有特定的顺序)

ABCC ABBB .. etc (with no particular sequence )

要解决这个问题,我可以使用$ P $垫()

to remedy this i can use pread()

$ P $垫()执行相同的功能,但是从试样中读取
     田间文件中的位置,而无需修改从man页面文件指针//

"Pread() performs the same function, but reads from the speci- fied position in the file without modifying the file pointer" // from man pages

但我不知道是否使用$ P $垫实际上将帮助我在我的目标,导致即使它不增加内部文件指针,有没有保证的响应不错杂。

But i am not sure whether using pread will actually help me in my objective , cause even though it does not increment the internal file pointer , there are no guarantees that the responses are not jumbled.

我所有的数据都对准页,我想读取每个线程一页像

All of my data is page aligned and i want to read one page from each thread like

主题1写着:AAAA
线程2读取:BBBB
主题3写着:CCCC ......没有实际错乱的内容。

Thread 1 reads:AAAA Thread 2 reads:BBBB Thread 3 reads:CCCC ... without actually garbling the content ..

我还发现一个帖子<一个href=\"http://stackoverflow.com/questions/4486563/is-it-safe-to-read-from-a-file-as-soon-as-write-returns\">Is可以安全地从文件尽快阅读()作为write()返回?

但它不是非常有用。

我也不能肯定的read()是否真的有问题,我在想,我读of.The文件是一个二进制文件,因此,我痘痘难以迅速刚手动读取和验证..

I am also not sure whether read() will actually have the problem, that i am thinking of.The file that i am reading is a binary file and hence i litle difficult to just quickly manually read and verify..

任何帮助将AP preciated

Any help will be appreciated

推荐答案

改变位置底层打开的文件。他们是线程安全的,在这个意义上,你的程序将不会有未定义的行为(崩溃或更糟),如果多个线程同时使用它们在同一个打开的文件执行IO,但是操作的顺序和原子可以根据不同的变化文件类型和实现的。

read and write change the position of the underlying open file. They are "thread safe" in the sense that your program will not have undefined behavior (crash or worse) if multiple threads perform IO on the same open file at once using them, but the order and atomicity of the operations could vary depending on the type of file and the implementation.

在另一方面, $ P $垫 PWRITE 不改变在打开文件的位置。他们加入到POSIX的正是你想要的目的:从多个线程或进程相同的开放文件,而无需使用一个干扰彼此的位置的操作执行的IO操作。你可能仍然会碰到一些麻烦排序,如果你混合 $ P $垫 PWRITE (或多个电话到 PWRITE )重叠的部分文件,但只要你避免这种情况,他们是绝对安全的你想要做什么。

On the other hand, pread and pwrite do not change the position in the open file. They were added to POSIX for exactly the purpose you want: performing IO operations on the same open file from multiple threads or processes without the operations interfering with one another's position. You could still run into some trouble with ordering if you're mixing pread and pwrite (or multiple calls to pwrite) with overlapping parts of the file, but as long as you avoid that, they're perfectly safe for what you want to do.

这篇关于读/ $ P $垫系统调用线程安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆