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 ProgrammingUNIX 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 HandlingMost 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 Handlingglobal variable errno holds the numeric code of the last system-call errorfunction perror() describes the system-call errorvoid 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 ManagementSystem Calls: open, fcntl, read, write, lseek, unlink, closeTypical sequence:openread/writecloseTypical File I/O Sequenceint 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 DescriptorsEach descriptor has its own private set of propertiesfile pointer (stores offset within file; changes on read/write/lseek)flag indicating if the file descriptor should be closed or not when process execsflag indicating if output to file should be appended to end of file or notothersFile DescriptorsCan 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 namemode: 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 Flagsr/w flags: O_RDONLY, O_WRONLY, O_RDWRmisc. flags: O_APPEND : position file pointer at the end of file before each writeO_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() failsO_NONBLOCK: for named pipesO_TRUNC: if file exists it is truncated to length 0Open Examplestmpfd = 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 bufthe 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 filereturns -1 if unsuccessfulExample: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 accordinglyIf 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 valuereturns -1 if unsuccessfulExample: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_tchanges file pointermode determines how offset is to be used(next slide)Moving the file pointer:lseek() modesmode = SEEK_SEToffset relative to start of filemode = SEEK_CURoffset relative to current position of filemode = SEEK_ENDoffset relative to end of fileMoving the file pointer:lseek()lseek fails if moved before start of filereturns new file position if successfulreturns -1 if unsuccessfulExample:lseek(fd, lineStart[i], SEEK_SET);charsRead = read(fd,buffer, lineStart[i+1] - lineStart[i]);Current Offset of lseekTo find out current position use:currentOffset =
View Full Document