1 System Calls, Stdio, and Course Wrap-Up COS 217 Professor Jennifer Rexford.

Slides:



Advertisements
Similar presentations
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
Advertisements

Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
1 Processes Professor Jennifer Rexford
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
1 System Calls & Stdio. 2 Two processes open the same file Both keep writing to it What happens?
Files. System Calls for File System Accessing files –Open, read, write, lseek, close Creating files –Create, mknod.
Home: Phones OFF Please Unix Kernel Parminder Singh Kang Home:
1 System Calls and Standard I/O Professor Jennifer Rexford
Chapter 8: I/O Streams and Data Files. In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based.
Guide To UNIX Using Linux Third Edition
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 2: Operating-System Structures Modified from the text book.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
Operating Systems Concepts 1. A Computer Model An operating system has to deal with the fact that a computer is made up of a CPU, random access memory.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
I/O Tanenbaum, ch. 5 p. 329 – 427 Silberschatz, ch. 13 p
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
Chapter 3.1:Operating Systems Concepts 1. A Computer Model An operating system has to deal with the fact that a computer is made up of a CPU, random access.
System Calls 1.
Adv. UNIX: lowIO/161 Advanced UNIX v Objectives –look at low-level operations for handling files Special Topics in Comp. Eng. 2 Semester.
RjpSystem Level Programming Operating Systems 1 Having fun withy the Unix Operating System Praxis Week 7 Rob Pooley.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
CSC 322 Operating Systems Concepts Lecture - 4: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
Segmentation & O/S Input/Output Chapter 4 & 5 Tuesday, April 3, 2007.
CSC 322 Operating Systems Concepts Lecture - 25: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
Recitation 9: Error Handling, I/O, Man Andrew Faulring Section A 4 November 2002.
CS162B: Pipes Jacob T. Chan. Pipes  These allow output of one process to be the input of another process  One of the oldest and most basic forms of.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
UNIX Files File organization and a few primitives.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Files & File system. A Possible File System Layout Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved
1 IT 252 Computer Organization and Architecture Interaction Between Systems: File Sharing R. Helps.
Chapter 7 : File Processing1 File-Oriented Input & Output CHAPTER 7.
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
CSCI 330 UNIX and Network Programming Unit VII: I/O Management I.
File Systems cs550 Operating Systems David Monismith.
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
Recitation 9: Error Handling, I/O, Man Anubhav Gupta Section D.
1 Process Description and Control Chapter 3. 2 Process A program in execution An instance of a program running on a computer The entity that can be assigned.
Chapter 1 Introduction  What is an operating system  History of operating systems  The operating system zoo  Computer hardware review  Operating system.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Tarek Abdelzaher Vikram Adve CS241 Systems Programming System Calls and I/O.
File I/O open close lseek read and write – unbuffered I/O dup and dup2.
The Process CIS 370, Fall 2009 CIS UMassD. The notion of a process In UNIX a process is an instance of a program in execution A job or a task Each process.
I/O Software CS 537 – Introduction to Operating Systems.
OS interface: file and I/O system calls File operations in C/C++? –fopen(), fread(), fwrite(), fclose(), fseek() in C f.open(…), f.close(…) in C++ I/O.
Files A collection of related data treated as a unit. Two types Text
1 Device Controller I/O units typically consist of A mechanical component: the device itself An electronic component: the device controller or adapter.
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
ECE 456 Computer Architecture Lecture #9 – Input/Output Instructor: Dr. Honggang Wang Fall 2013.
Using System Calls (Unix) Have to tell compiler (if C/C++) where to find the headers, etc. – i.e., the “include” files May have to tell compiler where.
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
Files in UNIX u UNIX deals with two different classes of files:  Special Files  Regular Files u Regular files are just ordinary data files on disk -
Error handling I/O Man pages
Introduction to Operating Systems
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Chapter 7 Text Input/Output Objectives
CS703 - Advanced Operating Systems
Files I/O, Streams, I/O Redirection, Reading with fscanf
Introduction to Operating Systems
Text and Binary File Processing
File Input and Output.
I/O and Process Management
Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Advanced UNIX progamming
Standard I/O Library Implementation
File I/O & UNIX System Interface
Presentation transcript:

1 System Calls, Stdio, and Course Wrap-Up COS 217 Professor Jennifer Rexford

2 Goals of Today’s Class System calls  How a user process contacts the Operating System  For advanced services that may require special privilege Standard I/O library  Generic I/O support for C programs  A smart wrapper around I/O-related system calls  Stream concept, line-by-line input, formatted output,... Course wrap-up  Timeline for reading and exam period  Main themes of the course

3 System Calls

4 Communicating With the OS Processor modes  User mode: can execute normal instructions and access only user memory  Supervisor mode: can also execute privileged instructions & access all memory (e.g., devices) User Process Operating System signalssystems calls

5 Main Categories of System Calls File system  Low-level file I/O  E.g., creat, open, read, write, lseek, close Multi-tasking mechanisms  Process control  E.g., fork, wait, exec, exit, signal, kill Inter-process communication  E.g., pipe, dup, dup2 Unix has a few hundred system calls  See “man 2 intro” or /usr/include/syscall.h

6 System Calls Method for user process to invoke OS services Called just like a function  Essentially a “protected” function call  That transfers control to the OS and back File System Stdio Library Appl Prog creat, open, close, read, write, lseek fopen, fclose, printf, fgetc, getchar,… user OS

7 Implementing a System Call System calls are often implemented using traps  OS gains control through trap  Switches to supervisor mode  Performs the service  Switches back to user mode  Gives control back to user movl $1, %eax int $0x80 Which call? 1: exit 2: fork 3: read 4: write 5: open 6: close … Trap to the OS System-call specific arguments are put in registers

8 Main UNIX System Calls for Files Creat: int creat(char *pathname, mode_t mode);  Create a new file and assign a file descriptor Open: int open(char *pathname, int flags, mode_t mode);  Open a the file pathname and return a file descriptor Close: int close(int fd);  Close a file descriptor fd Read: int read(int fd, void *buf, int count);  Read up to count bytes from fd, into the buffer at buf Write: int write(int fd, void *buf, int count);  Writes up to count bytes into fd, from the buffer at buf Lseek: int lseek(int fd, int offset, int whence);  Assigns the file pointer to a new value by applying an offset

9 Example: UNIX open() System Call Converts a path name into a file descriptor  int open(const char *pathname, int flags, mode_t mode); Arguments  Pathname: name of the file  Flags: bit flags for O_RDONLY, O_WRONLY, O_RDWR  Mode: permissions to set if file must be created Returns  Integer file descriptor (or a -1 if an error) Performs a variety of checks  E.g., whether the process is entitled to access the file

10 Example: UNIX read() System Call Converts a path name into a file descriptor  int read(int fd, void *buf, int count); Arguments  File descriptor: integer descriptor returned by open()  Buffer: pointer to memory to store the bytes it reads  Count: maximum number of bytes to read Returns  Number of bytes read –Value of 0 if nothing more to read –Value of -1 if an error Performs a variety of checks  Whether file has been opened, whether reading is okay

11 Standard I/O Library

12 Standard I/O Library Portability  Generic I/O support for C programs  Specific implementations for various host OSes  Invokes the OS-specific system calls for I/O Abstractions for C programs  Streams  Line-by-line input  Formatted output Additional optimizations  Buffered I/O  Safe writing File System Stdio Library Appl Prog user OS

13 Layers of Abstraction Disk Driver Storage File System disk blocks variable-length segments hierarchical file system Operating System Stdio Library FILE * stream Appl Prog User process int fd

14 Stream Abstraction Any source of input or destination for output  E.g., keyboard as input, and screen as output  E.g., files on disk or CD, network ports, printer port, … Accessed in C programs through file pointers  E.g., FILE *fp1, *fp2;  E.g., fp1 = fopen(“myfile.txt”, “r”); Three streams provided by stdio.h  Streams stdin, stdout, and stderr –Typically map to keyboard, screen, and screen  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 program

15 Sequential Access to a Stream Each stream has an associated file position  Starting at beginning of file (if opened to read or write)  Or, starting at end of file (if opened to append) Read/write operations advance the file position  Allows sequencing through the file in sequential manner Support for random access to the stream  Functions to learn current position and seek to new one file

16 Example: Opening a File FILE *fopen(“myfile.txt”, “r”)  Open the named file and return a stream  Includes a mode, such as “r” for read or “w” for write Creates a FILE data structure for the file  File descriptor, mode, status, buffer, …  Assigns fields and returns a pointer Opens or creates the file, based on the mode  Write (‘w’): create file with default permissions  Read (‘r’): open the file as read-only  Append (‘a’): open or create file, and seek to the end

17 Example: Formatted I/O int fprintf(fp1, “Number: %d\n”, i)  Convert and write output to stream in specified format int fscanf(fp1, “FooBar: %d”, &i)  Read from stream in format and assign converted values Specialized versions  printf(…) is just fprintf(stdout, …)  scanf(…) is just fscanf(stdin, …)

18 Example: A Simple getchar() int getchar(void) { static char c; if (read(0, &c, 1) == 1) return c; else return EOF; } Read one character from stdin  File descriptor 0 is stdin  &c points to the buffer  1 is the number of bytes to read Read returns the number of bytes read  In this case, 1 byte means success

19 Making getchar() More Efficient Poor performance reading one byte at a time  Read system call is accessing the device (e.g., a disk)  Reading one byte from disk is very time consuming  Better to read and write in larger chunks Buffered I/O  Read a large chunk from disk into a buffer –Dole out bytes to the user process as needed –Discard buffer contents when the stream is closed  Similarly, for writing, write individual bytes to a buffer –And write to disk when full, or when stream is closed –Known as “flushing” the buffer

20 Better getchar() with Buffered I/O int getchar(void) { static char base[1024]; static char *ptr; static int cnt = 0; if (cnt--) return *ptr++; cnt = read(0, base, sizeof(base)); if (cnt <= 0) return EOF; ptr = base; return getchar(); } persistent variables base ptr But, many functions may read (or write) the stream…

21 Details of FILE in stdio.h (K&R 8.5) #define OPEN_MAX 20 /* max files open at once */ typedef struct _iobuf { int cnt; /* num chars left in buffer */ char *ptr; /* ptr to next char in buffer */ char *base; /* beginning of buffer */ int flag; /* open mode flags, etc. */ char fd; /* file descriptor */ } FILE; extern FILE _iob[OPEN_MAX]; #define stdin (&_iob[0]) #define stdout (&_iob[1]) #define stderr (&_iob[2])

22 A Funny Thing About Buffered I/O int main() { printf(“Step 1\n”); sleep(10); printf(“Step2\n”); } Run “a.out > out.txt &” and then “more out.txt”  To run a.out in the background, outputting to out.txt  And then to see the contents on out.txt Neither line appears till ten seconds have elapsed  Because the output is being buffered  Add fflush(stdout) to flush the output buffer  fclose() also flushes the buffer before closing

23 Challenges of Writing Write system call  int write(int fd, void *buf, int count);  Writes up to count bytes into fd, from the buffer at buf Problem: might not write everything  Can return a number less than count  E.g., if the file system ran out of space Solution: safe_write  Try again to write the remaining bytes  Produce an error if it impossible to write more

24 Safe-Write Code int safe_write(int fd, char *buf, int nbytes) { int n; char *p = buf; char *q = buf + nbytes; while (p < q) { if ((n = write(fd, p, (q-p)*sizeof(char))) > 0) p += n/sizeof(char); else perror(“safe_write:”); } return nbytes; } pqp

25 Summary of System Calls and Stdio Standard I/O library provides simple abstractions  Stream as a source or destination of data  Functions for manipulating files and strings Standard I/O library builds on the OS services  Calls OS-specific system calls for low-level I/O  Adds features such as buffered I/O and safe writing Powerful examples of abstraction  User programs can interact with streams at a high level  Standard I/O library deals with some more gory details  Only the OS deals with the device-specific details

26 Course Wrap Up

27 The Rest of the Semester Final assignment  Due 9pm Tuesday May 13 (Dean’s Date)  Late assignments must be submitted before midnight Office hours  Bob Dondero and Tom Jablin: times TBA  Jen Rexford: by appointment Review sessions  Around May 21-22, exact times TBA Final exam  Saturday May 24 9am-noon  Open notes and open book, but no computers  No need to print and bring the IA32 manuals

28 Goals of COS 217 Understand boundary between code and computer  Machine architecture  Operating systems  Compilers Learn C and the Unix development tools  C is widely used for programming low-level systems  Unix has a rich development environment  Unix is open and well-specified, good for study & research Improve your programming skills  More experience in programming  Challenging and interesting programming assignments  Emphasis on modularity and debugging

29 Relationship to Other Courses Machine architecture  Logic design (306) and computer architecture (471)  COS 217: assembly language and basic architecture Operating systems  Operating systems (318)  COS 217: virtual memory, system calls, and signals Compilers  Compiling techniques (320)  COS 217: compilation process, symbol tables, assembly and machine language Software systems  Numerous courses, independent work, etc.  COS 217: programming skills, UNIX tools, and ADTs

30 Lessons About Computer Science Modularity  Well-defined interfaces between components  Allows changing the implementation of one component without changing another  The key to managing complexity in large systems Resource sharing  Time sharing of the CPU by multiple processes  Sharing of the physical memory by multiple processes Indirection  Representing address space with virtual memory  Manipulating data via pointers (or addresses)

31 Lessons Continued Hierarchy  Memory: registers, cache, main memory, disk, tape, …  Balancing the trade-off between fast/small and slow/big Bits can mean anything  Code, addresses, characters, pixels, money, grades, …  Arithmetic can be done through logic operations  The meaning of the bits depends entirely on how they are accessed, used, and manipulated

32 Have a Great Summer!!!