CSE 333 SECTION 3. POSIX I/O Functions

Similar documents
CSE 333 SECTION 3. POSIX I/O Functions

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

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

CS240: Programming in C

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

CSE 410: Systems Programming

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

Operating System Labs. Yuanbin Wu

Operating systems. Lecture 7

CSC 271 Software I: Utilities and Internals

Operating System Labs. Yuanbin Wu

UNIX System Programming

structs as arguments

Operating System Labs. Yuanbin Wu

Naked C Lecture 6. File Operations and System Calls

Goals of this Lecture

I/O Management! Goals of this Lecture!

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

I/O Management! Goals of this Lecture!

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

File Descriptors and Piping

UNIX System Calls. Sys Calls versus Library Func

Lecture 21 Systems Programming in C

OPERATING SYSTEMS: Lesson 2: Operating System Services

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

The UNIX File System. File Systems and Directories UNIX inodes Accessing directories Understanding links in directories.

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

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

File and Directories. Advanced Programming in the UNIX Environment

CSCE 313 Introduction to Computer Systems

Last Week: ! Efficiency read/write. ! The File. ! File pointer. ! File control/access. This Week: ! How to program with directories

Do not turn the page until 5:00.

Recitation 8: Tshlab + VM

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CS , Spring Sample Exam 3

All the scoring jobs will be done by script

Files. Eric McCreath

UNIX input and output

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

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

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

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

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

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

CPSC 410/611: File Management. What is a File?

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

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

FILE SYSTEMS. Jo, Heeseung

File Systems. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

Process Management! Goals of this Lecture!

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Lecture 23: System-Level I/O

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

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

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

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

CSE 333 Lecture 8 - file and network I/O

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

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

Outline. File Systems. File System Structure. CSCI 4061 Introduction to Operating Systems

CSC209F Midterm (L0101) Fall 1999 University of Toronto Department of Computer Science

CSE 333 Lecture 16 - network programming intro

Operating System Labs. Yuanbin Wu

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

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

Low-Level I/O, C++ Preview

Files and Directories

File Systems Overview. Jin-Soo Kim ( Computer Systems Laboratory Sungkyunkwan University

Preview. System Call. System Call. System Call. System Call. Library Functions 9/20/2018. System Call

File I/0. Advanced Programming in the UNIX Environment

CS360 Midterm 2 Spring, 2016 James S. Plank March 10, 2016

FILE SYSTEMS. Tanzir Ahmed CSCE 313 Fall 2018

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

Process Creation in UNIX

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

OPERATING SYSTEMS: Lesson 12: Directories

CSE 333 Midterm Exam 2/12/16. Name UW ID#

PRACTICAL NO : 1. AIM: To study various file management system calls in UNIX.

Overview. Unix System Programming. Outline. Directory Implementation. Directory Implementation. Directory Structure. Directories & Continuation

CSE 333 Section 8 - Client-Side Networking

Chapter 10. The UNIX System Interface

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

COMP 2355 Introduction to Systems Programming

All the scoring jobs will be done by script

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

ECE 650 Systems Programming & Engineering. Spring 2018

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

Input and Output System Calls

COSC Operating Systems Design, Fall Lecture Note: Unnamed Pipe and Shared Memory. Unnamed Pipes

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

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

Outline. Relationship between file descriptors and open files

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

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

File Systems. q Files and directories q Sharing and protection q File & directory implementation

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

Systems Programming. 09. Filesystem in USErspace (FUSE) Alexander Holupirek

Experiment 6 The Real World Interface

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

Transcription:

CSE 333 SECTION 3 POSIX I/O Functions

Administrivia Questions (?) HW1 Due Tonight Exercise 7 due Monday (out later today)

POSIX Portable Operating System Interface Family of standards specified by the IEEE Maintains compatibility across variants of Unix-like OS Defines API and standards for basic I/O: file, terminal and network Also defines a standard threading library API

Basic File Operations Open the file Read from the file Write to the file Close the file / free up resources

System I/O Calls int open(char* filename, int flags, mode_t mode); Returns an integer which is the file descriptor. Returns -1 if there is a failure. filename: A string representing the name of the file. flags: An integer code describing the access. O_RDONLY -- opens file for read only O_WRONLY opens file for write only O_RDWR opens file for reading and writing O_APPEND --- opens the file for appending O_CREAT -- creates the file if it does not exist O_TRUNC -- overwrite the file if it exists mode: File protection mode. Ignored if O_CREAT is not specified. [man 2 open]

System I/O Calls ssize_t read(int fd, void *buf, size_t count); fd: file descriptor. buf: address of a memory area into which the data is read. count: the maximum amount of data to read from the stream. The return value is the actual amount of data read from the file. ssize_t write(int fd, const void *buf, size_t count); int close(int fd); Returns 0 on success, -1 on failure. [man 2 read] [man 2 write] [man 2 close]

Question: Why is it important to remember to call the close() function once you have finished working on a file? In order to free resources i.e. other processes can acquire locks on those files.

Errors When an error occurs, the error number is stored in errno, which is defined under <errno.h> View/Print details of the error using perror() and errno. POSIX functions have a variety of error codes to represent different errors. Some common error conditions: EBADF - fd is not a valid file descriptor or is not open for reading. EFAULT - buf is outside your accessible address space. EINTR - The call was interrupted by a signal before any data was read. EISDIR - fd refers to a directory. errno is shared by all library functions and overwritten frequently, so you must read it right after an error to be sure of getting the right code [man 3 errno] [man 3 perror]

Reading a file #include <errno.h> #include <unistd.h>... char *buf =...; // buffer has size n int bytes_left = n; // where n is the length of file in bytes int result = 0; while (bytes_left > 0) { result = read(fd, buf + (n-bytes_left), bytes_left); if (result == -1) { if (errno!= EINTR) { // a real error happened, return an error result } // EINTR happened, do nothing and loop back around continue; } bytes_left -= result; }

Again, why are we learning POSIX functions? They are unbuffered. You can implement different buffering/caching strategies on top of read/write. More explicit control since read and write functions are system calls and you can directly access system resources. There is no standard higher level API for network and other I/O devices.

STDIO vs. POSIX Functions User mode vs. Kernel mode. STDIO library functions fopen, fread, fwrite, fclose, etc. use FILE* pointers. POSIX functions open, read, write, close, etc. use integer file descriptors.

Exercise 1 Given the name of a file as a command-line argument, write a C program that is analogous to cat, i.e. one that prints the contents of the file to stdout. Handle any errors! int main(int argc, char** argv) { /* 1. Check to make sure we have a valid command line arguments */ /* 2. Open the file, use O_RDONLY flag */ /* 3. Read from the file and write it to standard out. Try doing this without using printf() and instead have write() pipe to Stdout. It might be helpful to initialize a buffer variable (of size 1024 bytes should be fine) to pass in to read() and write(). /*4. Clean up */ }

Directories Accessing directories: Open a directory Iterate through its contents Close the directory Opening a directory: DIR *opendir(const char* name); Opens a directory given by name and provides a pointer DIR* to access files within the directory. Don t forget to close the directory when done: int closedir(dir *dirp); [man 0P dirent.h] [man 3 opendir] [man 3 closedir]

Directories Reading a directory file. struct dirent *readdir(dir *dirp); struct dirent { ino_t d_ino; /* inode number for the dir entry */ off_t d_off; /* not necessarily an offset */ unsigned short d_reclen; /* length (in bytes) of this record */ unsigned char d_type; char terminated */ }; /* type of file (not what you think); not supported by all file system types */ d_name[name_max+1] ; /* directory entry name, null [man 3 readdir] [man readdir]

Read the man pages man, section 2: Linux system calls man 2 intro man 2 syscalls man 2 open man 2 read man, section 3: glibc / libc library functions man 3 intro man 3 fopen man 3 fread man 3 stdio for a full list of functions declared in <stdio.h>

Exercise 2 Given the name of a directory, write a C program that is analogous to ls, i.e. prints the names of the entries of the directory to stdout. Handle any errors! int main(int argc, char** argv) { /* 1. Check to make sure we have a valid command line arguments */ /* 2. Open the directory, look at opendir() */ /* 3. Read through/parse the directory and print out file names. Look at readdir() and struct dirent */ /* 4. Clean up */ }