DOC PREVIEW
UI CS 270 - System Calls

This preview shows page 1-2-14-15-29-30 out of 30 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

System callsWe will investigate several issues related to system calls. Read chapter 12 of the bookLinux system call categories file managementprocess managementerror handlingnote that these categories are loosely defined and much is behind included, e.g. communication. Why? 1System callsFile management system call hierarchy you may not see some topics as part of “file management”, e.g., sockets2System callsProcess management system call hierarchy 3System callsError handling hierarchy 4Error HandlingAnything can fail! System calls are no exceptionTry to read a file that does not exist!Error number: errnoevery process contains a global variable errnoerrno is set to 0 when process is createdwhen error occurs errno is set to a specific code associated with the error causetrying to open file that does not exist sets errno to 25Error Handlingerror constants are defined in errno.hhere are the first few of errno.h on OS X 10.6.4#define EPERM 1 /* Operation not permitted */#define ENOENT 2 /* No such file or directory */#define ESRCH 3 /* No such process */#define EINTR 4 /* Interrupted system call */#define EIO 5 /* Input/output error */#define ENXIO 6 /* Device not configured */#define E2BIG 7 /* Argument list too long */#define ENOEXEC 8 /* Exec format error */#define EBADF 9 /* Bad file descriptor */#define ECHILD 10 /* No child processes */#define EDEADLK 11 /* Resource deadlock avoided */6Error Handlingcommon mistake for displaying errnofrom Linux errno man page:7Error HandlingDescription of the perror () system call.Library Function: void perror (char* str)perror () displays the string str, followed by a colon, followed by a description of the last system call error. If there is no error to report, it displays the string "Error 0." Actually, perror () isn't a system call, it is a standard C library function.8example from text $ cat showErrno.c#include <stdio.h>#include <fcntl.h>#include <errno.h>main (){ int fd; /* Open a nonexistent 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"); } fd = open ("/", O_WRONLY); /* Force a different error */ if (fd == -1) { printf ("errno = %d\n", errno); perror ("main"); } /* Execute a successful system call */ fd = open ("nonexist.txt", O_RDONLY | O_CREAT, 0644); printf ("errno = %d\n", errno); /* Display after successful call */ perror ("main"); errno = 0; /* Manually reset error variable */ perror ("main");}9output from example above $ ./showErrno ...run the program.errno = 2main: No such file or directoryerrno = 21main: Is a directoryerrno = 29 ...even after a successful callmain: Illegal seekmain: Success ...after we reset manually.$ _10File ManagementWhat is a file?In unix file types go beyond just your regular files on diskThe file types (and symbols) are:Regular files ( )Directories (d)Links (l)Special files (c)Sockets (s)Named pipes (p)11File ManagementExamplesfile is opened, a file descriptor is returned, after certain operations the file is closedint fd; /* File descriptor */...fd = open (fileName, ...); /* Open file, return file descriptor */if (fd == -1) { /* deal with error condition */ }...fcntl (fd, ...); /* Set some I/O flags if necessary */...read (fd, ...); /* Read from file */...write (fd, ...); /* Write to file */...lseek (fd, ...); /* Seek within file*/...close (fd); /* Close the file, freeing file descriptor */ close the file, even though you know....12File ManagementFile descriptorssequential numbers, starting with 0first three descriptors are0 = stdin1 = stdout2 = stderrwhen reference to file is closed, the fd is freed to be reassigned13File ManagementA file may have multiple file discriptors 14File ManagementFile descriptor properties include:file pointer indicating the offset in the file where it is reading/writingflag that indicates whether the descriptor should be automatically closed if the process calls exec() flag that indicates whether output should be appended to the end of file15File ManagementFile descriptor properties for special files include:flag that indicates whether a process should block on input from a file if it does not currently contain any inputA number that indicates a process ID or process group that should be sent a SIGIO signal if input becomes available a SIGIO signal indicates that I/O is now possible16File ManagementLinux basic I/O operationsopen Opens/creates a file.read Reads bytes from a file into a buffer.write Writes bytes from a buffer to a file.lseek Moves to a particular offset in a file.close Closes a file.unlink Removes a file.17Example: ReverseWrite a Utility: reverse -c [ fileName ]reverse reverses the lines of its input and displays them to standard output. If no file name is specified, reverse reverses its standard input. When the -c option is used, reverse also reverses the characters in each line.18Example: ReverseExamples of its application$ gcc reverse.c -o reverse ...compile the program.$ cat test ...list the test file.Christmas is coming,The days that grow shorter,Remind me of seasons I knew in the past.$ ./reverse test ...reverse the file.Remind me of seasons I knew in the past.The days that grow shorter,Christmas is coming,$ ./reverse -c test ...reverse the lines too..tsap eht ni wenk I snosaes fo em dnimeR,retrohs worg taht syad ehT,gnimoc si samtsirhC$ cat test | ./reverse ...pipe output to "reverse".Remind me of seasons I knew in the past.The days that grow shorter,Christmas is coming,$ _19Example: ReverseHow reverse worksit makes two passes over its input. During the first pass, it notes the starting offset of each line in the file and stores this information in an array. During the second pass, it jumps to the start of each line in reverse order, copying it from the original input file to its standard output.If no file name is specified on the command line, reverse reads from its standard input during the first pass and copies it into a temporary file for the second pass. When the program is finished, the temporary file is removed.202122 1 #include <fcntl.h> /* For file mode definitions */ 2 #include <stdio.h> 3 #include <stdlib.h> 4 5 6 /* Enumerator */ 7


View Full Document

UI CS 270 - System Calls

Download System Calls
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 System Calls 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 System Calls 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?