Книга: Programming with POSIX® Threads

10.1.5 Parallel I/O

10.1.5 Parallel I/O

Many high-performance systems, such as database engines, use threads, at least in part, to gain performance through parallel I/O. Unfortunately, Pthreads doesn't directly support parallel I/O. That is, two threads can independently issue I/O operations for files, or even for the same file, but the POSIX file I/O model places some restrictions on the level of parallelism.

One bottleneck is that the current file position is an attribute of the file descriptor. To read or write data from or to a specific position within a file, a thread must call lseek to seek to the proper byte offset in the file, and then read or write. If more than one thread does this at the same time, the first thread might seek, and then the second thread seek to a different place before the first thread can issue the read or write operation.

The X/Open pread and pwrite functions offer a solution, by making the seek and read or write combination atomic. Threads can issue pread or pwrite operations in parallel, and, in principle, the system can process those I/O requests completely in parallel without locking the file descriptor.

pread

size_t pread (

int fildes,

void *buf,

size_t nbyte,

off_t offset);

Read nbyte bytes from offset offset in the file opened on file descriptor fildes, placing the result into buf. The file descriptor's current offset is not affected, allowing multiple pread and/or pwrite operations to proceed in parallel.

References: none

Errors: [EINVAL]offsetisnegative.

[EOVERFLOW] attempt to read beyond maximum.

[ENXIO] request outside capabilities of device.

[ESPiPE] file is pipe. Hint: Allows high-performance parallel I/O.

pwrite

size_t pwrite (

int fildes,

const void *buf,

size_t nbyte,

off_t offset);

Write nbyte bytes to offset offset in the file opened on file descriptor fildes, from buf. The file descriptor's current offset is not affected, allowing multiplepread and/ or pwrite operations to proceed in parallel.

References: none

Errors: [EINVAL] offset is negative.

[ESPIPE] file is pipe. Hint: Allows high-performance parallel I/O.

Оглавление книги


Генерация: 0.043. Запросов К БД/Cache: 0 / 0
поделиться
Вверх Вниз