What Is Operating System? Operating Systems, System Calls, and Buffered I/O. Academic Computers in 1983 and Operating System

Similar documents
Operating Systems CS 217. Provides each process with a virtual machine. Promises each program the illusion of having whole machine to itself

Goals of this Lecture

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

Process Management! Goals of this Lecture!

CS240: Programming in C

Systems Programming. 08. Standard I/O Library. Alexander Holupirek

UNIX System Programming

Naked C Lecture 6. File Operations and System Calls

File Access. FILE * fopen(const char *name, const char * mode);

File I/O. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CSE 410: Systems Programming

CS240: Programming in C

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

UNIX input and output

CSE 333 SECTION 3. POSIX I/O Functions

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

The course that gives CMU its Zip! I/O Nov 15, 2001

File I/O. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

System-Level I/O. Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O

Files. Eric McCreath

UNIX System Calls. Sys Calls versus Library Func

Operating System Labs. Yuanbin Wu

C Basics And Concepts Input And Output

Section 3: File I/O, JSON, Generics. Meghan Cowan

Contents. Programming Assignment 0 review & NOTICE. File IO & File IO exercise. What will be next project?

Input and Output System Calls

Systems Programming. COSC Software Tools. Systems Programming. High-Level vs. Low-Level. High-Level vs. Low-Level.

Standard File Pointers

Layers in a UNIX System. Create a new process. Processes in UNIX. fildescriptors streams pipe(2) labinstructions

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

Lecture 3. Introduction to Unix Systems Programming: Unix File I/O System Calls

All the scoring jobs will be done by script

All the scoring jobs will be done by script

Unix File and I/O. Outline. Storing Information. File Systems. (USP Chapters 4 and 5) Instructor: Dr. Tongping Liu

CSE 333 SECTION 3. POSIX I/O Functions

Princeton University Computer Science 217: Introduction to Programming Systems. I/O Management

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Princeton University. Computer Science 217: Introduction to Programming Systems. I/O Management

Standard I/O in C, Computer System and programming in C

ENG120. Misc. Topics

Lecture files in /home/hwang/cs375/lecture05 on csserver.

File (1A) Young Won Lim 11/25/16

Lecture 23: System-Level I/O

Processes often need to communicate. CSCB09: Software Tools and Systems Programming. Solution: Pipes. Recall: I/O mechanisms in C

File System User API

Contents. NOTICE & Programming Assignment 0 review. What will be next project? File IO & File IO exercise

Process Creation in UNIX

Preview. Interprocess Communication with Pipe. Pipe from the Parent to the child Pipe from the child to the parent FIFO popen() with r Popen() with w

Princeton University Computer Science 217: Introduction to Programming Systems I/O Management

CMPSC 311- Introduction to Systems Programming Module: Input/Output

Input / Output Functions

Operating Systems. Lecture 06. System Calls (Exec, Open, Read, Write) Inter-process Communication in Unix/Linux (PIPE), Use of PIPE on command line

UNIX I/O. Computer Systems: A Programmer's Perspective, Randal E. Bryant and David R. O'Hallaron Prentice Hall, 3 rd edition, 2016, Chapter 10

Important Dates. October 27 th Homework 2 Due. October 29 th Midterm

File Descriptors and Piping

File I/O. Preprocessor Macros

CMPS 105 Systems Programming. Prof. Darrell Long E2.371

System-Level I/O Nov 14, 2002

UNIT IV-2. The I/O library functions can be classified into two broad categories:

System-Level I/O April 6, 2006

Outline. OS Interface to Devices. System Input/Output. CSCI 4061 Introduction to Operating Systems. System I/O and Files. Instructor: Abhishek Chandra

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL.

OS COMPONENTS OVERVIEW OF UNIX FILE I/O. CS124 Operating Systems Fall , Lecture 2

System- Level I/O. Andrew Case. Slides adapted from Jinyang Li, Randy Bryant and Dave O Hallaron

fopen() fclose() fgetc() fputc() fread() fwrite()

Operating systems. Lecture 7

Fall 2017 :: CSE 306. File Systems Basics. Nima Honarmand

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

everything is a file main.c a.out /dev/sda1 /dev/tty2 /proc/cpuinfo file descriptor int

Standard C Library Functions

Topic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional)

System Software Experiment 1 Lecture 7

File I/O. Arash Rafiey. November 7, 2017

Operating System Labs. Yuanbin Wu

W4118: OS Overview. Junfeng Yang

C File Processing: One-Page Summary

Chapter 12. Files (reference: Deitel s chap 11) chap8

Input/Output. Input/Output and Files. Input/Output. I/O System Objectives

A Typical Hardware System The course that gives CMU its Zip! System-Level I/O Nov 14, 2002

Computer Programming: Skills & Concepts (CP) Files in C

Lecture 9: File Processing. Quazi Rahman

Files and Streams Opening and Closing a File Reading/Writing Text Reading/Writing Raw Data Random Access Files. C File Processing CS 2060

Introduction. Files. 3. UNIX provides a simple and consistent interface to operating system services and to devices. Directories

CMSC 216 Introduction to Computer Systems Lecture 17 Process Control and System-Level I/O

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

Files. Programs and data are stored on disk in structures called files Examples. a.out binary file lab1.c - text file term-paper.

THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY. ISA 563: Fundamentals of Systems Programming

Day14 A. Young W. Lim Thr. Young W. Lim Day14 A Thr 1 / 14

CS 201. Files and I/O. Gerson Robboy Portland State University

Maria Hybinette, UGA. ! One easy way to communicate is to use files. ! File descriptors. 3 Maria Hybinette, UGA. ! Simple example: who sort

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

File IO and command line input CSE 2451

PROGRAMMAZIONE I A.A. 2017/2018

System Programming. Standard Input/Output Library (Cont d)

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

SOFTWARE ARCHITECTURE 2. FILE SYSTEM. Tatsuya Hagino lecture URL.

File Input and Output (I/O)

Transcription:

What Is Operating System? Operating Systems, System Calls, and Buffered I/O emacs gcc Browser DVD Player Operating System CS 217 1 Abstraction of hardware Virtualization Protection and security 2 Academic Computers in 1983 and 2003 1883 2003 Ratio CPU clock 3Mhz 3Ghz 1:1000 $/machine $80k $800 100:1 DRAM 256k 256M 1:1000 Disk 20MB 200GB 1:10,000 Network BW 10Mbits/sec 1GBits/sec 1:100 Computing and Communications Exponential Growth! (Courtesy J. Gray) Performance/Price doubles every 18 months 100x per decade Progress in next 18 months = ALL previous progress New storage = sum of all old storage (ever) New processing = sum of all old processing. Aggregate bandwidth doubles in 8 months Address bits 16-32 32-64 1:2 s/machine 10s 1 (or < 1) > 10:1 $/Performance $80k < $800/1000 100,000+:1 3 15 years ago 4

Application lib 5 6 Application Application Application Resident monitor 7 8

App1 App2 Time-sharing OS... App2 9 10 networking user interfaces 11 12

Phase 4: > 1 Machines per Parallel and distributed systems Parallel machine Clusters Network is the computer Pervasive computers Wearable computers Computers everywhere OS are general and specialized A Typical Operating System Abstraction: Layered services to access hardware We learn how to use the services here COS318 will teach how to implement Virtualization: Each user with his own machine Protection & security: safe from yourself and others Process Process Process Process OS Kernel 13 14 Layers of Abstraction System Calls Kernel provided system services: protected procedure call process Kernel Appl Prog Stdio Library File System Storage FILE * stream int fd hierarchical file system variable-length segments user kernel Appl Prog Stdio Library File System fopen,fclose, printf, fgetc, getchar, open, close, read, write, seek Driver Disk disk blocks Unix has ~150 system calls; see man 2 intro /usr/include/syscall.h 15 16

System-call interface = ADTs ADT operations File input/output open, close, read, write, dup Process control fork, exit, wait, kill, exec,... Interprocess communication pipe, socket... (int) (iret) 17 18 open system call NAME open - open and possibly create a file or device SYNOPSIS #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open(const char *pathname, int flags, mode_t mode); DESCRIPTION flags examples: O_RDONLY O_WRITE O_CREATE mode is the permissions to use if file must be created The open() system call is used to convert a pathname into a file descriptor (a small, non-negative integer for use in subsequent I/O as with read, write, etc.). When the call is successful, the file descriptor returned will be... 19 close system call NAME close - close a file descriptor SYNOPSIS int close(int fd); DESCRIPTION close closes a file descriptor, so that it no longer refers to any file and may be reused. Any locks held on the file it was associated with, and owned by the process, are removed (regardless of the file descriptor that was used to obtain the lock).... 20

read System Call write System Call NAME read - read from a file descriptor SYNOPSIS int read(int fd, void *buf, int count); DESCRIPTION read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf. If count is zero, read() returns zero and has no other results. If count is greater than SSIZE_MAX, the result is unspecified. RETURN VALUE On success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number. It is not an error if this number is smaller than the number of bytes requested.... On error, -1 is returned, and errno is set appropriately. 21 22 NAME write write to a file descriptor SYNOPSIS int write(int fd, void *buf, int count); DESCRIPTION write writes up to count bytes to the file referenced by the file descriptor fd from the buffer starting at buf. RETURN VALUE On success, the number of bytes written is returned (zero indicates nothing was written). It is not an error if this number is smaller than the number of bytes requested.... On error, -1 is returned, and errno is set appropriately. Making Sure It All Gets Written 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; Buffered I/O Single-character I/O is usually too slow int getchar(void) { char c; if (read(0, &c, 1) == 1) return c; else return EOF; 23 24

Buffered I/O (cont) Solution: read a chunk and dole out as needed int getchar(void) { static char buf[1024]; static char *p; static int n = 0; if (n--) return *p++; n = read(0, buf, sizeof(buf)); if (n <= 0) return EOF; p = buf; return getchar(); Standard I/O Library #define getc(p) (--(p)->_cnt >= 0? \ (int)(*(unsigned char *)(p)->_ptr++) : \ _filbuf(p)) typedef struct _iobuf { int _cnt; /* num chars left in buffer */ char *_ptr; /* ptr to next char in buffer */ char *_base; /* beginning of buffer */ int _bufsize;/* size of buffer */ short _flag; /* open mode flags, etc. */ char _file; /* associated file descriptor */ FILE; extern FILE *stdin, *stdout, *stderr; 25 26 Why Is getc A Macro? #define getc(p) (--(p)->_cnt >= 0? \ (int)(*(unsigned char *)(p)->_ptr++) : \ _filbuf(p)) #define getchar() getc(stdin) Invented in 1970s, when Computers had slow function-call instructions Compilers couldn t inline-expand very well It s not 1975 any more Moral: don t invent new macros, use functions fopen FILE *fopen(char *name, char *rw) { Use malloc to create a struct _iobuf Determine appropriate flags from rw parameter Call open to get the file descriptor Fill in the _iobuf appropriately 27 28

Stdio library fopen, fclose feof, ferror, fileno, fstat status inquiries fflush make outside world see changes to buffer fgetc, fgets, fread fputc fputs, fwrite printf, fprintf scanf, fscanf fseek and more... This (large) library interface is not the operating-system interface; much more room for flexibility. This ADT is implemented in terms of the lower-level file-descriptor ADT. Summary OS is the software between hardware and applications Abstraction: provide services to access the hardware Virtualization: Provides each process with its own machine Protection & security: make the environment safe System calls ADT for the user applications Standard I/O example -level libraries layered on top of system calls 29 30