CSE 333 – Section 3 POSIX I/O Functions.

Slides:



Advertisements
Similar presentations
Threads CS241 Discussion Section Week 3 2/09/09 – 2/13/09.
Advertisements

January 13, Csci 2111: Data and File Structures Week1, Lecture 2 Basic File Processing Operations.
CS 241 Section Week #5 2/23/12. 2 Topics This Section MP4 overview Function Pointers Pthreads File I/O.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
CS 311 – Lecture 09 Outline Introduction to Systems programming – System calls – Categories of system calls Error Management System calls File Handling.
CS 311 – Lecture 10 Outline Review open() and close() Difference between fopen() and open() File management system calls – read() – write() – lseek() –
CSE 451 Section 4 Project 2 Design Considerations.
Lecture 17 FS APIs and vsfs. File and File Name What is a File? Array of bytes. Ranges of bytes can be read/written. File system consists of many files,
CS 311 – Lecture 12 Outline File management system calls Stat() Directory Information  Opendir()  Readdir()  Closedir() Truncate() and remove() Lecture.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
1 UNIX System Programming v Objectives –look at how to program with directories –briefly describe the UNIX file system Directories and File System.
Operating Systems Recitation 1, March th, 2002.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
File Handling In C By - AJAY SHARMA. We will learn about- FFile/Stream TText vs Binary Files FFILE Structure DDisk I/O function OOperations.
Adv. UNIX: dirs/181 Advanced UNIX v Objectives –look at how to program with directories –briefly describe the UNIX file system Special.
Directory structure. Slide 2 Directory Structure  A directory ‘file’ is a sequence of lines; each line holds an i-node number and a file name.  The.
UNIX Files File organization and a few primitives.
File IO and command line input CSE 2451 Rong Shi.
File I/O, Project 1: List ADT Bryce Boe 2013/07/02 CS24, Summer 2013 C.
Today’s topic Access and manipulate meta data for files –File type, ownership, access permissions, access time, etc How to determine if a file is not there?
CSCI 330 UNIX and Network Programming Unit VII: I/O Management I.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Files and file allocation.
NCHU System & Network Lab Lab 14 File and Directory.
CSCI 330 UNIX and Network Programming
CSCC69: Operating Systems Tutorial 10. Hints on testing Assignment 3 How to test tlb replacement algorithms? – Write a program that creates an array larger.
CSE333 SECTION 3. Important Dates Jan 27 th – Homework 1 Due Feb 6 th – Midterm.
January 7, 2003Serguei Mokhov, 1 File I/O System Calls Reference COMP 229, 444, 5201 Revision 1.2 Date: July 21, 2004.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Tarek Abdelzaher Vikram Adve CS241 Systems Programming System Calls and I/O.
CS 241 Section Week #8 (10/29/09). Outline MP5 Overview Files & I/O UNIX File Systems inodes Directories Links.
File I/O open close lseek read and write – unbuffered I/O dup and dup2.
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.
CS 241 Section Week #5 9/22/11. 2 Topics This Section File I/O Advanced C.
FILES IN C. File Operations  Creation of a new file  Opening an existing file  Reading from a file  Writing to a file  Moving to a specific location.
File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
CSC 271 – Software I: Utilities and Internals An Introduction to File I/O in Linux Credited to Dr. Robert Siegfried and Beginning Linux Programming by.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
File table: a list of opened files Each entry contains: – Index: file descriptors – Pointer to the file in memory – Access mode File descriptor is a positive.
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
S ALVATORE DI G IROLAMO (TA) Networks and Operating Systems: Exercise Session 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 -
File System Design David E. Culler CS162 – Operating Systems and Systems Programming Lecture 23 October 22, 2014 Reading: A&D a HW 4 out Proj 2 out.
The Shell What does a shell do? - execute commands, programs - but how? For built in commands run some code to do the command For other commands find program.
Error handling I/O Man pages
Andrew Hanushevsky: Basic I/O API’s
Operating Systems Moti Geva
Lecture 31: Introduction to File System
Systems Programming.
Lecture 11 File input/output
Chapter 7 Text Input/Output Objectives
Linux Kernel Driver.
Week 4 - Monday CS222.
Directory Directory is a file containing list of entries,where each entry represents inode number and name of the file stored in that directory.
CSE 333 – Section 3 POSIX I/O Functions.
Operating System Hebrew University Spring 2004
CSE 333 – Section 3 POSIX I/O Functions.
Making Virtual Memory Real: The Linux-x86-64 way
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
Recitation 9 Greg Reshko
CSE 333 – Section 3 POSIX I/O Functions.
Text and Binary File Processing
File I/O (1) Prof. Ikjun Yeom TA – Mugyo
CSE 333 – Section 3 POSIX I/O Functions.
Client-side Networking CSE 333 Summer 2018
FILE I/O File Descriptors I/O Efficiency File Sharing
File I/O & UNIX System Interface
Low-Level I/O – the POSIX Layer CSE 333 Winter 2019
Chapter 12: File-System Implementation CSS503 Systems Programming
System Calls, POSIX I/O CSE 333 Spring 2019
CSE 333 – Section 3 POSIX I/O Functions.
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
Presentation transcript:

CSE 333 – Section 3 POSIX I/O Functions

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); 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 [man 2 open]

System I/O Calls ssize_t read(int fd, void *buf, size_t count); ssize_t write(int fd, const 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. int close(int fd); Returns 0 on success, -1 on failure. [man 2 read] [man 2 write] [man 2 close]

Reading a file #include <errno.h> #include <unistd.h> char *buf = ...; int bytes_read = 0; int result = 0; int fd = open("filename", O_RDONLY); // BUG: if filename is smaller than N, infinite loop! while (bytes_read < N) { result = read(fd, buf + bytes_read, N - bytes_read); if (result == -1) { if (errno != EINTR)) { // a real error happened, return an error result } // EINTR happened, do nothing and loop back around continue; bytes_read += result;

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. [man 3 errno] [man 3 perror]

Why learn these 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. Think about levels of abstraction

Standard I/O Calls Read the man pages! The most important (for you): [man 3 stdio] for a full list of functions declared in <stdio.h> The most important (for you): fopen fclose fread (!! the return value is probably not what you think !!) fwrite (!! the return value is probably not what you think !!) fseek Be sure to check out some of the others though! You might just find something interesting and/or useful!

Directories Accessing directories: Opening a directory: 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 of this record */ unsigned char d_type; /* type of file (not what you think); not supported by all file system types */ char d_name[NAME_MAX+1] ; /* directory entry name */ }; [man 3 readdir] but not [man readdir]

Section Exercise Find a partner if you wish. Write a C program that does the following: Given two files on the command line, print the contents of the first file to the second. On error, print an informative error message. Similar to cat. You must use the POSIX functions to open, close, read and write.