Title: System Interface
1System Interface
- Interface that provides services from the OS
- More efficient
- Access facility which is not in the library
- Unix System Call
- File System
- Storage System
2File System
- A popular but also complex subsystem
- File descriptors (pseudo files, like stdin and
stdout) - Low level IO
- File Management
- Examples
file pointer
EOF
3A little about File Attributes
- Unix file system ,each file is associated with 9
bits permission information. - -rwxr-xr-x 1 lina cisgrad 120320 Sep 27
1438 lec01.ppt - -rw-r--r-- 1 lina cisgrad 133632 Oct 9
2334 lec02.ppt - read, write and execute
- an octal digit, describes the permission to a set
of users - Three sets of different users, owner, group and
others - Some UNIX commands chmod, etc to change the
attributes - chmod 755
4File Descriptor
- File pointer FILE fp
- point to a structure that contains information
about the file - Used in library functions
- File descriptor int fd
- non negative integer
- used to identify a file
- used in low level I/O (system call)
5File Descriptor
- File pointer vs File descriptor
- standard input stdin 0
- standard output stdout 1
- standard error stderr 2
- All three are automatically opened when running a
program - Low level I/O can also use redirection and pipe
to change the default setting of the three files.
6Low level I/O read and write
- Read and Write
- getc() and putc() getchar() and putchar()
- int read(int fd, char buf, int n)
- Return -1 for error return 0 for EOF.
- int write(int fd, char buf, int n)
- Error return value does not equal to n
include stdio.h int main () char
buf1024 int n while ((n read(0,
buf, 1024)) gt 0) write ( 1, buf, n)
return 0
include stdio.h int getchar () char c
return (read(0, c, 1) 1 ) ? c EOF
7Open
- includeltfcntl.hgt
- int fd
- fdopen (name, flags, perms)
- flags O_RDONLY O_WRONLY O_RDWR
- perms always 0 for uses of open
- Flags are defined in ltfcntl.hgt
- can not open non-existing files, -1 for errors
8Creat
- create new files or rewrite old ones
- int fd
- fdcreat (name, perms)
- If file exists, creat will discard previous
contents - -1 for error
- use 9 bit file attributes as perms when creating
files. - This function also opens the new file
9Close , Unlink
- int close(int fd)
- free file descriptor for reuse.
- correspond to fclose in standard library.
- int unlink(char path)
- remove a file form the file system.
10Examples
include stdio.h / A copy program/ int main
() char f1 file1, f2file2
char buf1024 int fd1, fd2, n if
((fd1 open(f1, O_RDONLY, 0)) -1)
usage(unable to opening source file s\n, f1)
if ((fd2 creat(f2, 0666)) -1)
usage(unable to creating new file s \n, f2)
while ((n read(fd1, buf, 1024) )gt 0)
write ( fd2, buf, n) return 0
include ltstdio.hgt includeltstdarg.hgt void
usage(char fmt, ) va_list args
va_start(args, fmt) fprintf(stderr, error
) vfprintf(stderr, fmt, args)
va_end(args) fprint(usage srcfile
destfile\n)
11Random Access-Lseek
- lseek
- long lseek (int fd, long offset, int origin)
- Origin 0, 1, 2, determines where to start
offset. - 0 beginning 1 current location 2 the end.
- compare with
- int fseek(FILE fp, long offset, int origin)
12Example
include stdio.h / append file1 to file2/ int
main () char f1 file1, f2file2
int fd1, fd2, n char buf1024 if ((fd1
open(f1, O_RDONLY, 0)) -1) usage(
unable to opening source file s\n, f1) if
((fd2 creat(f2, 0666) ) -1)
usage(unable to creating new file s \n, f2)
else lseek(fd2, 0L, 2) while ((n
read(fd1, buf, 1024) ) gt 0) write ( fd2,
buf, n) return 0
13Lab4 Assignment
- From lina_at_cse.ohio-state.edu Saturday Nov 18
183355 2005 - Return-Path li.655_at_osu.edu
- How are you? ltNote email bodygt
- From lina_at_cse.ohio-state.edu Sunday Nov 19
183255 2005 - Return-Path ltli.655_at_osu.edugtI am fine. ltNote
email bodygt
14Lab4 Assignment
- /home/2/lina lab4 mailbox.lina
- 1From lina_at_cse.ohio-state.edu Saturday Nov 18
183355 2005 - 2 From lina_at_cse.ohio-state.edu Sunday Nov 19
183255 2005 - Please choose which email you want to read
- 1
- How are you?
- Please choose which email you want to read
- 2
- I am fine.
- Please choose which email you want to read
- 0
- Thanks for using my simple mail reader. Bye.
15Lab4 Assignment
- When started, read from a mailbox and print out
the ordered list of 'From ' lines from all the
emails.(30 points) - Ask the user about which email he or she wants to
read. - When having received a choice, print out the BODY
- of that email.(35 points)
- Repeat a) -- c) unless the user says 0, at which
point - your utility command exits.(40 points)
16Lab4 Assignment
- Read mailbox (a file)
- FILE fopen (char name, char mode)
- long ftell(FILE stream)
- int fseek(FILE fp, long offset, int origin)
- char fgets(char line, int maxline, FILE fp)
- int fclose(FILE fp)