Unformatted text preview:

1Chapter 13Systems ProgrammingGraham Glass and King Ables, UNIX for Programmers and Users,Third Edition Pearson Prentice Hall 20032008-12-1 cs3320 1Third Edition, Pearson Prentice Hall, 2003.Original Notes by Raj SunderramanConverted to presentation and updated by Michael WeeksSystems Programming• UNIX System calls: C functions that provide access to the file system, processes, and error handling.•System Calls grouped into 3 main categories:2008-12-1 cs3320 2ygp g– File Management (Fig. 13.1) – Process Management (Fig. 13.2)– Error Handling (Fig. 13.3)Error Handling• Most system calls are capable of failing in some way.• Example: open a file may fail because file does not exist!2008-12-1 cs3320 3• System call returns a value of -1 when it fails.• This value does not tell much about the cause of the failure.Error Handling• global variable errno – holds the numeric code of the last system-call error• function perror() describes the system-call error•void perror(char*str)2008-12-1 cs3320 4void perror(char str) – /* standard C function in stdio.h */– Displays str followed by : followed by a description of the last system call error. – Error 0 is displayed if no error– See example showErrno.cExample: File I/O#include <stdio.h>#include <fcntl.h>#include <errno.h>main () {int fd;/* Open a non-existent file to cause an error */fd = open ("nonexist.txt", O_RDONLY);if (fd == -1) /* fd == -1 =, an error occurred */ {2008-12-1 cs3320 5printf ("errno = %d\n", errno);perror ("main");}/* Force a different error */fd = open ("/", O_WRONLY); if (fd == -1) {printf ("errno = %d\n", errno);perror ("main");}Example: File I/O/* Execute a successful system call */fd = open ("nonexist.txt", O_RDONLY | O_CREAT, 0644);/* Display after successful call */printf ("errno = %d\n", errno); /* ill di l i (21) */2008-12-1 cs3320 6/* will display previous error num (21) */perror ("main");errno = 0; /* Manually reset error variable */perror ("main");}$ showErrorerrno = 21main: Is a directoryerrno = 29main: Illegal seekmain: Success2File Management• System Calls: open, fcntl, read, write, lseek, unlink, close• Typical sequence:–open2008-12-1 cs3320 7p– Each read() and write() invokes a system call!– Close– All data are processed as a series of bytes (no format)2008-12-1 cs3320 82008-12-1 cs3320 9 2008-12-1 cs3320 102008-12-1 cs3320 11UNIX File2008-12-1 cs3320 123File I/O• File– A sequence of bytes•Directory– A file that includes info on how to find other files.2008-12-1 cs3320 13vmunix/devconsole lp0…bincsh…liblibc.a…usrinclude …etcpasswd…File I/O• Path name– Absolute path name• Start at the root / of the file system• /user/john/fileA– Relative path name• Start at the “current directory” which is an attribute of the process accessing the path name./dirA/fileB2008-12-1 cs3320 14•./dirA/fileB•Links– Symbolic Link – 4.3BSD• A file containing the path name of another file can across file-system boundaries.– Hard Link•. or ..File I/O• File Descriptor– Non-negative integer returned by open() or creat(): 0 .. OPEN_MAX• Virtually un-bounded for SVR4 & 4.3+BSD• Each descriptor has its own private set of properties– file pointer (stores offset within file; changes on read/write/lseek)–flag indicating if the file descriptor should be closed or not when2008-12-1 cs3320 15flag indicating if the file descriptor should be closed or not when process execs– flag indicating if output to file should be appended to end of file or not others– Per-process base– POSIX.1 – 0: STDIN_FILENO, 1: STDOUT_FILENO, 2: STDERR_FILENO• <unistd.h>• Convention employed by the Unix shells and applicationsFile I/O - File Manipulation• Operations– open, close, read, write, lseek, dup, fcntl, ioctl, trunc, rename, chmod, chown, mkdir, cd, opendir, readdir, closedir, etc.2008-12-1 cs3320 16Read(4, …)Tables ofOpened Files(per process)SystemOpen FileTabl eIn-corev-node listi-nodei-nodei-nodesyncdata blockdata blockTypical File I/O Sequence• int fd; – /* file descriptor 0: std. in, 1: std. out, 2: std error*/ • fd = open(fileName,...);• if (fd == -1) /* deal with error */• fcntl(fd,...) /* set IO flags if needed */2008-12-1 cs3320 17(,) g• read(fd,...) /* read from file */• write(fd,...) /* write to file */• lseek(fd,...) /* seek within file */• close(fd); /* close file */File I/O – open#include <sys/types>#include <sys/stat.h>#include <fcntl.h>int open(const char*pathname, int mode, int permission);2008-12-1 cs3320 18• File/Path Name• O_RDONLY, O_WRONLY, O_RDWR• O_APPEND, O_TRUNC• O_CREAT, O_EXCL• O_NONBLOCKopen(pathname, O_WRONLY | O_CREAT | O_TRUNC, mode)4#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdio.h>#include <stdlib.h>int main(){int fd1, fd2;What is the output of the following program?2008-12-1 cs3320 19fd1 = open("foo1.txt", O_RDONLY, 0);close(fd1);fd2 = open("foo2.txt", O_RDONLY, 0);printf("fd2 = %d\n", fd2);exit(0);}open always returns lowest unopened descriptoropen always returns lowest unopened descriptorFile I/O – creat() and close()#include <sys/types>#include <sys/stat.h>#include <fcntl.h>int creat(const char*pathname, int mode);• open(pathname, O_WRONLY | O_CREAT | O_TRUNC, mode)• This interface is made obsolete by open•Only for write-access.2008-12-1 cs3320 20y• Q: What if you want to create a file for READ and WRITE?#include <unistd.h>int close(int filedes);• All open files are automatically closed by the kernel when a process terminates• Closing a file descriptor releases any record locks on that filemode Flags for O_CREAT• Defined in <sys/stat.h>– S_IRUSR: owner read permit– S_IWUSR: owner write permit– S_IXUSR: owner execute permit– S_IRGRP: group read permit– S_IWGRP: group write permitSIROTH th d it2008-12-1 cs3320 21–S_IROTH: others read permit– S_IWOTH: owners write permit– S_IXOTH: others execute permit• open(“myfile”, O_CREAT, S_IRUSR | S_IXOTH)File I/O - lseek#include <sys/types>#include <unistd.h>off_t lseek(int filedes, off_t offset, int whence);• Current file offset in bytes• changes file pointer• whence: SEEK_SET, SEEK_CUR, SEEK_END–determines how offset is to be used2008-12-1 cs3320 22–determines how offset is to be used– SEEK_SET : offset relative to start of file– SEEK_CUR : offset relative to current position of file– SEEK_END : offset relative to end of file• off_t: typedef


View Full Document

GSU CSC 3320 - chapter13

Download chapter13
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view chapter13 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view chapter13 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?