System Programming Chapter 3 File Programming September 8

System Programming Chapter 3. File Programming September 8, 2017 Jongmoo Choi Dept. of Software Dankook University choijm@dankook. ac. kr http: //embedded. dankook. ac. kr/~choijm

Chapter Objectives Understand disk geometry Discuss system programs for disk (storage) Apprehend the internal structure of a file Learn how to use system calls related to a file Make a program (command) that manipulates a file Refer to Chapter 10 in the main textbook 2

Introduction Issues on file ü ü ü File manipulation (create, access, remove, …) Associate a file name with actual data stored in disk Manage file attributes/access control Support hierarchy structure (directory) Support a variety of file types (e. g. device) File related system calls ü ü ü ü open(), creat(): create a file, start accessing a file (authentication) read(), write(): read/write bytes from/to a file close(): finish accessing a file lseek(): jump to a particular offset (location) in a file unlink(), remove() : delete a file fcntl() : control a file (file descriptor) … 3

Disk structure (1/3) Components ü ü ü Platter, Spindle, Surface Track, Sector, Cylinder Head, ARM 4

Disk structure (2/3) Disk access ü Sector addressing : head(surface), track(cylinder), sector ü Seek time: move head to appropriate track Rotational latency: wait for the sector to appear under the head Transmission time: read/write the request sector(s) ü ü ü Try to reduce the Seek time and Rotational latency Make use of various disk scheduling (eg. SCAN or elevator algorithm) and Parallel access techniques (RAID) 5

Disk structure (3/3, Optional) Disk vs. Flash memory VS ü ü ü No mechanical part (fast, lightweight) Overwrite limitation (erase before write) Read/Write vs. Erase granularity Endurance, Disturbance, Retention error SLC, MLC, TLC 6

System programs for Disk (1/7) Disk device driver ü ü Abstract disk as a logical disk (a collection of disk blocks) The size of a disk block is the same as that of page frame (4 or 8 KB) Disk command handling (ATA command: type, start, size, device, …) Disk initialization, scheduling, error handling, … 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 . . Disk device driver 7

System programs for Disk (2/7) File system ü ü ü Support file abstraction: stream of bytes Associate a file with disk blocks (inode, FAT) Support file attribute/access control, directory, … start offset size Reports. doc 10000 byte File system 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1, 5, 6 디스크 블록들이 아니라 13, 14, 15 디스크 블록들을 할당하면? . . Disk device driver 8

System programs for Disk (3/7) File system ü inode concept § An object for managing a file in a file system (metadata) § Used by various file systems such as UFS, FFS, Ext 2/3/4, LFS, … § Maintain information for a file (e. g. “ls –l”) • • • file size locations of disk blocks for a file owner, access permission time information file type: regular, directory, device, pipe, socket, … § Stored in disk § Constructed when a file is created inode Disk 69 6 e 74 20. . . (from chapter 1) 9

System programs for Disk (4/7) File system ü inode structure inode type (4 bit) u g s r w x i_inode_number i_mode i_nlink, i_dev i_uid, gid i_op, i_size i_atime, ctime, mtime 7 13 24 31 55 12 direct block …. 72 96 3 indirect block 123 10 67 77 83 125 99 128 131 S_IFSOCK S_IFLNK S_IFREG S_IFBLK S_IFDIR S_IFCHR S_IFIFO

System programs for Disk (5/7) File system ü inode example § When we create a new file, named “alphabet. txt”, whose contents include “AB…Z”. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 . . When we write more data? (when a file is increased? ) For instance, it becomes 5 KB, 50 KB or 100 KB? 11 type : regular size: 26 date, time. . . owner, group access bits locations : 10 _ _ _ ____ ABCDEFG. . . XYZ

System programs for Disk (6/7) System call ü Support interfaces such as open(), read(), write(), close(), … fd=open(“Reports. doc”, …) read(fd, buf, size) or write(fd, buf, size) close(fd) System call Reports. doc 10000 byte File system 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 . . Disk device driver 12

System programs for Disk (7/7) System call ü Use fd (file descriptor) instead of file name (for efficiency) § § fd: object to point out a file in kernel return value of the open() system call used by the following read(), write(), …, close() system calls fd is connected into inode through various kernel objects (file table) program file_descriptor … fd=open() ; . . . file structure (file table) offset 13 inode

Layered Architecture for Abstraction Revisit Chapter 1 application program library system call file system device driver device itself 14

File Programming (1/15) Practice 1: read data from an existing file /* file_test 1. c: read data from a file, by choijm@dku. edu*/ #include <stdio. h> #include <stdlib. h> #include <unistd. h> #include <fcntl. h> Refer to next slide (Syntax) #include <errno. h> #define MAX_BUF 16 char fname[] = “alphabet. txt"; int main() { int fd, size; char buf[MAX_BUF]; } Inform the cause when an error occurs cf) Error handling is quite important!! fd = open(fname, O_RDONLY); if (fd < 0) { printf(“Can’t open %s file with errno %dn”, fname, errno); exit(-1); } size = read(fd, buf, MAX_BUF); if (size < 0) { printf(“Can’t read from file %s, size = %dn”, fname, size); exit(-1); } else printf(“size of read data is %dn”, size); close(fd); 15

File Programming (2/15) Syntax of the open() and read() system call int open(const char *pathname, int flags, [mode_t mode]) ü pathname : absolute path or relative path ü flags (see: /usr/include/asm/fcntl. h) § O_RDONLY, O_WRONLY, O_RDWR § O_CREAT, O_EXCL § O_TRUNC, O_APPEND § O_NONBLOCK, O_SYNC § … ü mode § meaningful with the O_CREAT flag § file access mode (S_IRUSR, S_IWUSR, S_IXUSR, S_IRGRP, …, S_IROTH, …) ü return value § file descriptor if success § -1 if fail int read(int fd, char *buf, int size) // same as the write(fd, buf, size) ü fd: file descriptor (return value of open()) ü buf: memory space for keeping data ü size: request size ü return value § read size § -1 if fail 16

File Programming (3/15) Practice 1: execution results /usr/include/asm-generic/errno-base. h #define ENOENT 2 // No such file or directory 17

File Programming (4/15) Practice 2: extend the practice 1 so that it displays the read data on terminal /* file_test 1_ext. c: read data from a file and display them, by choijm@dku. edu*/ #include <stdio. h> #include <stdlib. h> #include <unistd. h> #include <fcntl. h> #include <errno. h> #define MAX_BUF 16 char fname[] = “alphabet. txt"; int main() { int fd, read_size, write_size; char buf[MAX_BUF]; fd = open(fname, O_RDONLY); if (fd < 0) { printf(“Can’t open %s file with errno %dn”, fname, errno); exit(-1); } read_size = read(fd, buf, MAX_BUF); // Due to the slide limit, I omit the error handling code (But, students must implement it)

File Programming (5/15) Practice 2: execution results Can we make the “cat” command? (or “more” command? ) 19

File Programming (6/15) Practice 3: make a “mycat” command (with argc, argv) /* mycat program, by choijm@dku. edu */ #include <stdio. h> #include <stdlib. h> #include <unistd. h> #include <fcntl. h> Command Convention #include <errno. h> #define MAX_BUF 64 int main(int argc, char *argv[]) { int fd, read_size, write_size; char buf[MAX_BUF]; } if (argc != 2) { printf("USAGE: %s file_namen", argv[0]); exit(-1); } fd = open(argv[1], O_RDONLY); if (fd < 0) { // open error handling } while (1) { read_size = read(fd, buf, MAX_BUF); if (read_size == 0) break; write_size = write(STDOUT_FILENO, buf, read_size); } close(fd); 20

File Programming (7/15) Practice 3: execution results 21

File Programming (8/15) Practice 4: create a new file /* file_create. c: create a new file, by choijm@dku. edu */ #include <stdio. h> #include <stdlib. h> #include <unistd. h> #include <fcntl. h> If we rerun this program? #include <errno. h> #define MAX_BUF 64 char fname[] = "newfile. txt"; char dummy_data[]="abcdefgn"; If we rerun without the O_EXCL flag? int main() { O_CREAT or creat() int fd, write_size, read_size; char buf[MAX_BUF]; If we want to write data at the end of this file? fd = open(fname, O_RDWR | O_CREAT | O_EXCL, 0664); if (fd < 0) { printf("Can't create %s file with errno %dn", fname, errno); exit(1); } write_size = write(fd, dummy_data, sizeof(dummy_data)); printf("write_size = %dn", write_size); close(fd); fd = open(fname, O_RDONLY); read_size = read(fd, buf, MAX_BUF); printf("read_size = %dn", read_size); write_size = write(STDOUT_FILENO, buf, read_size); close(fd); } 22 If we comment out these close() and open() statements?

File Programming (9/15) Practice 4: execution results 23

File Programming (10/15) Practice 5: want to read “d” from a file whose contents are “abcdefg” /* file_lseek. c: lseek example, by choijm@dku. edu */ #include <stdio. h> #include <stdlib. h> #include <unistd. h> Refer #include <fcntl. h> #include <errno. h> #define MAX_BUF 64 char fname[] = "newfile_lseek. txt"; char dummy_data[]="abcdefgn"; to next slide int main() { int fd, write_size, read_size, new_offset; char buf[MAX_BUF]; fd = open(fname, O_RDWR | O_CREAT | O_EXCL, 0664); write_size = write(fd, dummy_data, sizeof(dummy_data)); printf("write_size = %dn", write_size); close(fd); fd = open(fname, O_RDONLY); new_offset = lseek(fd, 3, SEEK_SET); read_size = read(fd, buf, MAX_BUF); printf("read_size = %dn", read_size); write_size = write(STDOUT_FILENO, buf, read_size); close(fd); } 24

File Programming (11/15) Syntax of the lseek() system call off_t lseek(int fd, off_t offset, int whence) ü fd : file descriptor ü offset : offset position ü whence (/usr/include/unistd. h) § SEEK_SET : New offset is set to offset bytes. § SEEK_CUR: New offset is set to its current location plus offset bytes. § SEEK_END: New offset is set to the size of the file plus offset bytes ü return value § new offset if success § -1 if fail Negative value is allowed Can be larger than the file size 25

File Programming (12/15) Practice 6: device file test. txt /* file_device. c, by choijm@dku. edu */ #include <unistd. h> #include <sys/types. h> #include <sys/stat. h> #include <fcntl. h> #define MAX_BUF 4 char fname[] = "test. txt"; char tmp_data[] = "abcdefghijklmn"; /dev/pts/2 abcd ef… inode int main() { int fd, size; char buf[MAX_BUF]; fd = open(fname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); write(fd, tmp_data, sizeof(tmp_data)); close(fd); fd = open(fname, O_RDONLY); lseek(fd, 5, SEEK_SET); size = read(fd, buf, MAX_BUF); close(fd); } Devices such as terminal can be accessed using file interfaces fd=open(“/dev/pts/2”, O_WRONLY); write(fd, buf, MAX_BUF); close(fd); 26

File Programming (13/15, Optional) Practice 7: redirection (derived from “mycat” program) ü Same fd but different objects /* file_redirection. c, by choijm@dku. edu */ #include <stdio. h> #include <stdlib. h> #include <unistd. h> #include <fcntl. h> #include <errno. h> #define MAX_BUF 64 int main(int argc, char *argv[]) { int fd, fd 1, read_size, write_size; char buf[MAX_BUF]; if (argc != 4) { printf("USAGE: %s input_file_name ”>” output_file_namen", argv[0]); exit(-1); } fd = open(argv[1], O_RDONLY); // for redirection. (eg. "mycat inputfile. txt > outputfile. txt“) // close(STDOUT_FILENO); fd 1 = open(argv[3], O_RDWR | O_CREAT, 0641); dup 2(fd 1, STDOUT_FILENO); // redirection end } while (1) { read_size = read(fd, buf, MAX_BUF); if (read_size == 0) break; write_size = write(STDOUT_FILENO, buf, read_size); } close(fd); 27

File Programming (14/15, Optional) Practice 7: execution results This is just an example. In general, redirection is in the form of “. /redirection sourcefile. txt > outputfile. txt” (shell actually handle the redirection code) 28

File Programming (15/15) Other system calls related to file ü ü ü ü ü link(), unlink() dup(), dup 2() pipe() mknod() stat(), fstat() (Source: CSAPP, Figure 10. 1 ) mkdir(), readdir(), rmdir() sync(), fsync() ioctl(), fcntl() chmod(), fchmod() creat() // same as open() with flag O_WRONLY | O_CREAT | O_TRUNC Discuss the tradeoff about the buffer size in read() and write(). 29

Tracing system call Using “strace” (Source: Operating Systems: Three Easy Pieces) 30

File system internals File descriptors after redirect (Source: CSAPP, Figure 10. 12 and Figure 10. 15 ) 31

Summary Understand the internal structure of disk Find out the relation between system programs for disk ü Driver, file system, system call Grasp the role of the inode Make a program with file interfaces ü ü ü open, read, write, close lseek device file and redirection Question: How can we figure out the size of a file using file interface? (3 ways) Homework 3: Make a command called “mycp” Requirements - use argc and argv[ ] - do not create a file if the same name already exists in current directory - shows student’s ID and date (using whoami and date) - hand out the report that includes a snapshot, source code and discussion Bonus: copy not only the contents but also the attributes 32

Homework 3: Snapshot example 33
- Slides: 33