File System Calls
System Calls :
program makes a request to the OS for a service; looks like a C function call
- see man 2 system calls
- see man 2 open
- open : system call to open a file
- open returns a file descriptor, an integer specifiying the
position of this open file in the table of open files for the current process
- creat : (no 'e') system call to create a file       /* no longer necessary */
- close : system call to close a file
- read : read data from a file opened for reading
- write : write data to a file opened for writing
- lseek : seek to a specified position in a file (used for random access when reading or writing a file)
- the original seek system call used a 16 bit address
- "lseek" is named because it uses a "long" integer as the
address to seek to. In practice this is a 32 bit integer
on most current machines
- the UNIX standards organizations have apparently not
yet standardized a suitable seek system call for 64 bit addresses
- Currently (January 2005), the llseek system
call is available on Solaris (hercules), but there is no
indication that this system call is portable
- Versions of Linux apparently support _llseek() or llseek() for
raw devices. Compilers may support lseek() as accepting a 64-bit
long or add lseek64(), according to a compile-time option.
open
SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open (const char *path, int oflag);
DESCRIPTION
path points to a path name naming a file. open opens a file descriptor
for the named file and sets the file status flags according to the value
of oflag.
O_RDONLY
Open for reading only.
O_WRONLY
Open for writing only.
O_RDWR Open for reading and writing.
read
SYNOPSIS
#include <unistd.h>
ssize_t read(int fildes, void *buf, size_t nbyte);
DESCRIPTION
read attempts to read nbyte bytes from the file associated with fildes
into the buffer pointed to by buf. If nbyte is zero, read returns zero
and has no other results.
write
SYNOPSIS
#include <unistd.h>
ssize_t write(int fildes, const void *buf, size_t nbyte);
DESCRIPTION
write attempts to write nbyte bytes from the buffer pointed to by buf to
the file associated with fildes. If nbyte is zero and the file is a
regular file, write returns zero and has no other results. fildes is a
file descriptor.
close
SYNOPSIS
#include <unistd.h>
int close(int fildes);
DESCRIPTION
close closes the file descriptor indicated by fildes.
stat
SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>
int stat(const char *path, struct stat *buf);
DESCRIPTION
stat obtains information about the named file. For more information, please read Section 2.8 in Interprocess Communications in UNIX:
The Nooks & Crannies.
In C, you use the fopen, fread, fwrite, fclose library functions:
- file pointer => fp
- fp = fopen("myfile.dat", "rb"); says open a file called myfile.dat for reading binary data.
- fopen is a 'C' library function that calls open, the true system call on UNIX
- on any OS, the implementation of the fopen library must eventually call the system call for that OS. The relevant system call is almost always known as open.
open System Call
- open (path, flags, mode)
- path is a function (i.e. /net/share/etc)
- flags - see man 2 open
Example Flag values:
- O_RDONLY: read only
- O_RDWR: read and write
- O_APPEND: append to file
- fd: file descriptor
- int fd;
- fd = open ("myfile", O_RDONLY, 0755);
      /* if it does not exist, create it with the given mode */
- mode => rwxr-xr-x
Open File Descriptor Table
- the fd table is an array
- for one running process
- associated with every process
- list of open files stored in the fd table
- each entry corresponds to one open file
- that is copied when fork is executed
- e.g.:
- each process has its own table
- open: add an entry to the table (new file created)
- close: delete an entry from the table
- this is for a "normal" process, one with no redirection of i/o (no forks)
- fd # from 0 to 31
0- ___________
| 0 | - stdin -- keyboard
| 1 | - stdout -- screen
| 2 | - stderr -- screen
| 3 | - file 'A', closed, now a NULL value
| 4 | - "myfile"
| 5 |
| 6 |
| 7 |
| |
| . |
| : |
31-| 31 |
fork => copy of this table goes with the new process
Redirection
In the UNIX shell (csh utiltiy program), the symbols '<' and '>'
cause output to be redirected. This is doen by changing the entries
in the file descriptor table.
printf
-- always sends output to the file/device described by
entry #1 in the fd table
fprintf (stderr, ...)
-- sends output to the file/device described by entry #2
scanf, getchar
-- take input from the file/device described by entry #0
- opening a file for reading adds an entry in the next free position in the fd table
- if two files are opened and the first is later closed, a NULL replaces that files in the fd table
- the other file entries in the fd table do not move up to fill the gap
- when a new file is opened,
it takes the place of the first available (NULL) place in the
file descriptor table.
fd = open();
read (fd, ...);       /* reads from a file */
To Redirect Input
copy the fd from entry #4 to entry #0       /* stdin always corresponds to entry #0 */
the copy command is called dup2       /* system call */
usage: dup2 (from position #, to position #)
e.g. dup2 (4, 0);       /* in code we use dup2 (fd, 0); */
- we now have 2 fds for the same file (i.e. #0 & #4)
- it is standard policy to remove one -- close (file_fd);
- we do this after a dup2
- we do not want 2 file descriptors for the same file
- with dup2 executed, we now have permanently lost our ability to read from the keyboard
- Csh always keeps copies of position #0, #1 & #2 and restores them when using the dup2 command
e.g. % a.out > output
- dup system call: fd2 = dup(fd 1)
- copies the entry from position fd #1 into the first free position in the fd table
- this position is returned to fd2
- we can use this to store fd #0, #1 & #2
before copying (executing dup2) so that you can later restore them
Output Redirection
e.g. %a.out > outfile
int outfd;
outfd = open ("outfile", O_WRONLY, 0700);
dup2 (outfd, 1);
tee : UNIX system program that prints to one file,
then prints to a second file
Table of Contents