File-Handling System Calls

Opening Files

fd=open(path, flag, mode);
  • Input Params
    • Path: relative/absolute
    • Flag: write/read/readwrite
    • Mode: Access rights
  • Return value: file descriptor
    • File handling data structures (such as file pointer) and pointers to kernel functions
  • There can be multiple file descriptors for the same file (eg someone reading and someone writing)

Creating Files

fd=creat(path, flag, mode);

Accessing Opened Files

newoffset=lseek(fd, offset, whence);
  • Modifies where the file pointer points to
  • Input Params
    • fd: File descriptor
    • offset: Address offset
    • whence: Specifies where the offset should be added, beginning of file, current file pointer, or offset from the end of the file
  • Return Value: Returns the new file pointer
nread=read(fd, buf, count);
  • Reads data and returns the number of bytes read
  • Input Params:
    • fd: File descriptor
    • buf: Address of buffer where data will be transferred when read
    • count: Number of bytes to be read
  • Return value: Number of bytes read

Closing a File

res=close(fd);
  • Closes a file descriptor such that it does not refer to a file anymore
  • Input Params: A file descriptor
  • Return Value: 0 if successful -1 if not successful in closing

Renaming/Deleting a File

res=rename(oldpath, newpath);
  • Changes the name of the file link and returns a flag
  • Input Param: oldpath, newpath
  • Return Value: 0 if successful -1 if not successful
res=unlink(pathname);
  • Decreases the file link count and removes the corresponding directory entry in the file descriptor
  • Input Param: Path name
  • Return Value: 0 on success and -1 on error

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s