Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35Slide 36Slide 37Slide 38Slide 39Slide 40Slide 41Slide 42Slide 43Slide 44Slide 45Slide 46Slide 47Slide 48Slide 49Slide 50Slide 51Slide 52Slide 53Slide 54Slide 55Slide 56Slide 57Slide 58Slide 59Slide 60Slide 61Slide 62Slide 63Slide 64Slide 65Slide 66Slide 67Slide 68Slide 69Slide 70Slide 71Slide 72Slide 73Slide 74Slide 75Slide 76Slide 77Slide 78Slide 79Slide 80Slide 81Slide 82Slide 83Slide 84Slide 85Slide 86Slide 87Slide 88Slide 89Slide 90Slide 91Slide 92Slide 93Slide 94Slide 95Slide 96Slide 97Slide 98Slide 99Slide 100Slide 101Slide 102Slide 103Slide 104Slide 105Slide 106Slide 107Slide 108Slide 109Slide 110Slide 111Slide 112Slide 113Slide 114Slide 115Slide 116Slide 117Slide 118Slide 119Slide 120Slide 121Slide 122Slide 123Slide 124Slide 125Chapter 13Systems ProgrammingGraham Glass and King Ables, UNIX for Programmers and Users,Third 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: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!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) /* 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 errorExample: 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 */ { printf ("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); /* will display previous error num (21) */ perror ("main"); errno = 0; /* Manually reset error variable */ perror ("main");}File ManagementSystem Calls: open, fcntl, read, write, lseek, unlink, closeTypical sequence:openread/writecloseTypical 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 */read(fd,...) /* read from file */write(fd,...) /* write to file */lseek(fd,...) /* seek within file */close(fd); /* close file */File Descriptors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 when process execsflag indicating if output to file should be appended to end of file or notothersFile DescriptorsCan open file several times (with different descriptors)System calls open() and fcntl() both allow you to manipulate these flags.Opening a File: open()int open(char *fileName, int mode [, int permissions])allows you to open an existing file or create a new file for r/w fileName: absolute or relative path namemode: bitwise or-ing of a r/w flag together with zero or more miscellaneous flags (next slide)permissions: supplied only when file is created (ex. 0600 octal)Mode Flagsr/w flags: O_RDONLY, O_WRONLY, O_RDWRmisc. flags: O_APPEND : position file pointer at the end of file before each writeO_CREAT : if the file does not exist, create it and set the owner ID = process' eff. uid (uses umask value to set permissions) O_EXCL : if O_CREAT is set and file exists then open() failsO_NONBLOCK: for named pipesO_TRUNC: if file exists it is truncated to length 0Open Examplestmpfd = open(tmpName, O_CREAT | O_RDWR, 0600); fd = open (fileName, O_RDONLY);Read from a regular file:read()ssize_t read(int fd, void *buf, size_t count)typedef int ssize_t;typedef unsigned int size_t;copies upto count bytes from the file referenced by fd into buffer bufthe bytes are read from current position (file pointer) which is then updated accordinglyRead from a regular file:read()it returns number of bytes copied returns 0 if it attempts to copy after end of filereturns -1 if unsuccessfulExample:charsRead = read(fd, buffer, BUFFER_SIZE);if (charsRead == 0) break;if (charsRead == -1) fatalError();Write to a regular file:write()ssize_t write(int fd, void *buf, size_t count)Copies upto count bytes from a buffer buf into the file referenced by fd The bytes are written into current position (file pointer) which is then updated accordinglyIf O_APPEND was set, file pointer is set to end of file before each writeWrite to a regular file:write()It returns number of bytes copied You should check this return valuereturns -1 if unsuccessfulExample:if (standardInput) { charsWritten = write(tmpfd,buffer,BUFFER_SIZE); if (charsWritten != charsRead) fatalError(); }Moving the file pointer:lseek()off_t lseek (int fd, off_t offset, int mode)typedef long off_tchanges file pointermode determines how offset is to be used(next slide)Moving the file pointer:lseek() modesmode = SEEK_SEToffset relative to start of filemode = SEEK_CURoffset relative to current position of filemode = SEEK_ENDoffset relative to end of fileMoving the file pointer:lseek()lseek fails if moved before start of filereturns new file position if successfulreturns -1 if unsuccessfulExample:lseek(fd, lineStart[i], SEEK_SET);charsRead = read(fd,buffer, lineStart[i+1] - lineStart[i]);Current Offset of lseekTo find out current position use:currentOffset =


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?