UNIX System Programming

Similar documents
Operating System Labs. Yuanbin Wu

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL.

Standard I/O in C, Computer System and programming in C

File I/0. Advanced Programming in the UNIX Environment

CS240: Programming in C

File I/O. Preprocessor Macros

Operating System Labs. Yuanbin Wu

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

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

Input/Output and the Operating Systems

Files. Eric McCreath

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

CS240: Programming in C

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

Chapter 5, Standard I/O. Not UNIX... C standard (library) Why? UNIX programmed in C stdio is very UNIX based

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

Naked C Lecture 6. File Operations and System Calls

EM108 Software Development for Engineers

CSE 333 SECTION 3. POSIX I/O Functions

CSE 410: Systems Programming

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

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

2009 S2 COMP File Operations

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

CMPE-013/L. File I/O. File Processing. Gabriel Hugh Elkaim Winter File Processing. Files and Streams. Outline.

25.2 Opening and Closing a File

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

File (1A) Young Won Lim 11/25/16

Chapter 10. The UNIX System Interface

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

File IO and command line input CSE 2451

File Handling. Reference:

Goals of this Lecture

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

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

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

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

CSE 333 SECTION 3. POSIX I/O Functions

PROGRAMMAZIONE I A.A. 2017/2018

File I/O. Arash Rafiey. November 7, 2017

C-Refresher: Session 10 Disk IO

System Software Experiment 1 Lecture 7

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

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

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

System Programming. Standard Input/Output Library (Cont d)

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

File Access. FILE * fopen(const char *name, const char * mode);

All the scoring jobs will be done by script

Operating systems. Lecture 7

Input and Output System Calls

Data File and File Handling

CSC 271 Software I: Utilities and Internals

How do we define pointers? Memory allocation. Syntax. Notes. Pointers to variables. Pointers to structures. Pointers to functions. Notes.

Princeton University Computer Science 217: Introduction to Programming Systems. I/O Management

Princeton University. Computer Science 217: Introduction to Programming Systems. I/O Management

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

Fundamentals of Programming. Lecture 10 Hamed Rasifard

UNIX System Calls. Sys Calls versus Library Func

All the scoring jobs will be done by script

Princeton University Computer Science 217: Introduction to Programming Systems I/O Management

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Standard C Library Functions

Input / Output Functions

fopen() fclose() fgetc() fputc() fread() fwrite()

File and Console I/O. CS449 Spring 2016

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

ENG120. Misc. Topics

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

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments

CSE2301. Introduction. Streams and Files. File Access Random Numbers Testing and Debugging. In this part, we introduce

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

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

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

C for Engineers and Scientists: An Interpretive Approach. Chapter 14: File Processing

Computer programming

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

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

UNIT IV-2. The I/O library functions can be classified into two broad categories:

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

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

Introduction to file management

EECS2031. Modifiers. Data Types. Lecture 2 Data types. signed (unsigned) int long int long long int int may be omitted sizeof()

UNIX I/O. Computer Systems: A Programmer's Perspective, Randal E. Bryant and David R. O'Hallaron Prentice Hall, 3 rd edition, 2016, Chapter 10

CSI 402 Lecture 2 Working with Files (Text and Binary)

C PROGRAMMING. Characters and Strings File Processing Exercise

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

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

UNIX System Overview E. Im

Intermediate Programming, Spring 2017*

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

Contents. Programming Assignment 0 review & NOTICE. File IO & File IO exercise. What will be next project?

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Process Management! Goals of this Lecture!

Computer Programming Unit v

Files. Programs and data are stored on disk in structures called files Examples. a.out binary file lab1.c - text file term-paper.

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

Transcription:

File I/O 경희대학교컴퓨터공학과 조진성 UNIX System Programming

File in UNIX n Unified interface for all I/Os in UNIX ü Regular(normal) files in file system ü Special files for devices terminal, keyboard, mouse, tape, hard disk, floppy disk, modem, etc. /dev/* ü Even a socket is similar to a file that is open n System calls for I/O ü open, read, write, close ü lseek, fcntl, ioctl n Predefined open file descriptors ü #include <unistd.h> ü STDIN_FILENO (0) ü STDOUT_FILENO (1) ü STDERR_FILENO (2) UNIX System Programming 1

System Calls for File I/O n Open a file or device ü #include <sys/types.h> ü #include <sys/stat.h> ü #include <fcntl.h> ü int open(char *pathname, int oflag,... /*, mode_t mode */); ü return: a file descriptor if OK, 1 on error ü Options (the second parameter, oflag) O_RDONLY, O_WRONLY, O_RDWR O_APPEND, O_CREAT, O_EXCL, O_TRUNC, O_NONBLOCK, O_SYNC n Close a file descriptor ü #include <unistd.h> ü int close(int fd); ü return: 0 if OK, 1 on error UNIX System Programming 2

System Calls for File I/O (Cont d) n Read from a file descriptor ü #include <unistd.h> ü ssize_t read(int fd, void *buf, size_t nbytes); ü return: number of bytes read if OK, 0 on end of file, 1 on error n Write to a file descriptor ü #include <unistd.h> ü ssize_t write(int fd, void *buf, size_t nbytes); ü return: number of bytes written if OK, 1 on error UNIX System Programming 3

System Calls for File I/O (Cont d) n Reposition read/write file offset ü #include <sys/types.h> ü #include <unistd.h> ü int lseek(int fd, off_t offset, int whence); ü return: new file offset if OK, 1 on error ü The third parameter, whence SEEK_SET(0), SEEK_CUR(1), SEEK_END(2) n Create a new file or device ü #include <sys/types.h> ü #include <sys/stat.h> ü #include <fcntl.h> ü int creat(char *pathname, mode_t mode); ü return: a file descriptor opened for write-only if OK, 1 on error ü Equivalent to open(pathname, O_WRONLY O_CREAT O_TRUNC, mode); UNIX System Programming 4

System Calls for File I/O (Cont d) n Manipulate a file descriptor ü #include <sys/types.h> ü #include <unistd.h> ü #include <fcntl.h> ü int fcntl(int fd, int cmd,... /* int arg */ ); ü return: depends on cmd if OK, 0 on end of file, 1 on error ü Change properties of a file that is already open n Control device ü #include <unistd.h> /* SVR4 */ ü #include <sys/ioctl.h> /* BSD */ ü int ioctl(int fd, int request,...); ü return: depends on request if OK, 1 on error ü Change I/O options for a given file descriptor ü For terminal I/O, socket I/O, tape I/O, disk I/O, file I/O, etc. UNIX System Programming 5

Exercise n Make my own cp program using file I/O system calls $ gcc o mycp mycp.c (or make mycp) $./mycp mycp.c mycp.bak $ ls l mycp.c mycp.bak n Make a big-hole file using creat & lseek system calls $ gcc o hole hole.c (or make hole) $./hole $ ls l file.hole UNIX System Programming 6

Error Handling in UNIX System Calls and Libraries n Global variable extern int errno in <errno.h> #include <stdio.h> #include <string.h> #include <errno.h> main(int argc, char *argv[]) { int fd; if ((fd = open("0123", O_RDONLY)) < 0) { printf("%s: %s\n", argv[0], strerror(errno)); perror(argv[0]); } } UNIX System Programming 7

Streams and FILE Objects n Byte Stream n Logical structure for handling physical files with ease n Predefined open streams ü FILE *stdin; ü FILE *stdout; ü FILE *stderr; UNIX System Programming 8

Basic Four Steps n Declaration of a pointer to FILE type FILE *fp; n Open a stream if ((fp = fopen("/etc/passwd", "rt")) == NULL) { } printf("file open error\n"); exit(1); n Read or Write a stream fputc('a', fp); ch = fgetc(fp); n Close a stream fclose(fp); UNIX System Programming 9

Exercise n List a text file with line numbers $ gcc o list list.c (or make list) $./list list.c UNIX System Programming 10

Opening and Closing a Stream n Open a stream ü FILE *fopen(char *pathname, char *type); ü return: a file pointer if OK, NULL on error ü I/O type (the second parameter, type) access mode: r, w, a, r+, w+, a+ file type: t, b n Close a stream ü int fclose(file *fp); ü return: 0 if OK, EOF on error UNIX System Programming 11

Read and Write a Stream n Character-at-a-time I/O n Line-at-a-time I/O n Direct I/O (Binary I/O) n Formatted I/O UNIX System Programming 12

Character-At-A-Time I/O n Input functions ü int getc(file *fp); ü int fgetc(file *fp); ü all return: next character if OK, EOF on end of file or error n Output functions ü int putc(int ch, FILE *fp); ü int fputc(int ch, FILE *fp); ü all return: ch if OK, EOF on error UNIX System Programming 13

Line-At-A-Time I/O n Input functions ü char *fgets(char *buf, int max, FILE *fp); ü return: buf if OK, NULL on end of file or error n Output functions ü int fputs(char *str, FILE *fp); ü return: non-negative value if OK, EOF on error UNIX System Programming 14

Direct I/O (Binary I/O) n Input functions ü size_t fread(void *ptr, size_t size, size_t nobj, FILE *fp); ü return: number of objects read if OK, EOF on end of file or error n Output functions ü size_t fwrite(void *ptr, size_t size, size_t nobj, FILE *fp); ü return: number of objects written if OK, EOF on end of file or error n Examples struct rec tmp; struct rec item[10]; fread(&tmp, sizeof(struct rec), 1, fp); fwrite(item, sizeof(struct rec), 10, fp); UNIX System Programming 15

Formatted I/O n Input functions ü int fscanf(file *fp, char *format,...); ü int sscanf(char *buf, char *format,...); ü return: number of items assigned if OK, EOF on end of file or error n Output functions ü int fprintf(file *fp, char *format,...); ü int sprintf(char *buf, char *format,...); ü return: number of characters written if OK, negative value on error UNIX System Programming 16

Positioning a Stream n Functions ü #include <unistd.h> ü int fseek(file *fp, long offset, int whence); ü return: 0 if OK, nonzero on error ü The third parameter, whence SEEK_SET(0), SEEK_CUR(1), SEEK_END(2) ü long ftell(file *fp); ü return: current file position if OK, -1L on error ü void rewind(file *fp); ü set file position to the beginning of the file UNIX System Programming 17

Miscellaneous n Functions ü int ungetc(int ch, FILE *fp); ü return: ch if OK, EOF on error ü int ferror(file *fp); ü int feof(file *fp); ü return: nonzero if true, 0 otherwise ü int fflush(file *fp); ü return: 0 if OK, EOF on error ü Flush buffer cache, i.e., write-back to the disk UNIX System Programming 18

Error Handling in File I/O n Error occurrence ü fopen for read à frequently Must check it ü fopen for write à infrequently Don t have to check it, but should check it for consistency ü read functions à infrequently But, must check EOF ü write functions à infrequently Don t have to check it ü fclose à infrequently Don t have to check it UNIX System Programming 19

Exercise n Make a text-copy program using character-at-a-time I/O stream $ gcc o tcp1 tcp1.c (or make tcp1) $./tcp1 tcp1.c tcp1.bak $ ls l tcp1.c tcp1.bak n Make a text-copy program using line-at-a-time I/O stream $ gcc o tcp2 tcp2.c (or make tcp2) $./tcp2 tcp2.c tcp2.bak $ ls l tcp2.c tcp2.bak n Make my own cp program using binary I/O stream $ gcc o mycp2 mycp2.c (or make mycp2) $./mycp2 mycp2.c mycp2.bak $ ls l mycp2.c mycp2.bak UNIX System Programming 20

Exercise (Cont d) n Split a file into two & Merge two files into one using binary I/O stream $ gcc o split split.c (or make split) $ gcc o merge merge.c (or make merge) $./split merge a b $ ls l merge a b $./merge a b merge.new $ chmod a+x merge.new $./merge.new UNIX System Programming 21

Exercise (Cont d) n Another binary I/O example ü Type-in a data file in text format ü Convert a text file into a binary file ü Access the binary file for search, insert, modify, etc. $ gcc o conv conv.c (or make conv) $ gcc o access access.c (or make access) $./conv test.in test.out $ ls l test.out $./access test.out n What does this program do? $ gcc o tab tab.c (or make tab) $./tab *.c UNIX System Programming 22

Summary n System calls in UNIX for file I/O ü open, close, read, write ü lseek, create, fcntl, ioctl n C libraries for file I/O ü fopen, fclose, fread, fwrite ü fgetc, fputc, fgets, fputs ü fscanf, fprintf, sscanf, sprintf ü fseek, ftell, rewind ü ungetc, ferror, feof, fflush UNIX System Programming 23