Stat, mmap, process syncing and file system interface Nezer J. Zaidenberg.

Slides:



Advertisements
Similar presentations
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 2)
Advertisements

© Original by Donald Acton; Changes by George Tsiknis Unix I/O Related Text Sections: 2nd Ed: and st Ed: and
Operating system services Program execution I/O operations File-system manipulation Communications Error detection Resource allocation Accounting Protection.
Daemon Processes Long lived utility processes Often started at system boot and ended when system shuts down Run in the background with no controlling terminal.
1 Files and Directories Hua LiSystems ProgrammingCS2690Files and Directories.
Read vs. mmap Tan Li. Man mmap #include void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset); int munmap(void *start, size_t.
System-Level I/O Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O.
CS 311 – Lecture 09 Outline Introduction to Systems programming – System calls – Categories of system calls Error Management System calls File Handling.
1 Case Study 1: UNIX and LINUX Chapter History of unix 10.2 Overview of unix 10.3 Processes in unix 10.4 Memory management in unix 10.5 Input/output.
CS Lecture 17 Outline Named pipes Signals Lecture 17
Files. System Calls for File System Accessing files –Open, read, write, lseek, close Creating files –Create, mknod.
CS 311 – Lecture 10 Outline Review open() and close() Difference between fopen() and open() File management system calls – read() – write() – lseek() –
6/24/2015B.RamamurthyPage 1 File System B. Ramamurthy.
File Systems CSE 121 Spring 2003 Keith Marzullo. CSE 121 Spring 2003Review of File Systems2 References In order of relevance... 1)Maurice J. Bach, The.
I/O April 16, 2002 Topics Files Unix I/O Standard I/O Reading: (Beta) or (New) Problems: 12.9 (Beta) or 12.4 (New) class24.ppt
Lecture 17 FS APIs and vsfs. File and File Name What is a File? Array of bytes. Ranges of bytes can be read/written. File system consists of many files,
7/15/2015B.RamamurthyPage 1 File System B. Ramamurthy.
CS 311 – Lecture 12 Outline File management system calls Stat() Directory Information  Opendir()  Readdir()  Closedir() Truncate() and remove() Lecture.
1 Unix File System API Operating System Hebrew University Spring 2007.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
Lecture 3: System Calls & API Standards. Lecture 3 / Page 2AE4B33OSS Silberschatz, Galvin and Gagne ©2005 Contents Implementation of API System call types.
Chapter 39 Virtsualization of Storage: File and Directory Chien-Chung Shen CIS, UD
Input and Output Topics I/O hardware Unix file abstraction Robust I/O File sharing io.ppt CS 105 “Tour of the Black Holes of Computing”
1 System-Level I/O Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron.
Week 12 - Friday.  What did we talk about last time?  Exam 2 post mortem  Low level file I/O.
CSCE Systems Programming Lecture 03 - The Stat System Call CSCE 510 Jan 23, 2013.
S -1 Shared Memory. S -2 Motivation Shared memory allows two or more processes to share a given region of memory -- this is the fastest form of IPC because.
File System Review bottomupcs.com J. Kubiatowicz, UC Berkeley.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
UNIX Files File organization and a few primitives.
NCHU System & Network Lab Lab 11 Memory Mapped File.
Files and Directories File types stat functions for file information
System Calls & Signals. setsockopt n Used to set socket options n SO_LINGER - Sets or gets the SO_LINGER option. The argument is a linger structure. n.
Today’s topic Access and manipulate meta data for files –File type, ownership, access permissions, access time, etc How to determine if a file is not there?
CSCI 330 UNIX and Network Programming Unit VII: I/O Management I.
Laface 2007 File system 2.1 Operating System Design Filesystem system calls buffer allocation algorithms getblk brelse bread breada bwrite iget iput bmap.
Stat mmap and file system interface Nezer J. Zaidenberg.
CSE333 SECTION 3. Important Dates Jan 27 th – Homework 1 Due Feb 6 th – Midterm.
January 7, 2003Serguei Mokhov, 1 File I/O System Calls Reference COMP 229, 444, 5201 Revision 1.2 Date: July 21, 2004.
CS 241 Section Week #8 (10/29/09). Outline MP5 Overview Files & I/O UNIX File Systems inodes Directories Links.
Library Functions The UNIX system provides a large number of C functions as libraries. Some of these implement frequently used operations, while others.
File I/O open close lseek read and write – unbuffered I/O dup and dup2.
CSCI 330 UNIX and Network Programming Unit VIII: I/O Management II.
OS interface: file and I/O system calls File operations in C/C++? –fopen(), fread(), fwrite(), fclose(), fseek() in C f.open(…), f.close(…) in C++ I/O.
File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
CSC 271 – Software I: Utilities and Internals An Introduction to File I/O in Linux Credited to Dr. Robert Siegfried and Beginning Linux Programming by.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
CSCE Systems Programming Lecture 07 – Make, Stdio, I/O Buffering CSCE 510 Jan 23, 2013.
Week 12 - Friday.  What did we talk about last time?  Exam 2 post mortem.
Chapter 39 File and Directory Chien-Chung Shen CIS/UD
File table: a list of opened files Each entry contains: – Index: file descriptors – Pointer to the file in memory – Access mode File descriptor is a positive.
File System Design David E. Culler CS162 – Operating Systems and Systems Programming Lecture 23 October 22, 2014 Reading: A&D a HW 4 out Proj 2 out.
Week 12 - Thursday CS222.
Lecture 3: System Calls & API Standards
Unix Programming Environment
Unix Files Course code: 10CS62 Prepared by Department of CSE
Operating System Hebrew University Spring 2004
Making Virtual Memory Real: The Linux-x86-64 way
File System B. Ramamurthy B.Ramamurthy 11/27/2018.
“The course that gives CMU its Zip!”
CSE 333 – Section 3 POSIX I/O Functions.
File Structure Related system calls
“The course that gives CMU its Zip!”
File I/O (1) Prof. Ikjun Yeom TA – Hoyoun
CSE 333 – Section 3 POSIX I/O Functions.
Persistence: File System API
CSE 333 – Section 3 POSIX I/O Functions.
Virtual Memory: Systems CSCI 380: Operating Systems
Chien-Chung Shen CIS, UD
Operating System Hebrew University Spring 2009
Presentation transcript:

Stat, mmap, process syncing and file system interface Nezer J. Zaidenberg

Agenda We will discuss how files are accessed in UNIX We will discuss how files are accessed in UNIX Lots of system calls will be introduced. (most of them – reintroduced.) Lots of system calls will be introduced. (most of them – reintroduced.) We will also discuss shared memory and syncing between processes. We will also discuss shared memory and syncing between processes.

Prior knowladge open(2) and fopen(3) – open a file to read/write. open(2) and fopen(3) – open a file to read/write. close(2) and fclose(3) – close a file to read/write. close(2) and fclose(3) – close a file to read/write. read(2), write(2), fread(2), fwrite(2) read(2), write(2), fread(2), fwrite(2) opendir(3), readdir(3) – read directory contents opendir(3), readdir(3) – read directory contents All those are covered in K&R2 chapter 8 All those are covered in K&R2 chapter 8

Stat(2) Stat(2) is a system call used to learn about a file. Stat(2) is a system call used to learn about a file. Information received Information received Access permissions Access permissions Size Size Owner Owner Group Group File last access time, change time and modification time (change time = file information changed, mtime = content change) File last access time, change time and modification time (change time = file information changed, mtime = content change) Etc. Etc. Stat(2) returns a struct with all the file information Stat(2) returns a struct with all the file information

Stat(2) NAME stat, lstat, fstat -- get file status stat, lstat, fstat -- get file statusSYNOPSIS #include #include int stat(const char *path, struct stat *sb); int stat(const char *path, struct stat *sb); int lstat(const char *path, struct stat *sb); int lstat(const char *path, struct stat *sb); int fstat(int fd, struct stat *sb); int fstat(int fd, struct stat *sb);

Struct stat (1/2)) struct stat { struct stat { dev_t st_dev; /* device inode resides on */ dev_t st_dev; /* device inode resides on */ ino_t st_ino; /* inode's number */ ino_t st_ino; /* inode's number */ mode_t st_mode; /* inode protection mode */ mode_t st_mode; /* inode protection mode */ nlink_t st_nlink; /* number or hard links to the file */ nlink_t st_nlink; /* number or hard links to the file */ uid_t st_uid; /* user-id of owner */ uid_t st_uid; /* user-id of owner */ gid_t st_gid; /* group-id of owner */ gid_t st_gid; /* group-id of owner */ dev_t st_rdev; /* device type, for special file inode */ dev_t st_rdev; /* device type, for special file inode */

Struct stat (2/2) struct timespec st_atimespec; /* time of last access */ struct timespec st_atimespec; /* time of last access */ struct timespec st_mtimespec; /* time of last data modification */ struct timespec st_mtimespec; /* time of last data modification */ struct timespec st_ctimespec; /* time of last file status change */ struct timespec st_ctimespec; /* time of last file status change */ off_t st_size; /* file size, in bytes */ off_t st_size; /* file size, in bytes */ quad_t st_blocks; /* blocks allocated for file */ quad_t st_blocks; /* blocks allocated for file */ u_long st_blksize;/* optimal file sys I/O ops blocksize */ u_long st_blksize;/* optimal file sys I/O ops blocksize */ u_long st_flags; /* user defined flags for file */ u_long st_flags; /* user defined flags for file */ u_long st_gen; /* file generation number */ u_long st_gen; /* file generation number */ }; };

Access permission Like everything in the IDF file system permissions in UNIX is divided to 3 parts Like everything in the IDF file system permissions in UNIX is divided to 3 parts ME – what I can do. ME – what I can do. MY GROUP – (every user have a group) – what my group can do. MY GROUP – (every user have a group) – what my group can do. OTHER – what everybody else can do. OTHER – what everybody else can do.

ACCESS PERMISSIONS And of course what every body can do also divides to 3. And of course what every body can do also divides to 3. Permission to read (file contents, directory contents) Permission to read (file contents, directory contents) Permission to write (file contents, add files to directory) Permission to write (file contents, add files to directory) Permission to execute (file, cd to directory) Permission to execute (file, cd to directory) FILE SYSTEM PERMISSION ARE MANDATORY! FILE SYSTEM PERMISSION ARE MANDATORY! We use 3 octal digits to represent permissions. We use 3 octal digits to represent permissions. Read = 4, write =2 execute =1 we sum the permission number and get the digit Read = 4, write =2 execute =1 we sum the permission number and get the digit

Some examples Permissionmeaning 6446(=4+2) I can read and write My group and everybody else can read 777(7 = 4+2+1) everybody can read write and execute 705(7=4+2+1), 5=(4+1) I can read write and execute My group can’t do anything but other people can read and execute 700Only I can read, write and execute the file

lseek(2) Move to specific location on a file. Move to specific location on a file. Usage example: Usage example: Large database on a specific file Large database on a specific file User interested in a specific value User interested in a specific value lseek(2) to the right location and read the value lseek(2) to the right location and read the value

lseek(2) NAME lseek -- reposition read/write file offset lseek -- reposition read/write file offsetSYNOPSIS #include #include off_t off_t lseek(int fildes, off_t offset, int whence); lseek(int fildes, off_t offset, int whence);

creat(2) SYNOPSIS #include #include int int creat(const char *path, mode_t mode); creat(const char *path, mode_t mode); // Open can also be used for this file.

dup(2) SYNOPSIS #include #include int int dup(int oldd); dup(int oldd); int int dup2(int oldd, int newd); dup2(int oldd, int newd);

File locking Different flavours of UNIX offers different file locking. Different flavours of UNIX offers different file locking. Two system calls are used: Two system calls are used: flock(2) –on BSD UNIX (Mac OSX, FreeBSD, Darwin) flock(2) –on BSD UNIX (Mac OSX, FreeBSD, Darwin) fcntl(2) - on SVR4 UNIX (Linux, Solaris, HP-UX) fcntl(2) - on SVR4 UNIX (Linux, Solaris, HP-UX) File locking only works on processes not threads. File locking only works on processes not threads. Use fcntl(2) locking on TAU. Use fcntl(2) locking on TAU.

flock(2) SYNOPSIS #include #include #define LOCK_SH 1 /* shared lock */ #define LOCK_SH 1 /* shared lock */ #define LOCK_EX 2 /* exclusive lock */ #define LOCK_EX 2 /* exclusive lock */ #define LOCK_NB 4 /* don't block when locking */ #define LOCK_NB 4 /* don't block when locking */ #define LOCK_UN 8 /* unlock */ #define LOCK_UN 8 /* unlock */ int int flock(int fd, int operation); flock(int fd, int operation);

fcntl(2) SYNOPSIS #include #include int int fcntl(int fd, int cmd, int arg); fcntl(int fd, int cmd, int arg); OFTEN USED FOR FILE LOCKING ON SYS V SYSTEMS

Memory mapping mmap(2) mmap(2) munmap(2) munmap(2) msync(2) msync(2)

mmap(2) Maps a file to memory Maps a file to memory This memory can be shared (among process) or locked This memory can be shared (among process) or locked Check mprotect(2) for locking option (beyond our scope) Check mprotect(2) for locking option (beyond our scope) Use msync(2) to write changes Use msync(2) to write changes Use munmap(2) to write and free memory Use munmap(2) to write and free memory Homework : use read(2) and write(2) to copy file. Then use mmap and memory copy. What works faster? Homework : use read(2) and write(2) to copy file. Then use mmap and memory copy. What works faster?

mmap(2) NAME mmap -- map files or devices into memory mmap -- map files or devices into memorySYNOPSIS #include #include void * void * mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t offset); mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t offset);

munmap(2) NAME munmap -- remove a mapping munmap -- remove a mappingSYNOPSIS #include #include int munmap(void *addr, size_t len); int munmap(void *addr, size_t len);

msync(2) NAME msync -- synchronize a mapped region msync -- synchronize a mapped regionSYNOPSIS #include #include int msync(void *addr, size_t len, int flags); int msync(void *addr, size_t len, int flags);

Example (1/2) #include #include #include /* mmap() is defined in this header */ #include #include int main (int argc, char *argv[]) // will be used as cp source dest { int fdin, fdout; char *src, *dst; char *src, *dst; struct stat statbuf; fdin = open (argv[1], O_RDONLY); fdout = open (argv[2], O_RDWR | O_CREAT | O_TRUNC, FILE_MODE);

Example 2/2 fstat (fdin,&statbuf); // get size lseek (fdout, statbuf.st_size - 1, SEEK_SET); // go to the location corresponding to the last byte write (fdout, "", 1) // output file now the size of input file /* mmap the input and output file */ /* mmap the input and output file */ src = mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED, fdin, 0); dst = mmap (0, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fdout, 0); memcpy (dst, src, statbuf.st_size); } // if we wanted to write and continue we had to use munmap or msync

Sync ProcessThread Shared memoryShared memory via mmap(2) Everything is shared MutexMutex via flock(2)Mutex via pthread_mutex CondCond via socket and message passing Cond via pthread_cond

Why does mmap(2) and memcpy(2) works faster then read(2) and write(2) when copying files?

Explanation When read(2)ing and write(2) we copy from OS buffer to user buffers and back. When read(2)ing and write(2) we copy from OS buffer to user buffers and back. This copy is redundant. mmap(2) just use the OS buffers making less memory copies. Therefore, we should expect mmap to work faster. This copy is redundant. mmap(2) just use the OS buffers making less memory copies. Therefore, we should expect mmap to work faster.

Linking Symlink(2) Symlink(2) Link(2) Link(2) Create soft and hard link Create soft and hard link

Types of links Hard Link Hard Link new (additional) name for a file. new (additional) name for a file. File is stored once with two (or more) names. File is stored once with two (or more) names. No “master” and “slave” later. All names are equal. No “master” and “slave” later. All names are equal. Problems Problems Who is charged for the file for quota? Who is charged for the file for quota? How to save links after backup? How to save links after backup? Symbolic (soft) Link Symbolic (soft) Link New file contain path of linked file New file contain path of linked file Most API follow symbolic links by reading content and opening linked file Most API follow symbolic links by reading content and opening linked file If linked file is deleted – we have broken link. Link that points at nothing If linked file is deleted – we have broken link. Link that points at nothing

link(2) and unlink(2) Unlink – delete a directory entry. Removes a “link” from a directory. When all links are removed file is deleted. (most files are linked once) Unlink – delete a directory entry. Removes a “link” from a directory. When all links are removed file is deleted. (most files are linked once) Link – create a hard link – give a new name to a file. File now resides in both original and new directory. Link – create a hard link – give a new name to a file. File now resides in both original and new directory.

Unlink(2) NAME unlink -- remove directory entry unlink -- remove directory entrySYNOPSIS #include #include int int unlink(const char *path); unlink(const char *path);

link(2) NAME link -- make a hard file link link -- make a hard file linkSYNOPSIS #include #include int int link(const char *path1, const char *path2); link(const char *path1, const char *path2);

symlink(2) NAME symlink -- make symbolic link to a file symlink -- make symbolic link to a fileSYNOPSIS #include #include int int symlink(const char *path1, const char *path2); symlink(const char *path1, const char *path2);

Signals - revisitted Debt from homework

Set signal handler signal(3) – old interface signal(3) – old interface sigaction(2) – new interface (more generic) sigaction(2) – new interface (more generic) Those functions get signal numbers and signal handler function pointer Those functions get signal numbers and signal handler function pointer When signal is caught the function handler is called. When signal is caught the function handler is called.

Signal(3) SYNOPSIS #include #include void (* void (* signal(int sig, void (*func)(int)))(int); signal(int sig, void (*func)(int)))(int); //or in the equivalent but easier to read typedef'd version: //or in the equivalent but easier to read typedef'd version: typedef void (*sig_t) (int); typedef void (*sig_t) (int); sig_t sig_t signal(int sig, sig_t func); signal(int sig, sig_t func);

Sigaction(2) (1/2) SYNOPSIS #include #include struct sigaction { union { struct sigaction { union { void (*__sa_handler)(int); void (*__sa_handler)(int); void (*__sa_sigaction)(int, struct __siginfo *, void *); void (*__sa_sigaction)(int, struct __siginfo *, void *); } __sigaction_u; /* signal handler */ } __sigaction_u; /* signal handler */ int sa_flags; /* see signal options below */ int sa_flags; /* see signal options below */ sigset_t sa_mask; /* signal mask to apply */ sigset_t sa_mask; /* signal mask to apply */ }; };

Sigaction(2) 2/2 #define sa_handler __sigaction_u.__sa_handler #define sa_handler __sigaction_u.__sa_handler #define sa_sigaction __sigaction_u.__sa_sigaction #define sa_sigaction __sigaction_u.__sa_sigaction int int sigaction(int sig, const struct sigaction * restrict act, sigaction(int sig, const struct sigaction * restrict act, struct sigaction * restrict oact); struct sigaction * restrict oact);

Kill(2) SYNOPSIS #include #include int int kill(pid_t pid, int sig); kill(pid_t pid, int sig);

What you may do in a sig handler Signal handler are very low level code. They are invoked by the kernel, as an interrupt to the program (even while doing other system call) – some of you got EINTR on select(2) Therefore we must not get another interrupt on signal handler. Use only memory operation and non blocking system calls. (specifically avoid file I/O on signal hanglers) Getting another signal on signal handler may result in undefined behavior or even damned recursion

Things to specifically avoid Don’t call new process from signal handler. (if the process dies and you get SIGCHLD you will end up in damn recursion) Don’t call new process from signal handler. (if the process dies and you get SIGCHLD you will end up in damn recursion) Avoid calling syslog from signal handler. Avoid calling syslog from signal handler. Adding stuff to log queue in signal handler and deleting stuff from the queue later is good alternative. Adding stuff to log queue in signal handler and deleting stuff from the queue later is good alternative.

Interrupted system call You get EINTR on select(2) You get EINTR on select(2) Check chapter 10.5 in advanced programming in the unix environment Check chapter 10.5 in advanced programming in the unix environment Also do man select(2) and check EINTR Also do man select(2) and check EINTR Basically if you get a signal such a SIGCHLD select will fail. This is condition you have to check but you don’t have to die (actually you should not die) Basically if you get a signal such a SIGCHLD select will fail. This is condition you have to check but you don’t have to die (actually you should not die)