Home:ALL Converter>What is the difference between read and pread in unix?

What is the difference between read and pread in unix?

Ask Time:2009-11-06T20:30:26         Author:Vijay

Json Formatter

What is the difference between the functions read() and pread() in unix?
When choosing between them, what points should I take into consideration?

I googled for the difference between them but without results.

Author:Vijay,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/1687275/what-is-the-difference-between-read-and-pread-in-unix
ihuk :

Pread() works just like read() but reads from the specified position in the file without modifying the file pointer.\n\nYou would use it when you need to repeatedly read data at fixed offset, for example a database index that points to individual records in file, to save on seek() calls.\n\nBasically use read() if your data is sequential or pread() if you know, or can calculate offset at which to read. ",
2009-11-06T12:34:45
user59634 :

From this pread link,\n\nThe atomicity of pread enables\nprocesses or threads that share file\ndescriptors to read from a shared file\nat a particular offset without using a\nlocking mechanism that would be\nnecessary to achieve the same result\nin separate lseek and read system\ncalls. Atomicity is required as the\nfile pointer is shared and one thread\nmight move the pointer using lseek\nafter another process completes an\nlseek but prior to the read.\n",
2009-11-06T12:34:46
mouviciel :

Google gave me man pread.\n\nIf you read() twice, you get two different results, which shows that read() advances in the file.\n\nIf you pread() twice, you get the same result, which shows that pread() stays at the same point in the file.",
2009-11-06T12:37:29
yy