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

Similar documents
All the scoring jobs will be done by script

All the scoring jobs will be done by script

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

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

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

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

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

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

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

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

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

UNIX System Calls. Sys Calls versus Library Func

Operating System Labs. Yuanbin Wu

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

System-Level I/O Nov 14, 2002

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

Lecture 23: System-Level I/O

A Typical Hardware System The course that gives CMU its Zip! System-Level I/O Nov 14, 2002

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

Operating System Labs. Yuanbin Wu

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

Files and Directories Filesystems from a user s perspective

structs as arguments

System-Level I/O March 31, 2005

CSC 271 Software I: Utilities and Internals

File Input and Output (I/O)

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

39. File and Directories

System- Level I/O. CS 485 Systems Programming Fall Instructor: James Griffioen

System-Level I/O. William J. Taffe Plymouth State University. Using the Slides of Randall E. Bryant Carnegie Mellon University

Introduction to Computer Systems , fall th Lecture, Oct. 19 th

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

Design Choices 2 / 29

CS , Spring Sample Exam 3

Advanced Systems Security: Ordinary Operating Systems

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

System-Level I/O : Introduction to Computer Systems 16 th Lecture, March 23, Instructors: Franz Franchetti & Seth Copen Goldstein

File System (FS) Highlights

17: Filesystem Examples: CD-ROM, MS-DOS, Unix

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

Foundations of Computer Systems

Q & A (1) Where were string literals stored? Virtual Address. SSE2033: System Software Experiment 2 Spring 2016 Jin-Soo Kim

CSci 4061 Introduction to Operating Systems. File Systems: Basics

Automated Test Generation in System-Level

Unix programming interface for file I/O operations and pipes

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

System Calls. Library Functions Vs. System Calls. Library Functions Vs. System Calls

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

Chapter 4 - Files and Directories. Information about files and directories Management of files and directories

Operating System Labs. Yuanbin Wu

OPERATING SYSTEMS: Lesson 2: Operating System Services

Memento: Time Travel for the Web

CSE 333 SECTION 3. POSIX I/O Functions

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

CS631 - Advanced Programming in the UNIX Environment

ELEC-C7310 Sovellusohjelmointi Lecture 3: Filesystem

System- Level I/O /18-243: Introduc3on to Computer Systems 16 th Lecture, Mar. 15, Instructors: Gregory Kesden and Anthony Rowe

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

Linux Forensics. Newbug Tseng Oct

UNIX System Programming

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

Lecture 21 Systems Programming in C

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Files and Directories Filesystems from a user s perspective

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

HEC POSIX I/O API Extensions Rob Ross Mathematics and Computer Science Division Argonne National Laboratory

Master Calcul Scientifique - Mise à niveau en Informatique Written exam : 3 hours

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

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

Thesis, antithesis, synthesis

CSE 333 SECTION 3. POSIX I/O Functions

Preview. lseek System Calls. A File Copy 9/18/2017. An opened file offset can be explicitly positioned by calling lseek system call.

File Types in Unix. Regular files which include text files (formatted) and binary (unformatted)

Files and Directories

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

The link() System Call. Preview. The link() System Call. The link() System Call. The unlink() System Call 9/25/2017

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

INTRODUCTION TO UNIX FILE SYSTEM:

Naked C Lecture 6. File Operations and System Calls

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

프로세스간통신 (Interprocess communication) i 숙명여대창병모

File Systems. CS 450: Operating Systems Sean Wallace Computer Science. Science

Computer Science & Engineering Department I. I. T. Kharagpur

Operating Systems. Processes

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

Operating System Labs. Yuanbin Wu

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

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

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

File I/0. Advanced Programming in the UNIX Environment

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

CSE 410: Systems Programming

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

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

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

File Systems. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

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

Interlude: Files and Directories

Transcription:

File I/O Examples Prof. Jin-Soo Kim(jinsookim@skku.edu) TA - Dong-Yun Lee(dylee@csl.skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu

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

Notice We will use sys server for assignment Both source code and report You can directly check your score via server You can change the final version of your code Be careful that your final version is right version There should be some restriction at server Maximum number of upload? etc 3

Programming Assignment #1 Let s check together PA #1 Some news.. 1. Input filename is *.txt (bible name is without.txt ) ex) genesis.txt, daniel.txt, etc.. 2. GNU make tools Your code should be compiled with the following cmd. $ make all 3. IndexPrinter : print the word with lowercase ex) the (O) The (X) 4

Report Report format : [ 학번 ].pdf I like simple, neat, clean, graphical, understandable, easy, and short report About your own data structure and functions 5

QnA For PA #1 6

Exercise Review (1) const qualifier void foo1(const char *s1) { s1[0] = a ; } void foo2(const char *s1) { s1 = hey ; } void foo3(const char *s1) { char *s = s1; } void foo4(const char *s1) { char *s = s1; s[0] = e ; } void foo5 (char * const s1) { s1[0] = a ; } void foo6(char * const s1) { s1 = hey ; } 7

Exercise Review (2) 8

Exercise Review (3) Why below code do not work correctly? Explain it correctly You can use anything for the answer int main(void) { char c; } while(read(0, &c, 1)!= 0) write(1, &c, 1); exit(0); 9

Remind 6 System calls open() close() read() write() lseek() stat() / fstat() 10

Example #1 (1) char filename[] = "hello-dos.txt"; int fd; char buffer[16]; off_t pos = 0; // long long; fd = open(filename, O_RDWR O_CREAT, 0755); read(fd, buffer, 6); read(fd, buffer+6, 2); File state (FD: 3) path: "hello-dos.txt" position: 0 size: 20 lseek(fd, -2, SEEK_CUR); buffer[0] = '\n'; write(fd, buffer, 1); lseek(fd, 8, SEEK_SET); strcpy(buffer, "How"); write(fd, buffer, 3); close(fd); H e l l o. \r \n W h o a r e y o u? EOF fd = open(filename, O_WRONLY O_CREAT O_EXCL, 0755); if (fd < 0) printf("errno : %d, error code - EEXIST : %d\n", errno, EEXIST); 11

Example #1 (2) char filename[] = "hello-dos.txt"; int fd; char buffer[16]; off_t pos = 0; // long long; fd = open(filename, O_RDWR O_CREAT, 0755); read(fd, buffer, 6); // "Hello." read(fd, buffer+6, 2); File state (FD: 3) path: "hello-dos.txt" position: 6 size: 20 lseek(fd, -2, SEEK_CUR); buffer[0] = '\n'; write(fd, buffer, 1); lseek(fd, 8, SEEK_SET); strcpy(buffer, "How"); write(fd, buffer, 3); close(fd); H e l l o. \r \n W h o a r e y o u? EOF fd = open(filename, O_WRONLY O_CREAT O_EXCL, 0755); if (fd < 0) printf("errno : %d, error code - EEXIST : %d\n", errno, EEXIST); 12

Example #1 (3) char filename[] = "hello-dos.txt"; int fd; char buffer[16]; off_t pos = 0; // long long; fd = open(filename, O_RDWR O_CREAT, 0755); read(fd, buffer, 6); read(fd, buffer+6, 2); // "Hello.\r\n" File state (FD: 3) path: "hello-dos.txt" position: 8 size: 20 lseek(fd, -2, SEEK_CUR); buffer[0] = '\n'; write(fd, buffer, 1); lseek(fd, 8, SEEK_SET); strcpy(buffer, "How"); write(fd, buffer, 3); close(fd); H e l l o. \r \n W h o a r e y o u? EOF fd = open(filename, O_WRONLY O_CREAT O_EXCL, 0755); if (fd < 0) printf("errno : %d, error code - EEXIST : %d\n", errno, EEXIST); 13

Example #1 (4) char filename[] = "hello-dos.txt"; int fd; char buffer[16]; off_t pos = 0; // long long; fd = open(filename, O_RDWR O_CREAT, 0755); read(fd, buffer, 6); read(fd, buffer+6, 2); File state (FD: 3) path: "hello-dos.txt" position: 6 size: 20 lseek(fd, -2, SEEK_CUR); buffer[0] = '\n'; write(fd, buffer, 1); lseek(fd, 8, SEEK_SET); strcpy(buffer, "How"); write(fd, buffer, 3); close(fd); H e l l o. \r \n W h o a r e y o u? EOF fd = open(filename, O_WRONLY O_CREAT O_EXCL, 0755); if (fd < 0) printf("errno : %d, error code - EEXIST : %d\n", errno, EEXIST); 14

Example #1 (5) char filename[] = "hello-dos.txt"; int fd; char buffer[16]; off_t pos = 0; // long long; fd = open(filename, O_RDWR O_CREAT, 0755); read(fd, buffer, 6); read(fd, buffer+6, 2); File state (FD: 3) path: "hello-dos.txt" position: 7 size: 20 lseek(fd, -2, SEEK_CUR); buffer[0] = '\n'; write(fd, buffer, 1); lseek(fd, 8, SEEK_SET); strcpy(buffer, "How"); write(fd, buffer, 3); close(fd); H e l l o. \n \n W h o a r e y o u? EOF fd = open(filename, O_WRONLY O_CREAT O_EXCL, 0755); if (fd < 0) printf("errno : %d, error code - EEXIST : %d\n", errno, EEXIST); 15

Example #1 (6) char filename[] = "hello-dos.txt"; int fd; char buffer[16]; off_t pos = 0; // long long; fd = open(filename, O_RDWR O_CREAT, 0755); read(fd, buffer, 6); read(fd, buffer+6, 2); File state (FD: 3) path: "hello-dos.txt" position: 8 size: 20 lseek(fd, -2, SEEK_CUR); buffer[0] = '\n'; write(fd, buffer, 1); lseek(fd, 8, SEEK_SET); strcpy(buffer, "How"); write(fd, buffer, 3); close(fd); H e l l o. \n \n W h o a r e y o u? EOF fd = open(filename, O_WRONLY O_CREAT O_EXCL, 0755); if (fd < 0) printf("errno : %d, error code - EEXIST : %d\n", errno, EEXIST); 16

Example #1 (7) char filename[] = "hello-dos.txt"; int fd; char buffer[16]; off_t pos = 0; // long long; fd = open(filename, O_RDWR O_CREAT, 0755); read(fd, buffer, 6); read(fd, buffer+6, 2); File state (FD: 3) path: "hello-dos.txt" position: 11 size: 20 lseek(fd, -2, SEEK_CUR); buffer[0] = '\n'; write(fd, buffer, 1); lseek(fd, 8, SEEK_SET); strcpy(buffer, "How"); write(fd, buffer, 3); close(fd); H e l l o. \n \n H o w a r e y o u? EOF fd = open(filename, O_WRONLY O_CREAT O_EXCL, 0755); if (fd < 0) printf("errno : %d, error code - EEXIST : %d\n", errno, EEXIST); 17

Example #1 (8) char filename[] = "hello-dos.txt"; int fd; char buffer[16]; off_t pos = 0; // long long; File state (FD: 3) :CLOSED fd = open(filename, O_RDWR O_CREAT, 0755); read(fd, buffer, 6); read(fd, buffer+6, 2); lseek(fd, -2, SEEK_CUR); buffer[0] = '\n'; write(fd, buffer, 1); lseek(fd, 8, SEEK_SET); strcpy(buffer, "How"); write(fd, buffer, 3); close(fd); fd = open(filename, O_WRONLY O_CREAT O_EXCL, 0755); if (fd < 0) printf("errno : %d, error code - EEXIST : %d\n", errno, EEXIST); 18

Example #1 (9) char filename[] = "hello-dos.txt"; int fd; char buffer[16]; off_t pos = 0; // long long; fd = open(filename, O_RDWR O_CREAT, 0755); read(fd, buffer, 6); read(fd, buffer+6, 2); lseek(fd, -2, SEEK_CUR); buffer[0] = '\n'; write(fd, buffer, 1); lseek(fd, 8, SEEK_SET); strcpy(buffer, "How"); write(fd, buffer, 3); close(fd); fd = open(filename, O_WRONLY O_CREAT O_EXCL, 0755); if (fd < 0) printf("errno : %d, error code - EEXIST : %d\n", errno, EEXIST); 19

Example #2 (1) File state (FD: 3) char filename[] = "hello-dos.txt"; int fd; char buffer[16]; off_t pos = 0; fd = open(filename, O_WRONLY); lseek(fd, 1, SEEK_END); strcpy(buffer, "!\n"); write(fd, buffer, 2); close(fd); path: "hello-dos.txt" type: Regular mode: Write only position: 0 size: 20 H e l l o. \r \n W h o a r e y o u? EOF 20

Example #2 (2) File state (FD: 3) char filename[] = "hello-dos.txt"; int fd; char buffer[16]; off_t pos = 0; fd = open(filename, O_WRONLY); lseek(fd, 1, SEEK_END); strcpy(buffer, "!\n"); write(fd, buffer, 2); close(fd); path: "hello-dos.txt" type: Regular mode: Write only position: 21 size: 20 H e l l o. \r \n W h o a r e y o u? EOF 21

Example #2 (3) File state (FD: 3) char filename[] = "hello-dos.txt"; int fd; char buffer[16]; off_t pos = 0; fd = open(filename, O_WRONLY); lseek(fd, 1, SEEK_END); strcpy(buffer, "!\n"); write(fd, buffer, 2); close(fd); path: "hello-dos.txt" type: Regular mode: Write only position: 23 size: 23 H e l l o. \r \n W h o a r e y o u? \0! \n EOF Data hole 22

Example #2 (4) char filename[] = "hello-dos.txt"; int fd; char buffer[16]; off_t pos = 0; File state (FD: 3) :CLOSED fd = open(filename, O_WRONLY); lseek(fd, 1, SEEK_END); strcpy(buffer, "!\n"); write(fd, buffer, 2); close(fd); 23

File Metadata Data about data, in this case file data. Maintained by kernel, accessed by users with the stat and fstat functions. /* Metadata returned by the stat and fstat functions */ struct stat { dev_t st_dev; /* device */ ino_t st_ino; /* inode */ mode_t st_mode; /* protection and file type */ nlink_t st_nlink; /* number of hard links */ uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ dev_t st_rdev; /* device type (if inode device) */ off_t st_size; /* total size, in bytes */ unsigned long st_blksize; /* blocksize for filesystem I/O */ unsigned long st_blocks; /* number of blocks allocated */ time_t st_atime; /* time of last file access */ time_t st_mtime; /* time of last file modification */ time_t st_ctime; /* time of last inode change */ }; /* statbuf.h included by sys/stat.h */ 24

Accessing File Metadata /* statcheck.c - Querying and manipulating a file s meta data */ int main (int argc, char **argv) { struct stat st; char *type, *readok; stat(argv[1], &st); if (S_ISREG(st.st_mode)) /* file type */ type = "regular"; else if (S_ISDIR(st.st_mode)) type = "directory"; else type = "other"; if ((st.st_mode & S_IRUSR)) /* OK to read? */ readok = "yes"; else readok = "no"; bass>./statcheck statcheck.c type: regular, read: yes bass> chmod 000 statcheck.c bass>./statcheck statcheck.c type: regular, read: no } printf("type: %s, read: %s\n", type, readok); exit(0); 25

Lab 2. Exercise (1) Make the following program Unix Dos Newline <LF> <CR> <LF> <TAB> char 8 consecutive <space> -> <TAB> <TAB> -> 4 consecutive <space> If input file is Unix format, convert it to the dosformat If input file is Dos format, convert it to the Unixformat 26

Lab 2. Exercise (2) Make the following program Unix Dos Newline <LF> <CR> <LF> <TAB> char 8 consecutive <space> -> <TAB> <TAB> -> 4 consecutive <space> 27

Lab 2. Exercise (3) Make the following program Unix Dos Newline <LF> <CR> <LF> <TAB> char 8 consecutive <space> -> <TAB> <TAB> -> 4 consecutive <space>./convert [input_file] You should name the output file as [input_file].out You cannot use fopen, fread, fwrite and fclose Instead, use open, read, write and close Test file is uploaded at project page http://csl.skku.edu/sse2033s16/projects 28