Read / pread system call thread security

I have several requests related to read () / pread () system calls in a multi-threaded environment

I use Mac-OSX, which is based on freeBsd, if that helps anyway I use this file only in read mode, not read / write AND c / C ++ language

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

and 4 alphabets correspond to one page of the file

So, Page1: AAAA

Page 2: BBBB ..... etc.

now i am starting to read a system call from two different threads with the same file descriptor my intention is to read the first page from stream 1, the second page from stream 2, ... etc.

read (FD, positive effect, SizeOf (pages));

From the man page I was given to understand that reading also increases the pointer to the file, so I definitely get distorted answers, for example

ABCC ABBB .. etc. (without a specific sequence)

to fix this i can use pread ()

"Pread () performs the same function, but reads from the fied specification in a file without changing the file pointer" // from man pages

But I'm not sure if using pread really helps me in my goal, because even if it does not increase the internal file pointer, there is no guarantee that the answers will not be mixed up.

All my data is page aligned and I want to read one page from each stream, for example

Theme 1 reads: AAAA Theme 2 reads: BBBB Theme 3 reads: UDP ... without actually distorting the content.

I also found the message Is it safe to read () from a file as soon as write () returns?

but it was not very helpful.

I'm also not sure if read () will really have the problem I'm thinking of. The file I am reading is a binary file, and therefore it is difficult for me to simply read and check manually quickly.

Any help would be appreciated

+8
c multithreading asynchronous thread-safety
source share
3 answers

read and write change the position of an open open file. They are "thread safe" in the sense that your program will not have undefined behavior (crash or worse) if several threads perform IO in the same open file at the same time as they are used, but the order and atomicity of operations may vary depending on the type file and implementation.

On the other hand, pread and pwrite do not change the position in the open file. They were added to POSIX exactly the way you want: performing I / O operations in one open file from several threads or processes without interfering with another position. You may encounter some problems when ordering if you mix pread and pwrite (or several pwrite calls) with overlapping parts of the file, but as long as you avoid this, they are completely safe for what you want to do.

+7
source share

fcntl advisory locks are locks in a file range. You may find this useful for serializing reading and writing to the same region, allowing concurrency in individual regions.

 int rc; struct flock f; f.l_type = F_RDLCK; /* or F_WRLCK */ f.l_whence = SEEK_SET; f.l_start = n; f.l_len = 1; while ((rc = fcntl(fd, F_SETLKW, &f)) == -1 && errno = EINTR) ; if (rc == -1) perror("fcntl(F_SETLKW)"); else { /* do stuff */ f.l_type = F_UNLCK; fcntl(fd, F_SETLK, &f); } 

At the same time, several reader locks are allowed, and blocking one script blocks all the others.

We will warn that all file locking mechanisms are finely divided into some configurations on all platforms .

0
source share

Share the mutex lock between the two threads, enable the lock in the thread before reading it, and open the lock when the correct read is complete. See pthread_mutex_create , pthread_mutex_lock and pthread_mutex_unlock .

-one
source share

All Articles