CSE 333 – Section 3 POSIX I/O Functions.

Slides:



Advertisements
Similar presentations
Recitation By yzhuang, sseshadr. Agenda Debugging practices – GDB – Valgrind – Strace Errors and Wrappers – System call return values and wrappers – Uninitialization.
Advertisements

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.
1 Advanced programming in UNIX 1 File I/O Hua LiSystems ProgrammingCS2690File I/O.
CS 311 – Lecture 10 Outline Review open() and close() Difference between fopen() and open() File management system calls – read() – write() – lseek() –
Concurrency Sharing of resources in the same time frame Apparent concurrency is sharing the same CPU, memory, or I/O device Real concurrency is sharing.
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 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
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.
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.
Navigating Directories
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
Lecture 3 Introduction to Unix Systems Programming: Unix File I/O System Calls.
CSCI 330 UNIX and Network Programming Unit VII: I/O Management I.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Files and file allocation.
CSCI 330 UNIX and Network Programming
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.
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.
Using System Calls (Unix) Have to tell compiler (if C/C++) where to find the headers, etc. – i.e., the “include” files May have to tell compiler where.
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
CS/COE 0449 (term 2174) Jarrett Billingsley
Andrew Hanushevsky: Basic I/O API’s
Sorting Tutorial Using C On Linux.
Systems Programming.
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Week 4 - Monday CS222.
CS111 Computer Programming
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.
File I/O We are used to reading from and writing to the terminal:
CSE 333 – Section 3 POSIX I/O Functions.
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
CSE 333 – Section 3 POSIX I/O Functions.
File I/O (1) Prof. Ikjun Yeom TA – Mugyo
File I/O (1) Prof. Ikjun Yeom TA – Hoyoun
Advanced UNIX progamming
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.
File I/O We are used to reading from and writing to the terminal:
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
Presentation transcript:

CSE 333 – Section 3 POSIX I/O Functions

Important Dates Jan 26th HW1 due Feb 9th HW2 due Feb 12th Midterm (And regular exercises in between) How are exercises and hw going? test suite is good, but write your own unit tests, if you make little progress debugging against test_suite

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

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. NOTE: C-version of fsync() is fflush() POSIX-version of fseek() is lseek()

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); 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]

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]

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.

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>

Read the man pages Be sure you’re reading the correct man page for a specific call. Ex. If you write “man read” you’ll get the shell command rather than the system call [Man man] You can see the system calls are in section 2 [Man 2 read] Here’s the system call read.

Reading a file ... char *buf = ...; int bytes_left = n; #include <errno.h> #include <unistd.h> ... char *buf = ...; int bytes_left = n; 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; So, here’s a short example of reading a file. Demo another example: filedump.c (run on attu) – which essentially reads and prints out the contents of a file Demo with a working file, and with nothing, and with a directory

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 length of named file 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;

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] [man readdir] Now for reading directories we have two options: Readdir and readdir_r (which isn’t shown). Readdir accepts the DIR* as a parameter and will return a struct dirent *, which points to a struct like the one shown below. This struct represents an entry in the directory, (whether it be file or directory) so hence the name, dirent. Please note that in this class we’ll only be concerned about d_name, the directory entry name. Feel free to look up the rest though. Why do we have two ways of doing the same thing? Well readdir is nice if only one thread is reading from a directory. But this is problematic with multiple threads reading from the same directory. readdir_r on the is the thread safe reentrant version of readdir. However, it has buffer overflow problems because of implicit length of user supplied buffer in the second argument. Not too important to know but look up “readdir_r considered harmful” if you’re interested. In this class it’s ok to use the readdir function, and for simplicity we recommend you to use readdir. Clint.py may give a warning for using readdir instead of readdir_r but you can ignore those warnings.

int main(int argc, char** argv) { if (argc != 2) { fprintf(stderr, "Usage: ./dirdump <path>\n"); exit(1); } DIR* dirp = opendir(argv[1]); if (dirp == NULL) { fprintf(stderr, "Could not open directory\n"); struct dirent *entry; entry = readdir(dirp); while (entry) { printf("%s\n", entry->d_name); closedir(dirp); return 0; Demo: dirdump.c. (also prints out hidden file starts with a dot history)

Section Exercise 1 Find a partner if you wish. Write a C program : Given a directory name as an argument, print the entries of the names in that dir to stdout

Section Exercise 2 Write a C program that does the following: Given a command line argument, if it is an ordinary file, print its contents to stdout. If not, or some other error occurs, print an informative error message using perror(). Similar to cat. You must use the POSIX functions to open, close, read and write.