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

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

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

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

UNIX System Calls. Sys Calls versus Library Func

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

All the scoring jobs will be done by script

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

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

CSci 4061 Introduction to Operating Systems. File Systems: Basics

read(2) There can be several cases where read returns less than the number of bytes requested:

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

All the scoring jobs will be done by script

Files and Directories Filesystems from a user s perspective

File I/0. Advanced Programming in the UNIX Environment

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

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

Operating System Labs. Yuanbin Wu

OPERATING SYSTEMS: Lesson 2: Operating System Services

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

Input and Output System Calls

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

Lecture 23: System-Level I/O

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

CSC 271 Software I: Utilities and Internals

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

UNIX SYSTEM PROGRAMMING

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

MMAP AND PIPE. UNIX Programming 2015 Fall by Euiseong Seo

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

CS631 - Advanced Programming in the UNIX Environment

File System (FS) Highlights

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

Advanced Unix Programming Module 06 Raju Alluri spurthi.com

39. File and Directories

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

Operating System Labs. Yuanbin Wu

ELEC-C7310 Sovellusohjelmointi Lecture 3: Filesystem

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

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

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

Programmation Systèmes Cours 8 Synchronization & File Locking

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

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

ISA 563: Fundamentals of Systems Programming

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

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

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

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

Files and Directories Filesystems from a user s perspective

UNIX System Overview E. Im

Files and Directories

Outline. Relationship between file descriptors and open files

File Input and Output (I/O)

Lecture 21 Systems Programming in C

CS , Spring Sample Exam 3

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

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

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

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

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

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

FILE SYSTEMS. Jo, Heeseung

Design Choices 2 / 29

ADVANCED I/O. ISA 563: Fundamentals of Systems Programming

UNIX System Programming

Advanced Systems Security: Ordinary Operating Systems

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

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

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

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

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

Files and Directories

CS631 - Advanced Programming in the UNIX Environment. Dæmon processes, System Logging, Advanced I/O

UNIX FILESYSTEM STRUCTURE BASICS By Mark E. Donaldson

Operating System Labs. Yuanbin Wu

ADVANCED OPERATING SYSTEMS UNIT 2 FILE AND DIRECTORY I/O BY MR.PRASAD SAWANT

CSCI 4061: Files, Directories, Standard I/O

System-Level I/O March 31, 2005

System-Level I/O Nov 14, 2002

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

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

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

File Systems. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

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

Files and Directories

Foundations of Computer Systems

Thesis, antithesis, synthesis

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

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

Automated Test Generation in System-Level

UNIT I INTRODUCTION TO UNIX & FILE SYSTEM

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

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

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

Naked C Lecture 6. File Operations and System Calls

File I/O - Filesystems from a user s perspective

Linux Forensics. Newbug Tseng Oct

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

Transcription:

UNIX Directory Organization UNIX directories are simple (generally ASCII) les which maain a name to inode mapping which is convenient for people to use. All le objects are represented by one or more names located in a directory and poing to the controlling inode for the named object. Each controlling inode keeps a link count which is incremented each time a new name is placed in a directory to po to the inode, and decremented each time a name is removed from a directory (by anrm command). When the link count on an inode reaches zero, the node is returned to the free I-list and its data blocks (if any exist) are returned to the free data block list. If you are erested in the inode which corresponds to a lename, use the -i option to the ls command: %ls -lai 74116 drwxr-xr-x 2 bill 1024 Oct 17 21:49. 38912 drwx--x--x 26 bill 54784 Nov 14 18:39.. 74118 -rw-r--r-- 1 bill 2714 May 10 1996 client.c 73822 -rw-r--r-- 1 bill 3176 Aug 16 12:35 client_bsd.c 74149 -rw-r--r-- 2 bill-- 2935 May 12 1996 client_sys5.c 74173 -rw-r--r-- 1 bill 2775 May 10 1996 clientbsd.c 74021 -rw-r--r-- 2 bill- 3506 Aug 18 17:01 nc_test_bsd.c 74021 -rw-r--r-- 2 bill_ 3506 Aug 18 17:01 nc_test_bsd_sun.c 73986 -rw-r--r-- 1 bill 3494 Aug 18 14:36 new_test_client.c 74149 -rw-r--r-- 2 bill 2935 May 12 1996 newclient.c 74148 -rw-r--r-- 1 bill 3961 May 12 1996 server.c 28

UNIX Directory Organization / sbin tmp lib usr etc dev mount ifconfig sh hostname /. 2.. 2 sbin 5 etc 12 usr 63 etc... libc.a libm.a libl.a libmp.a /etc etc d ucb. 12.. 2 e 76 k 143 moo 76 sm 55 etc... c sos xxx /etc/sm bin. 55.. 12 kit 76 top 1033 a b abc iso /usr yyy zzz. 2.. 2 etc 12 ucb 44 bin 165 abc 432 etc... e k moo sm kit top /usr/abc. 2.. 2 sos 44 iso 63 zzz 721 g etc... etc... 29

Basic IO The basic IO system calls include: Create a file #include <sys/types.h> #include <sys/stat.h> creat (path, mode) char * path; mode; path mode Address of a pathname Protection mode of the new file returns: a channel number or -1 x; if((x = creat("myfile", 0666)) == -1){ perror("creat failed"); 30

Basic IO (Cont'd) Remove a file object from a directory #include <unistd.h> unlink (path) char * path; path Address of a pathname returns: 0 on success or -1 x; if((x = unlink("myfile")) == -1){ perror("unlink failed"); 31

Basic IO (Cont'd) Open or create and open a file #include <fcntl.h> open (path, open_flag, protection_mode) char * path; open_flag; protection_mode; path Address of a pathname open_flag Open ent/open behavior flags protection_mode rwx bits, if file is created mode defined constants: O_RDONLY read only O_WRONLY write only O_RDWR read and write O_NDELAY O_NONBLOCK O_APPEND O_CREAT O_TRUNC O_EXCL O_SYNC no block on open no block on open write at EOF create if no file trash bytes in file error if file exists write-through buffer returns: a channel number or -1 32

Basic IO (Cont'd) open() (cont'd) fd; if((fd = open("myfile", O_RDWR O_CREAT, 0666)) == -1){ perror("open failed"); Close a file object close (fildes) fildes; fildes A valid, active file descriptor returns: 0 on success or -1 x; if((x = close(fd)) == -1){ perror("close failed"); 33

Basic IO Kernel Connections Process A Object Blocks mode RD FP 0 LINK 2 I-node Cache LINK 2 Process B mode RD FP 38 LINK 1 mode WR FP 5003 LINK 2 disk inode LINK 1 disk inode Process child of B mode RW FP 900 LINK 2 mode RD FP 0 LINK 1 LINK 1 disk inode LINK 2 mode RW FP 10240 LINK 1 disk inode 34

Basic IO (Cont'd) Write bytes to a file object Read bytes from a file object #include <unistd.h> write (fildes, buffer, nbyte) fildes; char buffer[]; unsigned nbyte; read (fildes, buffer, nbyte) fildes; char buffer[]; unsigned nbyte; fildes buffer nbyte An active, valid file descriptor. User data buffer. Size (bytes) of the read/write request. returns: number of bytes read/written or -1 35

Basic IO (Cont'd) read(), write() (cont'd) fd, ret, numbytes=256; char *buf[256]; if((fd = open("myfile", O_RDWR, 0)) == -1){ perror("open failed"); if((ret = write(fd, buf, numbytes)) == -1){ perror("write failed"); if((ret = read(fd, buf, numbytes)) == -1){ perror("read failed"); 36

Seek a position in a file #include <sys/file.h> #include <sys/types.h> #include <unistd.h> Basic IO (Cont'd) off_t off_t lseek (fildes, offset, whence) fildes; offset; whence; fildes is the poer to be changed: a valid, active file descriptor having a current position attribute. offset is the new position of the file poer. whence is one of these three values specifying whether offset is an absolute or incremental address: SEEK_SET Set fildes to offset bytes. SEEK_CUR Set fildes to (fildes + offset) bytes. SEEK_END Set fildes to (sizeof(fildes) + offset) bytes. returns: file position or -1 fd, ret; if((ret = lseek(fd, -45, SEEK_CUR)) == -1){ perror("lseek failed"); prf("file at %d byte location\n", ret); 37

Extended IO Make a file, directory, FIFO or device #include <sys/types.h> #include <sys/stat.h> mknod (path, mode, device) char * path; mode; device; path mode device Address of a pathname Access mode of the new file Device specifier returns: 0 on success or -1 x; union{ devcode; struct{ short major,minor; dev_parts; dev; device.dev_parts.major = 3; device.dev_parts.minor = 1; if((x = mknod("/dev/mydev", S_IFCHRS 0600, dev.devcode)) == -1){ perror("mknod type c failed"); 38

Extended IO (Cont'd) Make a hard link name Make a symbolic link file object #include <unistd.h> link (old_path, new_path) char * old_path; char * new_path; symlink (old_path, new_path) char * old_path; char * new_path; old_path new_path is a pathname to an existing file. is the additional pathname to be assigned to the file. returns: 0 on success or -1 39

Extended IO (Cont'd) link(), symlink() (cont'd) x; if((x = link("myfile", "sub1/myfiletoo")) == -1){ perror("hard link failed"); chdir("sub2"); if((x = symlink("../myfile", "myfiletoo")) == -1){ perror("symbolic link failed"); % ls -li myfile 74148 -rw-r--r-- 2 bill 3961 May 12 1996 myfile % cd sub1; ls -l myfiletoo 74148 -rw-r--r-- 2 bill 3961 May 12 1996 myfiletoo % cd../sub2; ls -l myfiletoo 87655 lrwxrwxrwx 1 bill 8 Sep 14 20:48 myfiletoo ->../myfile 40

Extended IO (Cont'd) Change access mode to a file object #include <sys/types.h> #include <sys/stat.h> chmod (path, mode) char * path; mode; fchmod (fildes, mode) fildes; mode; path fildes mode Address of a pathname File descriptor File's new mode returns: 0 on success or -1 41

Extended IO (Cont'd) chmod(), fchmod() (cont'd) x, fd; if((x = chmod("myfile", 0666)) == -1){ perror("chmod failed"); if((fd = open("myfile", O_RDONLY, 0)) == -1){ perror("open failed"); if((x = fchmod(fd, 0666)) == -1){ perror("fchmod failed"); 42

Extended IO (Cont'd) Change ownership of a file object Change ownership of a symlink #include <unistd.h> chown (path, user, group) char * path; user; group; fchown (fildes, user, group) fildes; user; group; lchown (path, user, group) char * path; user; group; path is the pathname of the file whose access is to be changed. user is the new user id (st_uid) for the file. A value of -1 leaves the user id unchanged. group is the new group id (st_gid) for the file. A value of -1 leaves the group id unchanged. returns: 0 on success or -1 x, fd; if((x = chown("myfile", 215, 102)) == -1){ perror("chmod failed"); 43

Extended IO (Cont'd) Check REAL access privilege to a file object #include <sys/file.h> access (path, amode) char * path; amode; path Address of a pathname naming a file of type ordinary, directory, FIFO, block special, character special, or symbolic link. amode Access mode bit pattern returns: 0 on success or -1 x; if((x = access("myfile", W_OK R_OK)) == -1){ perror("no REAL access"); 44

Extended IO (Cont'd) Get file object status from i-node #include <sys/types.h> #include <sys/stat.h> stat (path, buffer_ptr) char * path; struct stat * buffer_ptr; fstat (fildes, buffer_ptr) fildes; struct stat * buffer_ptr; path fildes buffer_ptr Address of a pathname A valid, active file descriptor Address of a stat buffer to fill returns: 0 on success or -1 45

Extended IO (Cont'd) stat(), fstat() (cont'd) struct stat { dev_t st_dev; /* device i-node on */ ino_t st_ino; /* this i number */ mode_t st_mode; /* protection bits */ nlink_t st_nlink; /* hard link count */ uid_t st_uid; /* owner UID */ gid_t st_gid; /* owner GID */ dev_t st_rdev; /* device codes if device */ off_t st_size; /* total bytes */ time_t st_atime; /* last file access */ time_t st_mtime; /* last file modify */ time_t st_ctime; /* last i-node modify */ long st_blksize; /* element size */ long st_blocks; /* blocks allocated */ ; x; struct stat buf; if((x = stat("myfile", &buf)) == -1){ perror("stat failed"); prf(" myfile is %d bytes long\n", buf.st_size); 46

Extended IO (Cont'd) Change operations on an open file #include <fcntl.h> fcntl (fildes, command, argument) fildes; command; argument; fildes A valid, active file descriptor command A file control command argument An argument, either an eger (when command is one of F_DUPFD, F_GETFD, F_SETFD, or F_SETFL) or a poer to a struct flock (when command is one of F_GETLK, F_SETLK, F_SETLKW or F_FREESP) returns: value, channel or -1 47

Extended IO (Cont'd) fcntl() (cont'd) defined cmd values: F_DUPFD dup channel on lowest channel greater than or equal to arg F_GETFD get channel close-on-exec flag (LSB 0 = keep-open-across-exec) F_SETFD set close-on-exec flag as above using arg (0 open, 1 close) F_GETFL get channel status flag F_SETFL set channel status flag with arg F_FREESP free storage space on a section of the file using flock structure F_SETLK set or clear a lock using flock F_SETLKW synchronous version of F_SETLK F_GETLK check for lock status using flock struct flock { short l_type; /* F_RDLCK, F_UNLCK, F_WRLCK */ short l_whence; /* SEEK_SET, SEEK_CUR, SEEK_END */ off_t l_start; /* relative offset to start */ off_t l_len; /* length to lock (0 to EOF) */ pid_t l_pid; /* pid of process with lock */ short l_sysid; /* system_id of process with lock */ ; 48

Extended IO (Cont'd) fcntl() (cont'd) defined arg values for F_GETFL and F_SETFL: O_APPEND change to append mode O_NONBLOCK non_blocking mode for terminals and pipes O_SYNC synchronous writes x, fd, flag; struct flock mylock; if((flag = fcntl(0, F_GETFL, 0)) == -1){ perror("fcntl F_GETFL failed"); if((x = (fcntl(0, F_SETFL, flag O_NONBLOCK)) == -1){ perror("fcntl F_SETFL failed"); mylock.l_type = F_WRLCK; mylock.l_whence = SEEK_SET; mylock.l_start = 300; mylock.l_len = 0; if((x = (fcntl(fd, F_SETLKW, &mylock) == -1){ perror("fcntl F_SETLKW failed"); 49