Take the 2-minute tour ×

Read in text books that there are mainly two file access methods; sequential and direct. Which one we are using in Linux?

In read command we are giving the how much bytes to read and to which buffer. So we are having sequential access in Linux?

But physically we have files stored is blocks? I couldnt relate to it.

Whether direct access possible in Linux?

I read about these access models in Operating System Concepts by Galvin

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

Both are possible.

When you do a read on an ordinary file, it does read the file sequentially, advancing the file pointer each time by the right amount.

But you can also use seek to move to an arbitrary point in the file.

Not all files support random/direct access. Pipes for instance are typically only sequential access (you can't rewind or skip forward).

So pretty much everything is possible, but some file types have restrictions.

(File access with direct I/O (O_DIRECT flag) is a different concept altogether.)

share|improve this answer
add comment

You can certainly read/write from an arbitrary position in an open (disc) file.

There are a number of methods of doing random IO, which are optimised for different kinds of usage.

  • The simplest method is seek() followed by read() or write(). The file pointer moves on by the amount of bytes read/written, and it can allow sequential IO following a random jump. Consider seek() as logically spinning the an old "reel-to-reel" tape drive (even though we don't have these any more).
  • The pread and pwrite system calls combine seek() and read/write(), specifically for use in multithreaded programs (where two syscalls would result in a race condition). They don't change the file pointer, so you can think of it logically just taking or putting a random bit of data.
  • mmap() maps a file into memory - where you can then do with it, what you will, using conventional pointer/ memory manipulation (for example, memset, memcpy, etc).
share|improve this answer
add comment

Your Answer


