The words in a file of size n are addressed by the numbers 0 to n - 1. The first (or leftmost) word in the file is word 0; the last (or rightmost) word in the file is word n - 1. If two words are adjacent in a file and the first word is word i, then the second word is word i + 1; that is, word address increase consecutively and sequentially from the first word of the file.
When a file is opened, the file pointer references the first word in the file, or end of file if the file is empty. Read and write operations take place relative to the file pointer, and move the file pointer to reference the word after the data read or written, or the end of file.
There is one file pointer per opened file. If a file is opened more than once, it will have more than one file pointer; for example, a file opened three times will have three file pointers. File pointers are independent of one another; an operation affecting one of a file's file pointers doesn't affect any of the other file pointers.
system_call::open
)device::file
. Register 3 contains the integer identifying the file to
open. Register 2 contains a set of flags indicating how the file should be
opened:
file_io::read
file_io::write
file_io::create
file_io::exists
file_io::exclusive
file_io::shared
The flag set may contain both reading and writing; if neither are given, reading is assumed. The flag set may contain both create and exists; if neither are given, exists is assumed. The flag set may contain either exclusive or shared, but not both; if neither is given, shared is assumed.
After the open system call is completed, CPU register 0 contains the system call status:
status::ok
device::device_busy
device::bad_device
device::disk
device::terminal
, or device::file
.
If the open system call completed successfully, CPU register 1 contains a file identifier, which must be included as an argument to other system calls related to this file. If the open system call didn't complete successfully, the contents of CPU register 1 is undefined.
system_call::close
)After the close system call is completed, CPU register 0 contains the system call status:
status::ok
status::bad_device
If the close system command completes successfully, the file id held by the user process becomes invalid and may not usefully be used in any other file-oriented system call.
system_call::read
)After the read system call is completed, CPU register 0 contains system call status:
status::ok
status::bad_device
status::bad_address
status::bad_count
If the read system command completes successfully, CPU register 1 contains the number of words read. The number of words actually read may be smaller (never larger) than the number of words requested if the number of words from the file pointer to the end of the file is less than the requested number of words to read.
system_call::write
)After the write system call is completed, CPU register 0 contains the system call status:
status::ok
status::bad_device
status::bad_address
status::bad_count
system_call::seek
)'c'
, 'b'
, or 'e'
:
'c'
: Offset from the current file pointer location. If
the file pointer is pointing at word p in a file, and register 3
contains i, then the file pointer will be pointing at the word p +
i after the seek call is done.
'b'
: Offset from the beginning of the file. If register 3
contains i, then the file pointer will be pointing at the word i
after the seek call is done.
'e'
: Offset from the end of the file. If the file contains
n words and register 3 contains i, then the file pointer will be
pointing at the word n - i after the seek call is done.
It is not an error to attempt to move the file pointer past either end of a file; in such cases, the file pointer moves as far as it can and stops.
After the write system call is completed, CPU register 0 contains the system call status:
status::ok
status::bad_device
status::bad_address
'c'
, 'b'
, or
'e'
.
Register 1 contains the number of the word being referenced by the file pointer.
system_call::remove
)After the write system call is completed, CPU register 0 contains the system call status:
status::ok
status::bad_device
A file that's opened can be removed; the remove should be delayed until all opens on the file are closed. A file that's undergoing delayed deletion can be opened by other processes.
pa4-simple-fio.dsk
- Contains a process that forks two other
processes. The first process forked creates a file and writes the filename
into the file. The second process opens the created file, reads its contents,
compares it to the filename, and removes the file. The processes are
self-checking; if there's no output, everything went o.k.
$ /e*/h*/c*/*5/S*/bin/os4 -Dins-cnts -d /e*/h*/c*/*5/d*/pa4-simple-fio.dsk The system is halted. Total execution time: 155 ticks, idle time: 63 ticks (40%). Op-code: count add: 65 move: 38 sysc: 14 cmpr: 14 beq: 14 $
pa4-double-simple-fio.dsk
- Runs the processes in
pa-simple-fio.dsk
twice.
$ /e*/h*/c*/*5/S*/bin/os4 -Dins-cnts -d /e*/h*/c*/*5/d*/pa4-double-simple-fio.dsk The system is halted. Total execution time: 341 ticks, idle time: 136 ticks (39%). Op-code: count add: 140 move: 83 sysc: 33 cmpr: 32 beq: 32 $
pa4-bad.dsk
- Contains a process that tries to do bad things with
the file system. It is self-checking; if there's no output, everything went
o.k.
$ /e*/h*/c*/*5/S*/bin/os4 -Dins-cnts -d /e*/h*/c*/*5/d*/pa4-bad.dsk The system is halted. Total execution time: 176 ticks, idle time: 59 ticks (33%). Op-code: count add: 60 bne: 10 move: 56 sysc: 17 cmpr: 16 beq: 6 $
This page last modified on 19 August 2002.