<<

File and I/O System Calls

System Call Parameters Returns Notes

name, flags fid Connect to open file creat name, mode fid Creates file and connect to open file fid, buffer, count count Reads bytes for open file fid, buffer, count count Writes bytes to open file lseek fid, offset, mode offset Moves position of next read or write fid code Disconnect open file unlink name code Delete named file

In order to use a file, you first need to ask for it by name. This is called opening the file. The open creates an object called an open file. The open file is logically connected to the file you named in the open system call. An open file has a file location associated with it. It is the offset in the file where the next read or write will start. After you open a file, you can use the read or write system calls to read or write the open file. Each read or write system call increments a file location for a number of characters read or written. Thus, the file is read (or/and written) sequentially by default. The lseek system call is used to achieve random access into the file since it changes the file location that will be used for the next read or write. You close the open file using the close system call, when you are done using it.

• int open (char * pathname, int openFlags); The named file is opened and a positive integer, the open file identifier is returned. The file location is set to 0. The flags can be one of the following: 0 – open for reading 1 – open for writing 2 – open for reading and writing

If an error occurs in trying to open the file, a negative integer is returned. For causes of different negative values see man pages.

• int creat (char * pathname, int fileMode); The named file is created as an empty file and opened for writing, and a positive integer, the open file identifier is returned. The file location is set to 0. The fileMode defines file access rights.

If an error occurs in trying to create the file, a negative integer is returned. For causes of different negative values see man pages.

The open file identifiers returned by open and creat are local to the process making the calls. Thus, open file identifier 3 in one process is not related to open file identifier in another process.

• int close (int openFileID); The file is closed. A return code 0 means the close succeeded. A return value -1 means that the openFileID did not refer to open file.

1 • int unlink (char * pathname); The file is deleted. A return code 0 means the unlink succeeded. If an error occurs in trying to delete a file, a negative integer is returned. For causes of different negative values see man pages.

• int read (int openFileID, char * bufferAddress, int count); The operating system tries to read count bytes from the open file designated by openFileID. The bytes are read from the file starting at the offset provided by the file location. If fewer then count bytes are left before the end of the file all remaining bytes are read. This is not considered to be an error. The bytes read are places into the array of bytes starting at bufferAddress. The file location is increased by the number of bytes read. The return value is the number of bytes that were actually read, and this might be less than count. A return value 0 indicates that the end of file has been reached and the file location is equal to the size of the file, thus no more bytes can be read. A negative return value indicates some error occurred in the read. For causes of different negative values see man pages.

Note that the read system call specifies which file to read, how much to read, and where to put the read characters, but it does not indicate which characters in the file to read, i.e. where in the file to read. The location into file is not passed with the call but it is kept with open file information as a part of the file state.

• int write (int openFileID, char * bufferAddress, int count); The operating system tries to write count bytes to the open file designated by openFileID. The bytes are written to the file starting at the offset provided by the file location. The bytes are taken from the array of bytes starting at bufferAddress. The file location is increased by the number of bytes read. The return value is the number of bytes that were actually written. A negative return value indicates some error occurred in the write. For causes of different negative values see man pages.

Note that the write system call specifies into which file to write, and from where to take and how many characters for writing, but it does not indicate where in the file to write. The location into file is not passed with the call but it is kept with open file information as a part of the file state.

• int lseek (int openFileID, inf offset, int moveMode); The file location associated with the open file is changed to a new value. The new value is computed by adding the offset to the base. The base is selected according to the moveMode as follows: 0 – use 0 as a base (move from the beginning of the file) 1 – use the current file location as a base (for relative move) 2 – use the current as a base (move from the end of the file). Positive values of the offset will cause the next write to be beyond the current end of the file. A 0 value will cause the next write to extend the file.

• The returned value is the new file position. An lseek with an offset of 0 and a moveMode of 1 will not change the file position but will return the current file position. If an error occures, a negative integer is returned. For causes of different negative values see man pages.

2