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

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

Files. Eric McCreath

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

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

UNIX System Calls. Sys Calls versus Library Func

File I/0. Advanced Programming in the UNIX Environment

Operating System Labs. Yuanbin Wu

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

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

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

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

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

CS240: Programming in C

CSC 271 Software I: Utilities and Internals

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

Operating systems. Lecture 7

System Calls and I/O Appendix. Copyright : University of Illinois CS 241 Staff 1

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

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

Operating System Labs. Yuanbin Wu

UNIX System Programming

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

CSE 333 SECTION 3. POSIX I/O Functions

Process Creation in UNIX

CSci 4061 Introduction to Operating Systems. File Systems: Basics

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

OPERATING SYSTEMS: Lesson 2: Operating System Services

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

The bigger picture. File systems. User space operations. What s a file. A file system is the user space implementation of persistent storage.

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

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

CSI 402 Lecture 11 (Unix Discussion on Files continued) 11 1 / 19

File I/O - Filesystems from a user s perspective

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

Recitation 8: Tshlab + VM

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

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

we are here I/O & Storage Layers Recall: C Low level I/O Recall: C Low Level Operations CS162 Operating Systems and Systems Programming Lecture 18

FILE SYSTEMS. Jo, Heeseung

CSE 333 SECTION 3. POSIX I/O Functions

Lecture 21 Systems Programming in C

we are here Page 1 Recall: How do we Hide I/O Latency? I/O & Storage Layers Recall: C Low level I/O

CS 33. Files Part 2. CS33 Intro to Computer Systems XXI 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Goals of this Lecture

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

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

File Systems. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

CSE 410: Systems Programming

Files and the Filesystems. Linux Files

File Descriptors and Piping

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

Input/Output. Input/Output and Files. Input/Output. I/O System Objectives

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

Files and Directories Filesystems from a user s perspective

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

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

UNIX System Overview E. Im

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

Lecture 20. Log into Linux. Copy directory /home/hwang/cs375/lecture20 Project 5 due today. Project 6 posted, due Tuesday, April 8. Questions?

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

Computer Programming Lecture 1 이윤진서울대학교

Process Management! Goals of this Lecture!

Physical Files and Logical Files. Opening Files. Chap 2. Fundamental File Processing Operations. File Structures. Physical file.

Naked C Lecture 6. File Operations and System Calls

Outline. Relationship between file descriptors and open files

UNIX Kernel. UNIX History

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

All the scoring jobs will be done by script

CptS 360 (System Programming) Unit 6: Files and Directories

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

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

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

CS 3013 Operating Systems WPI, A Term Assigned: Friday, August 31, 2007 Due: Monday, September 17, 2007

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

All the scoring jobs will be done by script

First Semester Examination Introduction to Computer Systems (COMP2300/COMP6300)

Contents. NOTICE & Programming Assignment #1. QnA about last exercise. File IO exercise

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

Chapter 10. The UNIX System Interface

File and Directories. Advanced Programming in the UNIX Environment

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

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

System Calls and I/O. CS 241 January 27, Copyright : University of Illinois CS 241 Staff 1

CSCI 4500/8506 Operating Systems Some UNIX System Calls, Library, and PThreads Functions

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

File System. Minsoo Ryu. Real-Time Computing and Communications Lab. Hanyang University.

Lecture 23: System-Level I/O

Input and Output System Calls

ECE 650 Systems Programming & Engineering. Spring 2018

File Systems. Today. Next. Files and directories File & directory implementation Sharing and protection. File system management & examples

Operating System Labs. Yuanbin Wu

Lecture 3: System Calls & API Standards

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Data and File Structures Chapter 2. Basic File Processing Operations

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

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

Memory Mapped I/O. Michael Jantz. Prasad Kulkarni. EECS 678 Memory Mapped I/O Lab 1

CS631 - Advanced Programming in the UNIX Environment

Transcription:

Lecture 5 Lecture files in /home/hwang/cs375/lecture05 on csserver. cp -r /home/hwang/cs375/lecture05. scp -r user@csserver.evansville.edu:/home/hwang/cs375/lecture05. Project 1 posted, due next Thursday Questions? Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 1

Outline Exercises from last lecture Working with files File Descriptors Read, Write, Open and Close File Management File Information Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 2

Working with Files C, C++, Java, etc. each provide some means of file input/output. Each of these languages build upon the lower-level file access routines provided by the OS. The system routines provided by UNIX for file/device I/O are: open, close, read, write, and ioctl. They are documented in section 2 of the man pages, e.g. man 2 write. Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 3

Why Use Low-Level Routines? Normally we use the file access methods provided by our programming language (I/O streams in C++, for example). They are more efficient and portable. BUT, interprocess communication (IPC) requires that we work with the OS routines. Writing kernel code (i.e., device drivers) ALSO requires knowledge of the low-level routines. Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 4

File Descriptors Information about every open file is kept in a system-wide file table. (A file may be opened multiple times with different access modes; there would then be multiple entries in the system file table.) Every process has a file table that contains indexes into the system file table. Indexes into the per-process file table are called file descriptors. Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 5

File Descriptors File Descriptors Per Process File Table 0 1 2 3 4 5 6 System File Table Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 6

File Descriptors Programs usually (automatically) have standard input, standard output, and standard error open. These are associated with file descriptors 0, 1, and 2, respectively. You can associate other file descriptors with other files and devices by using the open system routine. System I/O routines require a file descriptor as an argument in the routine call. Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 7

Example: Byte I/O // File: bytcat.cpp // byte at a time cat from stdin using system calls #include <unistd.h> int main() { char c; ssize_t nread, nwrite; } while ((nread = read(0, &c, sizeof(c)))!= 1) { if (nread == 0) break; // break on end of file if((nwrite = write(1, &c, nread)) == 1) break; // break on write error } if (nread == 1 nwrite == 1) return 1; // error occurred return 0; Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 8

Example: Block I/O // File: blkcat.cpp // block cat from stdin using system calls #include <unistd.h> int main() { const int BLKSZ = 512; char c[blksz]; ssize_t nread, nwrite; } while ((nread = read(0, c, BLKSZ))!= 1) { if (nread == 0) break; // break on end of file if((nwrite = write(1, c, nread)) == 1) break; // break on write error } if (nread == 1 nwrite == 1) return 1; // error occurred return 0; Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 9

Read and Write There is no guarantee that read will read the number of bytes requested. (Fewer bytes will be read when near the end of the file. Also, only a single line of data is read when reading from a terminal, not 512 bytes.) Notice the request is made to write nread bytes and not BLKSZ bytes. write may also write fewer bytes than requested, but this usually can be attributed to more serious problems. Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 10

Open and Close The open call is used to associate a file descriptor with a physical filename. // Required header files #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> // open returns a file descriptor (an int) // Use this form int open(const char *pathname, int flags); // or this one. int open(const char *pathname, int flags, mode_t mode); The close call closes a file descriptor. int close(int fd); Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 11

Open and Close flags should be O_RDONLY, O_WRONLY, or O_RDWR possibly bitwise or'd ( ) with O_CREAT, O_EXCL, O_TRUNC, etc. mode (optional) specifies permissions (see the man page). There is also a creat (not create!) call: int creat(const char *pathname, mode_t mode); // this is equivalent to int open(const char *pathname, O_CREAT O_WRONLY O_TRUNC, mode_t mode); Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 12

Using lseek lseek is used to reposition the file position pointer associated with a file descriptor: #include <sys/types.h> #include <unistd.h> off_t lseek(int fd, off_t offset, int whence); offset is the # of bytes to move. whence is either SEEK_SET, SEEK_CUR, or SEEK_END to move relative to the start of file, the current position, or the end of the file (offset may be negative). Returns offset from the start or -1. Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 13

Duplicating File Descriptors dup and dup2 are used to duplicate a file descriptor. The duplicated descriptor will have the same file pointer in the file table. These calls are useful for interprocess communication. #include <unistd.h> int dup(int oldfd); # use the lowest avail. int dup2(int oldfd, int newfd) # make newfd a copy of oldfd They return the new file descriptor (or -1 on error). Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 14

File Information stat, fstat, and lstat are used to obtain all file information (type, owner, group, permissions, times, # of links, etc): #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> int stat(const char *file_name, struct stat *buf); int fstat(int filedes, struct stat *buf); int lstat(const char *file_name, struct stat *buf); Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 15

File Information stat and lstat differ in regard to symlinks. stat returns info on the file pointed to while lstat returns info on the symlink. fstat acts like stat. Each of the routines require a pointer to a stat structure. The header files define the stat structure and also define several useful macros. Use "man 2 stat" to get the man page. ("man stat" will get the command-line stat.) Refer to the getstat.cpp program. Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 16

File Information There is also a stat command line utility that will display file information: $ stat /etc/passwd File: `/etc/passwd' Size: 32889 Blocks: 72 IO Block: 4096 regular file Device: 801h/2049d Inode: 1712752 Links: 1 Access: (0644/ rw r r ) Uid: (0/root) Gid: (0/root) Access: 2010 09 08 08:17:16.000000000 0500 Modify: 2010 08 27 08:16:42.000000000 0500 Change: 2010 08 27 08:16:42.000000000 0500 Birth: Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 17

Other File Related Routines chmod(path, mode) chown(path,owner,group) unlink(path) link(path1, path2) symlink(path1, path2) mkdir(path, mode) rmdir(path) chdir(path) getcwd(buffer, size) # change file perms # change file owner (root) # delete file, # (decr # of links by 1) # create a hard link to # a file # create a symbolic link # create a new directory # delete directory # change process directory # get current working # directory Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 18

In-class Exercises Modify getstat.cpp so that it also displays the inode #, # of links, and the file size. Reminder: the man page for stat(2) has information about the stat structure and various macros. Further modify getstat.cpp to display file permissions in both octal and symbolic form. Further modify getstat.cpp to display all the information shown by the stat command. Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 19