Outline. Relationship between file descriptors and open files

Similar documents
read(2) There can be several cases where read returns less than the number of bytes requested:

Input and Output System Calls

Recitation 8: Tshlab + VM

CS 25200: Systems Programming. Lecture 14: Files, Fork, and Pipes

File I/0. Advanced Programming in the UNIX Environment

File Descriptors and Piping

Contents. IPC (Inter-Process Communication) Representation of open files in kernel I/O redirection Anonymous Pipe Named Pipe (FIFO)

CSE 333 SECTION 3. POSIX I/O Functions

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

Programmation Système Cours 4 IPC: FIFO

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

CSE 410: Systems Programming

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

Files and Directories

Programmation Systèmes Cours 4 IPC: FIFO

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

POSIX Shared Memory. Linux/UNIX IPC Programming. Outline. Michael Kerrisk, man7.org c 2017 November 2017

Pipes and FIFOs. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

Operating System Labs. Yuanbin Wu

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

Advanced Unix/Linux System Program. Instructor: William W.Y. Hsu

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

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

Like select() and poll(), epoll can monitor multiple FDs epoll returns readiness information in similar manner to poll() Two main advantages:

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

CSE 333 SECTION 3. POSIX I/O Functions

CSci 4061 Introduction to Operating Systems. File Systems: Basics

Contents. PA1 review and introduction to PA2. IPC (Inter-Process Communication) Exercise. I/O redirection Pipes FIFOs

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

Process Management! Goals of this Lecture!

Chapter 3. File I/O. System Programming 熊博安國立中正大學資訊工程學系

Computer Science 162, Fall 2014 David Culler University of California, Berkeley Midterm 2 November 14, 2014

Lecture 17. Log into Linux. Copy two subdirectories in /home/hwang/cs375/lecture17/ $ cp r /home/hwang/cs375/lecture17/*.

Outline. Overview. Linux-specific, since kernel 2.6.0

Programmation Systèmes Cours 8 Synchronization & File Locking

which maintain a name to inode mapping which is convenient for people to use. All le objects are

Operating System Labs. Yuanbin Wu

CS2028 -UNIX INTERNALS

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

ADVANCED I/O. ISA 563: Fundamentals of Systems Programming

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

CSE 410: Systems Programming

UNIX System Calls. Sys Calls versus Library Func

IPC and Unix Special Files

Chapter 3 Processes we will completely ignore threads today

UNIT I Linux Utilities

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

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

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

CS 33. Shells and Files. CS33 Intro to Computer Systems XX 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Network Socket Programming - 1 BUPT/QMUL

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

Computer Science 162, Fall 2014 David Culler University of California, Berkeley Midterm 2 November 14, 2014

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

Lecture 23: System-Level I/O

UNIX System Programming

UNIX System Programming. Overview. 1. A UNIX System. 2. Processes (review) 2.1. Context. Pipes/FIFOs

Process Management 1

CISC2200 Threads Spring 2015

Sistemas Operativos /2016 Support Document N o 1. Files, Pipes, FIFOs, I/O Redirection, and Unix Sockets

File I/O - Filesystems from a user s perspective

The Shell, System Calls, Processes, and Basic Inter-Process Communication

Naked C Lecture 6. File Operations and System Calls

UNIT I Linux Utilities and Working with Bash

Files and the Filesystems. Linux Files

CSC209H Lecture 11. Dan Zingaro. March 25, 2015

Process Management! Goals of this Lecture!

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

Topics. Unix Pipes (CSE 422S) In A Nutshell. Ken Wong Washington University. Pipe (Unnamed FIFO) in the Shell.

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

ECE 650 Systems Programming & Engineering. Spring 2018

Operating systems. Lecture 7

UNIX System Overview E. Im

Windows architecture. user. mode. Env. subsystems. Executive. Device drivers Kernel. kernel. mode HAL. Hardware. Process B. Process C.

T4-Input/Output Management

CSC 271 Software I: Utilities and Internals

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

Processes. Processes (cont d)

CSCE 548 Building Secure Software Dirty COW Race Condition Attack

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

CS631 - Advanced Programming in the UNIX Environment. Dæmon processes, System Logging, Advanced I/O

Outline. CSCI 4730/6730 Systems Programming Refresher. What is a Pipe? Example: Shell Pipes. Programming with Pipes

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

Outline. Basic features of BPF virtual machine

UNIX Structure. Operating Systems In Depth VII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

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

CS240: Programming in C

System Calls & Signals. CS449 Spring 2016

CS 261 Fall Mike Lam, Professor. Exceptional Control Flow and Processes

Creating a Shell or Command Interperter Program CSCI411 Lab

Process Creation in UNIX

CS240: Programming in C

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

OPERATING SYSTEMS: Lesson 2: Operating System Services

UNIX Kernel. UNIX History

Preview. Process Control. What is process? Process identifier The fork() System Call File Sharing Race Condition. COSC350 System Software, Fall

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING SOFTWARE ENGINEERING DEPARTMENT

ISA 563: Fundamentals of Systems Programming

Transcription:

Outline 3 File I/O 3-1 3.1 File I/O overview 3-3 3.2 open(), read(), write(), and close() 3-7 3.3 The file offset and lseek() 3-21 3.4 Atomicity 3-30 3.5 Relationship between file descriptors and open files 3-35 3.6 Duplicating file descriptors 3-44 3.7 File status flags (and fcntl()) 3-50 3.8 Other file I/O interfaces 3-58 Relationship between file descriptors and open files Multiple file descriptors can refer to same open file 3 kernel data structures describe relationship: Linux/UNIX System Programming c 2017, Michael Kerrisk File I/O 3-36 3.5

File descriptor table Per-process table with one entry for each FD opened by process: Flags controlling operation of FD (close-on-exec flag) Reference to open file description struct fdtable in include/linux/fdtable.h Linux/UNIX System Programming c 2017, Michael Kerrisk File I/O 3-37 3.5 Open file table (table of open file descriptions) System-wide table, one entry for each open file on system: File offset File access mode (R / W / R-W, from open()) File status flags (from open()) Signal-driven I/O settings Reference to i-node object for file struct file in include/linux/fs.h Following terms are commonly treated as synonyms: open file description (OFD) (POSIX) open file table entry or open file handle (These two are ambiguous; POSIX terminology is preferable) Linux/UNIX System Programming c 2017, Michael Kerrisk File I/O 3-38 3.5

(In-memory) i-node table System-wide table drawn from file i-node information in filesystem: File type (regular file, FIFO, socket,... ) File permissions Other file properties (size, timestamps,... ) struct inode in include/linux/fs.h Linux/UNIX System Programming c 2017, Michael Kerrisk File I/O 3-39 3.5 Duplicated file descriptors (intraprocess) A process may have multiple FDs referring to same OFD Achieved using dup() or fcntl() Linux/UNIX System Programming c 2017, Michael Kerrisk File I/O 3-40 3.5

Duplicated file descriptors (between processes) Two processes may have FDs referring to same OFD Can occur as a result of fork() Linux/UNIX System Programming c 2017, Michael Kerrisk File I/O 3-41 3.5 Distinct open file table entries referring to same file Two processes may have FDs referring to distinct OFDs that refer to same i-node Two processes independently open()ed same file Linux/UNIX System Programming c 2017, Michael Kerrisk File I/O 3-42 3.5

Why does this matter? Two different FDs referring to same OFD share file offset (File offset == location for next read()/write()) Changes (read(), write(), lseek()) via one FD visible via other FD Applies to both intraprocess & interprocess sharing of OFD Similar scope rules for status flags (O_APPEND, O_SYNC,... ) Changes via one FD are visible via other FD (fcntl(f_setfl) and fcntl(f_getfl)) Conversely, changes to FD flags (held in FD table) are private to each process and FD kcmp(2) KCMP_FILES operation can be used to test if two FDs refer to same OFD Linux-specific [TLPI 5.4] Linux/UNIX System Programming c 2017, Michael Kerrisk File I/O 3-43 3.5

Outline 3 File I/O 3-1 3.1 File I/O overview 3-3 3.2 open(), read(), write(), and close() 3-7 3.3 The file offset and lseek() 3-21 3.4 Atomicity 3-30 3.5 Relationship between file descriptors and open files 3-35 3.6 Duplicating file descriptors 3-44 3.7 File status flags (and fcntl()) 3-50 3.8 Other file I/O interfaces 3-58 A problem./ myprog > output. log 2 >&1 What does the shell syntax, 2>&1, do? How does the shell do it? Open file twice, once on FD 1, and once on FD 2? FDs would have separate OFDs with distinct file offsets standard output and error would overwrite File may not even be open()-able: e.g.,./myprog 2>&1 less Need a way to create duplicate FD that refers to same OFD [TLPI 5.5] Linux/UNIX System Programming c 2017, Michael Kerrisk File I/O 3-46 3.6

Duplicating file descriptors # include < unistd.h> int dup ( int oldfd ); Arguments: oldfd: an existing file descriptor Returns new file descriptor (on success) New file descriptor is guaranteed to be lowest available Linux/UNIX System Programming c 2017, Michael Kerrisk File I/O 3-47 3.6 Duplicating file descriptors FDs 0, 1, and 2 are normally always open, so shell can achieve 2>&1 redirection by: close ( STDERR_FILENO ); /* Frees FD 2 */ newfd = dup ( STDOUT_FILENO ); /* Reuses FD 2 */ But what if FD 0 was closed? Linux/UNIX System Programming c 2017, Michael Kerrisk File I/O 3-48 3.6

Duplicating file descriptors # include < unistd.h> int dup2 ( int oldfd, int newfd ); Like dup(), but uses newfd for the duplicate FD Silently closes newfd if it was open Closing + reusing newfd is done atomically Important: otherwise newfd might be re-used in between Does nothing if newfd == oldfd Returns new file descriptor (i.e., newfd) on success dup2(stdout_fileno, STDERR_FILENO); See dup2(2) man page for more details [TLPI 5.5] Linux/UNIX System Programming c 2017, Michael Kerrisk File I/O 3-49 3.6

Outline 3 File I/O 3-1 3.1 File I/O overview 3-3 3.2 open(), read(), write(), and close() 3-7 3.3 The file offset and lseek() 3-21 3.4 Atomicity 3-30 3.5 Relationship between file descriptors and open files 3-35 3.6 Duplicating file descriptors 3-44 3.7 File status flags (and fcntl()) 3-50 3.8 Other file I/O interfaces 3-58 File status flags Control semantics of I/O on a file (O_APPEND, O_NONBLOCK, O_SYNC,... ) Associated with open file description Set when file is opened Can be retrieved and modified using fcntl() [TLPI 5.3] Linux/UNIX System Programming c 2017, Michael Kerrisk File I/O 3-52 3.7

fcntl() : file control operations # include <fcntl.h> int fcntl ( int fd, int cmd /*, arg */ ); Performs control operations on an open file Arguments: fd: file descriptor cmd: the desired operation arg: optional, type depends on cmd Return on success depends on cmd; -1 returned on error Many types of operation file locking, signal-driven I/O, file descriptor flags... Linux/UNIX System Programming c 2017, Michael Kerrisk File I/O 3-53 3.7 Retrieving file status flags and access mode Retrieving flags (both access mode and status flags) flags = fcntl ( fd, F_GETFL ); Check access mode amode = flags & O_ACCMODE ; if ( amode == O_RDONLY amode == O_RDWR ) printf (" File is readable \n"); read and write are not separate bits! if ( flags & O_RDONLY ) /* Wrong!! */ printf (" File is readable \n"); Access mode is a 2-bit field that is an enumeration: 00 == O_RDONLY 01 == O_WRONLY 10 == O_RDWR Access mode can t be changed after file is opened Linux/UNIX System Programming c 2017, Michael Kerrisk File I/O 3-54 3.7

Retrieving and modifying file status flags Retrieving file status flags flags = fcntl ( fd, F_GETFL ); if ( flags & O_NONBLOCK ) printf (" Nonblocking I/O is in effect \n"); Setting a file status flag flags = fcntl ( fd, F_GETFL ); /* Retrieve flags */ flags = O_APPEND ; /* Set " append " bit */ fcntl ( fd, F_SETFL, flags ); /* Modify flags */ Not thread-safe... (But in many cases, flags can be set when FD is created, e.g., by open()) Clearing a file status flag flags = fcntl ( fd, F_GETFL ); /* Retrieve flags */ flags &= ~ O_APPEND ; /* Clear " append " bit */ fcntl ( fd, F_SETFL, flags ); /* Modify flags */ Linux/UNIX System Programming c 2017, Michael Kerrisk File I/O 3-55 3.7 Exercise 1 Show that duplicate file descriptors share file offset and file status flags by writing a program ([template: fileio/ex.fd_sharing.c]) that: Opens an existing file (supplied as argv[1]) and duplicates (dup()) the resulting file descriptor, to create a second file descriptor. Displays the file offset and the state of the O_APPEND file status flag via the first file descriptor. Initially the file offset will be zero, and the O_APPEND flag will not be set Changes the file offset and enables (turns on) the O_APPEND file status flag via the first file descriptor. Displays the file offset and the state of the O_APPEND file status flag via the second file descriptor. Linux/UNIX System Programming c 2017, Michael Kerrisk File I/O 3-56 3.7

Exercise 2 Read about the KCMP_FILE operation in the kcmp(2) man page. Extend the program created in the preceding exercise to use this operation to verify that the two file descriptors refer to the same open file description (i.e., use kcmp(getpid(), getpid(), KCMP_FILE, fd1, fd2)). Note: because there is currently no kcmp() wrapper function in glibc, you will have to write one yourself using syscall(2): # define _GNU_SOURCE # include < unistd.h> # include <sys / syscall.h> static int kcmp ( pid_t pid1, pid_t pid2, int type, unsigned long idx1, unsigned long idx2 ) { return syscall ( SYS_kcmp, pid1, pid2, type, idx1, idx2 ); } Linux/UNIX System Programming c 2017, Michael Kerrisk File I/O 3-57 3.7