Lecture 16: System-Level I/O



Similar documents
Giving credit where credit is due

System Calls Related to File Manipulation

64 Bit File Access. 1.0 Introduction. 2.0 New Interfaces. 2.1 New User Visible Types. Adam Sweeney

System Calls and Standard I/O

Lecture 24 Systems Programming in C

Linux Kernel Architecture

Interlude: File and Directories

As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets!

File Handling. What is a file?

Operating Systems. Privileged Instructions

Linux/UNIX System Programming. POSIX Shared Memory. Michael Kerrisk, man7.org c February 2015

Illustration 1: Diagram of program function and data flow

Networks. Inter-process Communication. Pipes. Inter-process Communication

CSI 402 Lecture 13 (Unix Process Related System Calls) 13 1 / 17

OS: IPC I. Cooperating Processes. CIT 595 Spring Message Passing vs. Shared Memory. Message Passing: Unix Pipes

1. Introduction to the UNIX File System: logical vision

Lab 4: Socket Programming: netcat part

Operating System Structure

UNIX File Management (continued)

LOW LEVEL FILE PROCESSING

Chapter 10 Case Study 1: LINUX

Unix System Calls. Dept. CSIE

Operating Systems and Networks

The C Programming Language course syllabus associate level

System calls. Problem: How to access resources other than CPU

File Systems Management and Examples

Programmation Systèmes Cours 7 IPC: FIFO

/* File: blkcopy.c. size_t n

Lab 2 : Basic File Server. Introduction

Outline. Review. Inter process communication Signals Fork Pipes FIFO. Spotlights

Intro to GPU computing. Spring 2015 Mark Silberstein, , Technion 1

Transparent Monitoring of a Process Self in a Virtual Environment

Chapter 6, The Operating System Machine Level

Computer Systems II. Unix system calls. fork( ) wait( ) exit( ) How To Create New Processes? Creating and Executing Processes

Shared Memory Introduction

Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z)

Leak Check Version 2.1 for Linux TM

Intel P6 Systemprogrammering 2007 Föreläsning 5 P6/Linux Memory System

NDK: NOVELL NSS AUDIT

Introduction. dnotify

CSC 2405: Computer Systems II

Have both hardware and software. Want to hide the details from the programmer (user).

Chapter 12 File Management

Chapter 12 File Management. Roadmap

Fortify on Demand Security Review. Fortify Open Review

CS3600 SYSTEMS AND NETWORKS

Device Management Functions

Chapter I/O and File System

Process definition Concurrency Process status Process attributes PROCESES 1.3

ProTrack: A Simple Provenance-tracking Filesystem

Operating System Structures

Laboratory Report. An Appendix to SELinux & grsecurity: A Side-by-Side Comparison of Mandatory Access Control & Access Control List Implementations

Xillybus host application programming guide for Linux

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

File Management. Chapter 12

CS162 Operating Systems and Systems Programming Lecture 4. Introduction to I/O (Continued), Sockets, Networking. Recall: Fork and Wait

Network File System (NFS)

Module 816. File Management in C. M. Campbell 1993 Deakin University

Lecture 25 Systems Programming Process Control

Operating System Components and Services

Virtual Memory. How is it possible for each process to have contiguous addresses and so many of them? A System Using Virtual Addressing

Distributed Systems [Fall 2012]

Operating Systems Concepts: Chapter 7: Scheduling Strategies

Building accurate intrusion detection systems. Diego Zamboni Global Security Analysis Lab IBM Zürich Research Laboratory

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Operating System Engineering: Fall 2005

Grundlagen der Betriebssystemprogrammierung

COSC 6374 Parallel Computation. Parallel I/O (I) I/O basics. Concept of a clusters

How To Understand How A Process Works In Unix (Shell) (Shell Shell) (Program) (Unix) (For A Non-Program) And (Shell).Orgode) (Powerpoint) (Permanent) (Processes

COSC 6374 Parallel Computation. Parallel I/O (I) I/O basics. Concept of a clusters

The Linux Virtual Filesystem

Migration of Process Credentials

Linux Syslog Messages in IBM Director

DATABASE MANAGEMENT SYSTEMS

Operating Systems Design 16. Networking: Sockets

Lecture 16: Storage Devices

Operating Systems. 05. Threads. Paul Krzyzanowski. Rutgers University. Spring 2015

Lecture 17. Process Management. Process Management. Process Management. Inter-Process Communication. Inter-Process Communication

Undergraduate Course Syllabus

Operating System Components

How ToWrite a UNIX Daemon

CHAPTER 17: File Management

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Storage Management. in a Hybrid SSD/HDD File system

Introduction to Socket programming using C

CS161: Operating Systems

COS 217: Introduction to Programming Systems

CSC230 Getting Starting in C. Tyler Bletsch

COS 318: Operating Systems. File Layout and Directories. Topics. File System Components. Steps to Open A File

Runtime Monitoring & Issue Tracking

As shown, the emulator instance connected to adb on port 5555 is the same as the instance whose console listens on port 5554.

Software Vulnerabilities

CS420: Operating Systems OS Services & System Calls

Objectives. Chapter 2: Operating-System Structures. Operating System Services (Cont.) Operating System Services. Operating System Services (Cont.

1 Posix API vs Windows API

Capability-Based Access Control

ELEC 377. Operating Systems. Week 1 Class 3

CS 377: Operating Systems. Outline. A review of what you ve learned, and how it applies to a real operating system. Lecture 25 - Linux Case Study

Transcription:

CSCI-UA.0201-003 Computer Systems Organization Lecture 16: System-Level I/O Mohamed Zahran (aka Z) mzahran@cs.nyu.edu http://www.mzahran.com Some slides adapted (and slightly modified) from: Clark Barrett Jinyang Li Randy Bryant Dave O Hallaron

CPU Memory

I/O Devices Very diverse devices behavior (i.e., input vs. output vs. storage) partner (who is at the other end?) data rate I/O Design affected by many factors (expandability, resilience) Performance: access latency throughput connection between devices and the system the memory hierarchy the operating system A variety of different users

Application programs If this is enough Language Run-time Systems high-level facility for I/O (e.g. ANSI C standard I/O) Kernel Level I/O system calls Why bother learning this?

Why Bother? Understanding kernel-level I/O will help you understand other systems concepts I/O plays a key role in process creation and execution Process creation plays a key role in how files are shared by different processes Sometimes language run-time is not enough to do what you want

Unix I/O A file is a sequence of m bytes. All I/O devices are modeled as files. All I/O is performed by reading and writing the appropriate files. Opening a file: an application wants to use and I/O device. Kernel gives the application a file descriptor (nonnegative integer) Changing the current file position: position is a byte offset from the beginning of the file (kept by kernel) Reading and writing files Closing files

Unix I/O UNIX abstracts many things into files E.g. regular files, devices (/dev/sda2), FIFO pipes, sockets Allow a common set of syscalls for handling I/O E.g. reading and writing to files/pipes/sockets: read and write

Overview of File System implementation in UNIX file data dir block root / inode 0 dir block inode 1 f1.txt 2 inode 2 home user Inode 3 f2.txt 2 Inodes contain meta-data about files/directories Last modification time, size, user id Hard links: multiple names for the same file (/home/f1.txt and /usr/f2.txt refer to the same file)

UNIX I/O (i.e. I/O related syscalls) Getting meta-data (info maintained in i- nodes) Stat Directory operations opendir, readdir, rmdir Open/close files Open,close Read/write files read/write

File Metadata Access file meta-data using stat syscall Example: rkmatch.c void read_file(const char *fname, char **doc, int *doc_len) { struct stat st; if (stat(fd, &st)!= 0) { perror("read_file: fstat "); exit(1); } } *doc = (char *)malloc(st.st_size);

File Metadata Access file meta-data using stat syscall struct stat { dev_t st_dev; /* ID of device containing file */ ino_t st_ino; /* inode number */ mode_t st_mode; /* protection */ nlink_t st_nlink; /* number of hard links */ uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ dev_t st_rdev; /* device ID (if special file) */ off_t st_size; /* total size, in bytes */ blksize_t st_blksize; /* block size for file system I/O */ blkcnt_t st_blocks; /* number of 512B blocks allocated */ time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last status change */ };

Opening Files Open a file before access: Returns a small integer file descriptor (or -1 for error) int fd; /* file descriptor */ if ((fd = open( X", O_RDONLY)) < 0) { perror("open"); exit(1); } For more info, do man 2 open Why fd? Kernel maintains an array of info on currently opened files for a process fd indexes into this in-kernel array Each process starts out with three open files 0: standard input 1: standard output 2: standard error

Closing Files Closing a file informs kernel that you are finished accessing that file int fd; /* file descriptor */ if (close(fd) < 0) { perror("close"); exit(1); }

Simple read/write example Copying standard in to standard out, one byte at a time #include <stdio.h> int main(void) { char c; Returns # of bytes read, -1 for error } while(read(stdin_fileno, &c, 1) == 1){ write(stdout_fileno, &c, 1); } exit(0); cpstdin.c Returns # of bytes written, -1 for error

Kernel Presentation of Open Files Kernel uses 3 related data structures to represent open files Descriptor table: per process Indexed by the process open file descriptor Each entry points to an entry in the file table File table: Shared by all processes Each entry contains info about file position, reference count,, and a pointer to an entry in the v-node table v-node table: Shared by all processes contains info that can be read by stat syscall

Kernel tracks user processes opened files kernel state Descriptor table [one table per process] Open file table [shared by all processes] v-node table [shared by all processes] stdin stdout stderr fd 0 fd 1 fd 2 fd 3 fd 4 File A (terminal) File pos refcnt=1 File access File size File type Info in stat struct File B (disk) File access File pos refcnt=1 File size File type

Kernel tracks user processes opened files Calling open twice with the same filename Descriptor table [one table per process] Open file table [shared by all processes] v-node table [shared by all processes] stdin stdout stderr fd 0 fd 1 fd 2 fd 3 fd 4 File A (disk) File pos refcnt=1 File access File size File type File B (disk) File pos refcnt=1

Child process inherits its parent s Before fork() call: open files Descriptor table [one table per process] Open file table [shared by all processes] v-node table [shared by all processes] stdin stdout stderr fd 0 fd 1 fd 2 fd 3 fd 4 File A (terminal) File pos refcnt=1 File access File size File type File B (disk) File access File pos refcnt=1 File size File type

Child process inherits its parent s open files After fork(): Child s descriptor table same as parent s, and +1 to each refcnt Descriptor table [one table per process] fd 0 fd 1 fd 2 fd 3 fd 4 Parent Open file table [shared by all processes] File A (terminal) File pos refcnt=2 v-node table [shared by all processes] File access File size File type fd 0 fd 1 fd 2 fd 3 fd 4 Child File B (disk) File pos refcnt=2 File access File size File type

Fun with File Descriptors (fork) #include <stdio.h> #include <fcntl.h> int main(int argc, char *argv[]) { int fd1; char c1, c2; char *fname = argv[1]; fd1 = open(fname, O_RDONLY, 0); read(fd1, &c1, 1); if (fork()) { /* Parent */ read(fd1, &c2, 1); printf("parent: c1 = %c, c2 = %c\n", c1, c2); } else { /* Child */ sleep(5); read(fd1, &c2, 1); printf("child: c1 = %c, c2 = %c\n", c1, c2); } return 0; } Solution: Parent: c1 = a, c2 = b Child: c1 = a, c2 = c What would this program print for file containing abcde?

Fun with File Descriptors (dup2) #include <stdio.h> #include <fcntl.h> int main(int argc, char *argv[]) { int fd1, fd2, fd3; char c1, c2, c3; char *fname = argv[1]; fd1 = open(fname, O_RDONLY, 0); fd2 = open(fname, O_RDONLY, 0); fd3 = open(fname, O_RDONLY, 0); dup2(fd2, fd3); read(fd1, &c1, 1); read(fd2, &c2, 1); read(fd3, &c3, 1); printf("c1 = %c, c2 = %c, c3 = %c\n", c1, c2, c3); return 0; } ffiles1.c Solution: c1 = a, c2= a, c3 = b What would this program print for file containing abcde?

I/O Redirection How does a shell redirect I/O? unix$ ls > foo.txt Use syscall dup2(oldfd, newfd) Copies descriptor table entry oldfd to entry newfd Descriptor table before dup2(4,1) Descriptor table after dup2(4,1) fd 0 fd 1 fd 2 fd 3 fd 4 a b fd 0 fd 1 fd 2 fd 3 fd 4 b b

I/O Redirection Example Step #1: open output file to which stdout should be redirected Descriptor table [one table per process] Open file table [shared by all processes] v-node table [shared by all processes] stdin stdout stderr fd 0 fd 1 fd 2 fd 3 fd 4 File A File pos refcnt=1 File access File size File type Opened file has fd=4 File B File pos refcnt=1 File access File size File type

I/O Redirection Example (cont.) Step #2: call dup2(4,1) cause fd=1 (stdout) to refer to disk file pointed at by fd=4 Descriptor table [one table per process] Open file table [shared by all processes] v-node table [shared by all processes] stdin stdout stderr fd 0 fd 1 fd 2 fd 3 fd 4 File A File pos refcnt=0 File access File size File type File B File access File pos refcnt=2 File size File type

Standard I/O Functions The C library (libc.so) contains a collection of higher-level standard I/O functions fopen fdopen fread fwrite fscanf fprintf sscanf sprintf fgets fputs fflush fseek fclose Internally invokes I/O syscalls open read write lseek stat close

Standard I/O Streams Standard I/O implements buffered streams Abstraction for a file descriptor and a buffer in memory. C programs begin life with three open streams stdin (standard input) stdout (standard output) stderr (standard error) #include <stdio.h> extern FILE *stdin; /* standard input (descriptor 0) */ extern FILE *stdout; /* standard output (descriptor 1) */ extern FILE *stderr; /* standard error (descriptor 2) */ int main() { fprintf(stdout, "Hello, world\n"); }

Unix I/O vs. standard I/O Unix I/O: Pros most general, lowest overhead. All other I/O packages are implemented using Unix I/O functions. Provides functions for accessing file metadata. async-signal-safe and can be used safely in signal handlers. Cons Efficient reading/writing may require some form of buffering

Unix I/O vs. Standard I/O: Standard I/O: Pros: Buffering increases efficiency by reducing # of read and write system calls Cons: Provides no function for accessing file metadata Not async-signal-safe, and not appropriate for signal handlers. Not appropriate for input and output on network sockets

Choosing I/O Functions General rule: use the highest-level I/O functions you can Many C programmers are able to do all of their work using the standard I/O functions When to use standard I/O When working with disk or terminal files When to use raw Unix I/O Inside signal handlers, because Unix I/O is async-signal-safe. When working with network sockets In rare cases when you want to tune for absolute highest performance.

Standard I/O Buffering in Action You can see this buffering in action for yourself use strace to monitor a program s syscall invocation: #include <stdio.h> void main() { char c; while ((c = getc(stdin))!='\n') { printf("%c",c); } printf("\n"); } linux% strace./a.out execve("./a.out", [./a.out"], [/* */]). read(0,"hello\n", 1024) = 6 write(1, "hello\n", 6) = 6 exit_group(0) =?

Conclusions UNIX/LINUX use files to abstract many I/O devices Accessing files can be done either by standard I/O or UNIX I/O