Operating Systems (original) (raw)

File System Calls


System Calls : program makes a request to the OS for a service; looks like a C function call

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:

open System Call

Example Flag values:


Open File Descriptor Table

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

fd = open(); read (fd, ...); /* reads from a file */


To Redirect Input


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