OS interface: file and I/O system calls - PowerPoint PPT Presentation

About This Presentation
Title:

OS interface: file and I/O system calls

Description:

OS interface: file and I/O system calls File operations in C/C++? fopen(), fread(), fwrite(), fclose(), fseek() in C f.open( ), f.close( ) in C++ – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 10
Provided by: xyu7
Learn more at: http://www.cs.fsu.edu
Category:
Tags: calls | file | interface | system

less

Transcript and Presenter's Notes

Title: OS interface: file and I/O system calls


1
OS interface file and I/O system calls
  • File operations in C/C?
  • fopen(), fread(), fwrite(), fclose(), fseek() in
    C f.open(), f.close() in C
  • I/O operation in C/C
  • printf(), scanf() in C
  • cin, cout, cerr in C
  • At the UNIX OS interface, File and I/O operations
    in the UNIX system share the same interface.
  • open(), read(), write(),close()
  • I/O devices are treated as special files (see
    /dev directory)

2
  • File .vs. open file
  • file - passive container of data a named
    sequence of bytes
  • open file a file that is opened in a process
  • active sources and sinks for data, need to
    maintain current position of the open file.
  • File
  • Current file position
  • Maintained by the OS
  • May shared among different processes

3
  • File and I/O system calls
  • open a file
  • int open(const char path, int flags, mode_t
    modes)
  • // path file name, flags type of open, modes
    file permission, only when the file is being
    created.
  • // return a non negative number when successful,
    -1 when fail
  • Example fd open(/etc/passwd, O_RDONLY)
  • Each open creates a new open file (even if the
    same file is opened).
  • read a file
  • ssize_t read(int fd, void buf, size_t nbyte)
  • // read nbyte data from file fd to buffer buf
  • // fd file descriptor
  • // buf pointer to the buffer to hold the data
    from the file
  • // nbyte number of bytes to be read
  • // return total number of bytes read, 0 for end
    of file, -1 when error
  • Example number read(fd, buffer, count)

4
  • File and I/O system calls (continue)
  • write to a file
  • ssize_t write(int fd, const void buf, size_t
    nbyte)
  • Example number write(fd, buffer, count)
  • close a file
  • int close(int fd)
  • create a file
  • int creat(const char path, mode_t mode)
  • // the meaning of mode,
  • // also determined by mask, see man -a umask.
  • creat(path, mode)
  • open(path, O_WRONLY O_CREAT O_TRUNC, mode)

5
void FileCopy(char fromFile, char toFile)
int fromFD open(fromFile, O_RDONLY) if
(fromFD lt 0) cerr ltlt Error opening ltlt
fromFile ltlt \n return int toFD
creat(toFile, 0666) if (toFD lt 0)
cerr ltlt Error opening ltlt toFile ltlt \n
close(fromFD) return while (1)
char ch int n read (fromFD, ch, 1)
if (n lt 0) break // end of file
n write(toFD, ch, 1) if (n lt 0) cerr
ltlt Error\n return close(fromFD)
close(toFD) Example1.cpp
6
Control the position to read/write lseek
from/to where the file is read/written (file
pointer, an implicit parameter) include
ltsys/types.hgt include ltunistd.hgt off_t lseek(int
fd, off_t offset, int whence) whence SEEK_SET
begin offset SEEK_CUR
current offset SEEK_END end
offset Example lseek(fd, (off_t)(-1),
SEEK_CUR) lseek(fd, (off_t)0,
SEEK_END) lseek(fd, (off_t)0,
SEEK_SET)
7
// Example2.cpp void ReverseFile(char fromFile,
char toFile) int fromFd open(fromFile,
O_RDONLY) int toFd creat(toFile, 0666)
lseek(fromFd, (off_t)(-1), SEEK_END)
while(1) char ch int n
read(fromFd, ch, 1) n write(toFd, ch,
1) if (n lt 0) cerr ltlt Error\n
exit(0) if (lseek(fromFd, (off_t)(-2),
SEEK_CUR) lt 0) break close(toFd)
close(fromFd)
8
  • File meta-information - information about the
    file that isnt in the file, such as
  • Owner, permissions, timestamps, size, etc.
  • Try an ls -l on a typical UNIX file
  • include ltsys/types.hgt
  • include ltsys/stat.hgt
  • int stat(const char path, struct stat buf)
  • Determine whether file path exists.
  • Determine whether the file is executable.
  • Other meta-information, such as access time, etc.
  • check out what you can do with utime.

9
// example3.cpp int check_executable(char
filename) struct stat buf int j
stat(filename, buf) if (j (-1)) return
FILE_NOT_EXIST if (buf.st_mode 0111)
return EXECUTABLE return NOT_EXECUTABLE
Write a Comment
User Comments (0)
About PowerShow.com