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

Similar documents
Housekeeping (Lecture 12-6/26/2018)

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

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

UNIX Structure. Operating Systems In Depth VII 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

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

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

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

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

CSci 4061 Introduction to Operating Systems. File Systems: Basics

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

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

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

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

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

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

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

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

Process Creation in UNIX

UNIX System Calls. Sys Calls versus Library Func

CS 25200: Systems Programming. Lecture 14: Files, Fork, and Pipes

CSC 271 Software I: Utilities and Internals

CS240: Programming in C

Lecture 21 Systems Programming in C

Advanced Unix Programming Module 06 Raju Alluri spurthi.com

Linux C C man mkdir( ) mkdir Linux man mkdir mkdir( ) mkdir mkdir( )

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

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

Chapter 10. The UNIX System Interface

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

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

IPC and Unix Special Files

Unix System Calls. Gwan-Hwan Hwang Dept. CSIE National Taiwan Normal University

UNIX System Programming. Overview. 1. A UNIX System. 2. Processes (review) 2.1. Context. Pipes/FIFOs

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

MMAP AND PIPE. UNIX Programming 2015 Fall by Euiseong Seo

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

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

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

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

CSE 333 SECTION 3. POSIX I/O Functions

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

CSCI 4061: Files, Directories, Standard I/O

Logical disks. Bach 2.2.1

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

Summer June 15, 2010

CSE 410: Systems Programming

Operating systems. Lecture 7

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

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

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

Basic OS Progamming Abstrac7ons

Preview. Interprocess Communication with Pipe. Pipe from the Parent to the child Pipe from the child to the parent FIFO popen() with r Popen() with w

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

Topics. Unix Pipes (CSE 422S) In A Nutshell. Ken Wong Washington University. Pipe (Unnamed FIFO) in the Shell.

File I/0. Advanced Programming in the UNIX Environment

Basic OS Progamming Abstrac2ons

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

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Programmation Systèmes Cours 4 IPC: FIFO

Files and the Filesystems. Linux Files

Overview. Unix System Programming. Outline. Directory Implementation. Directory Implementation. Directory Structure. Directories & Continuation

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

CS2028 -UNIX INTERNALS

Inter-Process Communication

CSE 333 SECTION 3. POSIX I/O Functions

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

Programmation Système Cours 4 IPC: FIFO

FILE SYSTEMS. Jo, Heeseung

UNIX input and output

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

Programmation Systèmes Cours 8 Synchronization & File Locking

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

TCSS 422: OPERATING SYSTEMS

ECE 650 Systems Programming & Engineering. Spring 2018

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

RCU. ò Walk through two system calls in some detail. ò Open and read. ò Too much code to cover all FS system calls. ò 3 Cases for a dentry:

VFS, Continued. Don Porter CSE 506

File and Directories. Advanced Programming in the UNIX Environment

Data and File Structures Chapter 2. Basic File Processing Operations

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

Virtual File System. Don Porter CSE 506

CS631 - Advanced Programming in the UNIX Environment

CS140 Operating Systems Final December 12, 2007 OPEN BOOK, OPEN NOTES

Basic OS Programming Abstractions (and Lab 1 Overview)

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

Recitation 8: Tshlab + VM

UNIX System Programming

Operating System Labs. Yuanbin Wu

Virtual File System. Don Porter CSE 306

Files and Directories Filesystems from a user s perspective

T4-Input/Output Management

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

Maria Hybinette, UGA. ! One easy way to communicate is to use files. ! File descriptors. 3 Maria Hybinette, UGA. ! Simple example: who sort

Files and Directories

Files and Directories Filesystems from a user s perspective

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

ISA 563: Fundamentals of Systems Programming

Transcription:

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

Directories unix etc home pro dev passwd motd twd unix... slide1 slide2 CS33 Intro to Computer Systems XXI 2 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Directory Representation Component Name directory entry Inode Number. 1.. 1 unix 117 etc 4 home 18 pro 36 dev 93 CS33 Intro to Computer Systems XXI 3 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Hard Links unix etc home pro dev twd image motd unix... slide1 slide2 $ ln /unix /etc/image # link system call CS33 Intro to Computer Systems XXI 4 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Directory Representation. 1.. 1 unix 117 etc 4 home 18 pro 36 dev 93. 4.. 1 image 117 motd 33 CS33 Intro to Computer Systems XXI 5 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Symbolic Links unix etc home pro dev twd image twd unix... mylink /home/twd % ln s /unix /home/twd/mylink % ln s /home/twd /etc/twd # symlink system call slide1 slide2 /unix CS33 Intro to Computer Systems XXI 6 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Working Directory Maintained in kernel for each process paths not starting from / start with the working directory changed by use of the chdir system call» cd shell command displayed (via shell) using pwd» how is this done? CS33 Intro to Computer Systems XXI 7 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Symbolic Links twd Quiz 1 What is the working directory after doing cd /etc/twd/..? unix etc home pro dev a) / b) /etc c) /home d) /home/twd image twd unix... mylink /home/twd slide1 slide2 /unix CS33 Intro to Computer Systems XXI 8 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Open #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open(const char *path, int options [, mode_t mode]) options» O_RDONLY open for reading only» O_WRONLY open for writing only» O_RDWR open for reading and writing» O_APPEND set the file offset to end of file prior to each write» O_CREAT if the file does not exist, then create it, setting its mode to mode adjusted by umask» O_EXCL if O_EXCL and O_CREAT are set, then open fails if the file exists» O_TRUNC delete any previous contents of the file» O_NONBLOCK don t wait if I/O can t be done immediately CS33 Intro to Computer Systems XXI 9 Copyright 2018 Thomas W. Doeppner. All rights reserved.

File Access Permissions Who s allowed to do what? who what» user (owner)» group» others (rest of the world)» read» write» execute CS33 Intro to Computer Systems XXI 10 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Permissions Example adm group: tom, trina $ ls -lr.: total 2 drwxr-x--x 2 tom adm 1024 Dec 17 13:34 A drwxr----- 2 tom adm 1024 Dec 17 13:34 B./A: total 1 -rw-rw-rw- 1 tom adm 593 Dec 17 13:34 x./b: total 2 -r--rw-rw- 1 tom adm 446 Dec 17 13:34 x -rw----rw- 1 trina adm 446 Dec 17 13:45 y CS33 Intro to Computer Systems XXI 11 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Setting File Permissions #include <sys/types.h> #include <sys/stat.h> int chmod(const char *path, mode_t mode) sets the file permissions of the given file to those specified in mode only the owner of a file and the superuser may change its permissions nine combinable possibilities for mode (read/write/execute for user, group, and others)» S_IRUSR (0400), S_IWUSR (0200), S_IXUSR (0100)» S_IRGRP (040), S_IWGRP (020), S_IXGRP (010)» S_IROTH (04), S_IWOTH (02), S_IXOTH (01) CS33 Intro to Computer Systems XXI 12 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Umask Standard programs create files with maximum needed permissions as mode compilers: 0777 editors: 0666 Per-process parameter, umask, used to turn off undesired permission bits e.g., turn off all permissions for others, write permission for group: set umask to 027» compilers: permissions = 0777 & ~(027) = 0750» editors: permissions = 0666 & ~(027) = 0640 set with umask system call or (usually) shell command CS33 Intro to Computer Systems XXI 13 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Creating a File Use either open or creat open(const char *pathname, int flags, mode_t mode)» flags must include O_CREAT creat(const char *pathname, mode_t mode)» open is preferred The mode parameter helps specify the permissions of the newly created file permissions = mode & ~umask CS33 Intro to Computer Systems XXI 14 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Link and Reference Counts 1 RDONLY 0 inode pointer n1 dir1 dir2 link count == 2 reference count == 1 int fd = open("n1", O_RDONLY); // n1 s reference count // incremented by 1 n2 f1 CS33 Intro to Computer Systems XXI 15 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Link and Reference Counts 1 RDONLY 0 inode pointer n1 dir1 dir2 link count == 2 1 reference count == 1 int fd = open("n1", O_RDONLY); // n1 s reference count // incremented by 1 unlink("n1"); // link count decremented by 1 // same effect in shell via rm n1 n2 f1 CS33 Intro to Computer Systems XXI 16 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Link and Reference Counts 1 RDONLY 0 inode pointer n1 dir1 dir2 link count == 2 1 reference count == 1 0 int fd = open("n1", O_RDONLY); // n1 s reference count // incremented by 1 unlink("n1"); // link count decremented by 1 close(fd); // reference count decremented by 1 n2 f1 CS33 Intro to Computer Systems XXI 17 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Link and Reference Counts n1 dir1 dir2 link count == 2 1 reference count == 1 0 int fd = open("n1", O_RDONLY); // n1 s reference count // incremented by 1 unlink("n1"); // link count decremented by 1 close(fd); // reference count decremented by 1 n2 f1 CS33 Intro to Computer Systems XXI 18 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Link and Reference Counts n1 dir1 dir2 0 link count == 1 reference count == 0 unlink( dir1/n2"); // link count decremented by 1 n2 f1 CS33 Intro to Computer Systems XXI 19 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Quiz 2 int main() { int fd = creat("file", 0666); unlink("file"); PutStuffInFile(fd); ReadStuffFromFile(fd); return 0; } Assume that PutStuffInFile writes to the given file, and ReadStuffFromFile reads from the file. a) This program is doomed to failure, since the file is deleted before it s used b) Because the file is used after the unlink call, it won t be deleted c) The file will be deleted when the program terminates CS33 Intro to Computer Systems XXI 20 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Interprocess Communication (IPC): Pipes CS33 Intro to Computer Systems XXI 21 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Interprocess Communication: Same Machine I Kernel buffer CS33 Intro to Computer Systems XXI 22 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Interprocess Communication: Same Machine II process 1 process 2 Shared Memory CS33 Intro to Computer Systems XXI 23 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Interprocess Communication: Different Machines Internet CS33 Intro to Computer Systems XXI 24 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Intramachine IPC $cslab2e who wc -l who pipe wc -l CS33 Intro to Computer Systems XXI 25 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Intramachine IPC $cslab2e who wc -l int fd[2]; pipe(fd); fd[1] pipe fd[0] if (fork() == 0) { close(fd[0]); close(1); dup(fd[1]); close(fd[1]); execl( /usr/bin/", "who", 0); // who sends output to pipe } if (fork() == 0) { close(fd[1]); close(0); dup(fd[0]); close(fd[0]); execl( /usr/bin/wc", "wc", "-l", 0); // wc s input is from pipe } close(fd[1]); close(fd[0]); // CS33 Intro to Computer Systems XXI 26 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Intermachine Communication Can pipes be made to work across multiple machines? covered soon...» what happens when you type who ssh cslab3a wc l? CS33 Intro to Computer Systems XXI 27 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Sharing Files You re doing a project with a partner You code it as one 15,000-line file the first 7,500 lines are yours the second 7,500 lines are your partner s You edit the file, changing 6,000 lines it s now 5am Your partner completes her changes at 5:01am At 5:02am you look at the file your partner s changes are there yours are not CS33 Intro to Computer Systems XXI 28 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Lessons Never work with a partner Use more than one file Read up on git Use an editor and file system that support file locking CS33 Intro to Computer Systems XXI 29 Copyright 2018 Thomas W. Doeppner. All rights reserved.

What We Want... I want to just read the file. Me too. I want to modify the file. Me too. CS33 Intro to Computer Systems XXI 30 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Types of Locks Shared (readers) locks any number may have them at same time may not be held when an exclusive lock is held Exclusive (writers) locks only one at a time may not be held when a shared lock is held CS33 Intro to Computer Systems XXI 31 Copyright 2018 Thomas W. Doeppner. All rights reserved.

What We Want... I ve got a shared lock. Me too. My exclusive lock request was rejected. Mine too. CS33 Intro to Computer Systems XXI 32 Copyright 2018 Thomas W. Doeppner. All rights reserved.

What We Want... I ve got an exclusive lock. My exclusive request was rejected. My shared request was rejected. Mine too. CS33 Intro to Computer Systems XXI 33 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Locking Files Early Unix didn t support file locking How did people survive? open("file.lck", O_RDWR O_CREAT O_EXCL, 0666);» operation fails if file.lck exists, succeeds (and creates file.lck) otherwise» requires cooperative programs CS33 Intro to Computer Systems XXI 34 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Locking Files (continued) How it s done in modern Unix advisory locks may be placed on files» may request shared (readers) or exclusive (writers) lock fcntl system call» either succeeds or fails» open, read, write always work, regardless of locks» a lock applies to a specified range of bytes, not necessarily the whole file» requires cooperative programs CS33 Intro to Computer Systems XXI 35 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Locking Files (still continued) How to: struct flock fl; fl.l_type = F_RDLCK; // read lock // fl.l_type = F_WRLCK; // write lock // fl.l_type = F_UNLCK; // unlock fl.l_whence = SEEK_SET; fl.l_start = 0; fl.l_len = 0; fd = open("file", O_RDWR); // starting where // offset // how much? (0 = whole file) if (fcntl(fd, F_SETLK, &fl) == -1) if ((errno == EACCES) (errno == EAGAIN)) // didn t get lock else // something else is wrong else // got the lock! CS33 Intro to Computer Systems XXI 36 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Locking Files (yet still continued) Making locks mandatory: if the file s permissions have group execute permission off and set-group-id on, then locking is enforced» read, write fail if file is locked by someone other than the caller however» doesn t work on NFSv3 or earlier (we run NFSv3 at Brown CS) CS33 Intro to Computer Systems XXI 37 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Quiz 3 Your program currently has a shared lock on a portion of a file. It would like to upgrade the lock to be an exclusive lock. Would there be any problems with adding an option to fcntl that would allow the holder of a shared lock to wait until it s possible to upgrade to an exclusive lock, then do the upgrade? a) at least one major problem b) either no problems whatsoever or some easy-to-deal-with problems CS33 Intro to Computer Systems XXI 38 Copyright 2018 Thomas W. Doeppner. All rights reserved.