DOC PREVIEW
Princeton COS 217 - System Calls and Standard I/O

This preview shows page 1-2-3-4 out of 13 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 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 13 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 13 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 13 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

11System Calls and Standard I/OProfessor Jennifer Rexfordhttp://www.cs.princeton.edu/~jrex2Goals of Today’s Class• System callso How a user process contacts the Operating Systemo For advanced services that may require special privilege• Standard I/O libraryo Generic I/O support for C programso A smart wrapper around I/O-related system callso Stream concept, line-by-line input, formatted output, ...23System Calls4Communicating With the OS• System callo Request to the operating system to perform a tasko … that the process does not have permission to perform• Signalo Asynchronous notification sent to a processo … to notify the process of an event that has occurred User ProcessOperating Systemsignals systems calls35Processor Modes• The OS must restrict what a user process can doo What instructions can executeo What portions of the address space are accessible• Supervisor mode (or kernel mode)o Can execute any instructions in the instruction set– Including halting the processor, changing mode bit, initiating I/Oo Can access any memory location in the system– Including code and data in the OS address space• User modeo Restricted capabilities– Cannot execute privileged instructions– Cannot directly reference code or data in OS address spaceo Any such attempt results in a fatal “protection fault”– Instead, access OS code and data indirectly via system calls6Main Categories of System Calls• File systemo Low-level file I/Oo E.g., creat, open, read, write, lseek, close• Multi-tasking mechanismso Process controlo E.g., fork, wait, exec, exit, signal, kill• Inter-process communicationo E.g., pipe, dup, dup2• Unix has a few hundred system callso See “man 2 intro” or /usr/include/syscall.h47System Calls• Method for user process to invoke OS services• Called just like a functiono Essentially a “protected” function callo That transfers control to the OS and backFile Systemcreat, open, close,read, write, lseekApplicationOSUserProcess8Implementing a System Call • System calls are often implemented using trapso OS gains control through trapo Switches to supervisor modeo Performs the serviceo Switches back to user modeo Gives control back to usermovl $1, %eaxint $0x80Which call?1: exit2: fork3: read4: write5: open6: close…Trap to the OSSystem-call specific arguments are put in registers59Main UNIX System Calls for Files• Creat: int creat(char *pathname, mode_t mode);o Create a new file and assign a file descriptor• Open: int open(char *pathname, int flags, mode_t mode);o Open the file pathname and return a file descriptor• Close: int close(int fd);o Close a file descriptor fd• Read: int read(int fd, void *buf, int count);o Read up to count bytes from fd, into the buffer at buf•Write:int write(int fd, void *buf, int count);o Writes up to count bytes into fd, from the buffer at buf• Lseek: int lseek(int fd, int offset, int whence);o Assigns the file pointer to a new value by applying an offset10Example: UNIX open() System Call• Converts a path name into a file descriptoro int open(const char *pathname, int flags, mode_t mode);• Argumentso Pathname: name of the fileo Flags: bit flags for O_RDONLY, O_WRONLY, O_RDWRo Mode: permissions to set if file must be created• Returnso Integer file descriptor (or a -1 if an error)• Performs a variety of checkso E.g., whether the process is entitled to access the file611Example: UNIX read() System Call• Converts a path name into a file descriptoro int read(int fd, void *buf, int count);• Argumentso File descriptor: integer descriptor returned by open()o Buffer: pointer to memory to store the bytes it readso Count: maximum number of bytes to read• Returnso Number of bytes read– Value of 0 if nothing more to read– Value of -1 if an error• Performs a variety of checkso Whether file has been opened, whether reading is okay12Standard I/O Library713Standard I/O Library• Portabilityo Generic I/O support for C programso Specific implementations for various host OSeso Invokes the OS-specific system calls for I/O• Abstractions for C programso Streamso Line-by-line inputo Formatted output • Additional optimizationso Buffered I/Oo Safe writingFile SystemStdio LibraryAppl ProguserOS14Layers of AbstractionDiskDriverStorageFile Systemdisk blocksvariable-length segmentshierarchical file systemOperatingSystemStdio LibraryFILE *streamAppl ProgUserprocessint fd815Stream Abstraction• Any source of input or destination for outputo E.g., keyboard as input, and screen as outputo E.g., files on disk or CD, network ports, printer port, …• Accessed in C programs through file pointerso E.g., FILE *fp1, *fp2;o E.g., fp1 = fopen(“myfile.txt”, “r”);• Three streams provided by stdio.ho Streams stdin, stdout, and stderr– Typically map to keyboard, screen, and screeno Can redirect to correspond to other streams– E.g., stdin can be the output of another program– E.g., stdout can be the input to another program16Sequential Access to a Stream• Each stream has an associated file positiono Starting at beginning of file (if opened to read or write)o Or, starting at end of file (if opened to append)• Read/write operations advance the file positiono Allows sequencing through the file in sequential manner• Support for random access to the streamo Functions to learn current position and seek to new onefile file917Example: Opening a File •FILE *fopen(“myfile.txt”, “r”)o Open the named file and return a streamo Includes a mode, such as “r” for read or “w” for write• Creates a FILE data structure for the fileo File descriptor, mode, status, buffer, …o Assigns fields and returns a pointer• Opens or creates the file, based on the modeo Write (‘w’): create file with default permissionso Read (‘r’): open the file as read-onlyo Append (‘a’): open or create file, and seek to the end18Example: Formatted I/O•int fprintf(fp1, “Number: %d\n”, i)o Convert and write output to stream in specified format•int fscanf(fp1, “FooBar: %d”, &i)o Read from stream in format and assign converted values• Specialized versionso printf(…) is just fprintf(stdout, …)o scanf(…) is just fscanf(stdin, …)1019Example: A Simple getchar()int getchar(void) {static char c;if (read(0, &c, 1) == 1)return c;else return EOF;}• Read one character from stdino File descriptor 0 is stdino &c points to the buffero 1 is the number of bytes to read• Read returns the number of


View Full Document

Princeton COS 217 - System Calls and Standard I/O

Documents in this Course
Summary

Summary

4 pages

Lecture

Lecture

4 pages

Generics

Generics

14 pages

Generics

Generics

16 pages

Lecture

Lecture

20 pages

Debugging

Debugging

35 pages

Types

Types

7 pages

Lecture

Lecture

21 pages

Assembler

Assembler

16 pages

Lecture

Lecture

20 pages

Lecture

Lecture

39 pages

Testing

Testing

44 pages

Pipeline

Pipeline

19 pages

Lecture

Lecture

6 pages

Signals

Signals

67 pages

Building

Building

17 pages

Lecture

Lecture

7 pages

Modules

Modules

12 pages

Generics

Generics

16 pages

Testing

Testing

22 pages

Signals

Signals

34 pages

Lecture

Lecture

19 pages

Load more
Download System Calls and Standard I/O
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 Standard I/O 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 and Standard I/O 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?