This shows an example of low level operating system programming that I use for instructional purposes.
There are two options:
- Output the last N lines of a file.
- Continuously output the content of a file that is growing.
- Position to the end of the file (without reading anything)
- Start reading backwards, counting
newline
characters - When enough lines have been read, output the lines
The key is not reading all the contents of the file, which yields a large performance gain which increases as the file size grows.
- Read and output the contents of the file until the end is reached
- Sleep for a while
- Reset the
end-of-file
indication so that Step 1 can resume