stat mmap and file system interface nezer j. zaidenberg
TRANSCRIPT
Stat mmap and file Stat mmap and file system interfacesystem interfaceNezer J. ZaidenbergNezer J. Zaidenberg
Stat(2)Stat(2)
NAMENAME
stat, lstat, fstat -- get file statusstat, lstat, fstat -- get file status
SYNOPSISSYNOPSIS
#include <sys/types.h>#include <sys/types.h>
#include <sys/stat.h>#include <sys/stat.h>
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 (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 dev_t st_rdev; /* device type, for special file inode */inode */
Struct stat (2/2)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 */
};};
Unlink(2)Unlink(2)
NAMENAME
unlink -- remove directory entryunlink -- remove directory entry
SYNOPSISSYNOPSIS
#include <unistd.h>#include <unistd.h>
intint
unlink(const char *path);unlink(const char *path);
Lseek(2)Lseek(2)
NAMENAME
lseek -- reposition read/write file offsetlseek -- reposition read/write file offset
SYNOPSISSYNOPSIS
#include <unistd.h>#include <unistd.h>
off_toff_t
lseek(int fildes, off_t offset, int whence);lseek(int fildes, off_t offset, int whence);
Creat(2)Creat(2)
SYNOPSISSYNOPSIS
#include <fcntl.h>#include <fcntl.h>
intint
creat(const char *path, mode_t mode);creat(const char *path, mode_t mode);
Dup(2)Dup(2)
SYNOPSISSYNOPSIS
#include <unistd.h>#include <unistd.h>
intint
dup(int oldd);dup(int oldd);
intint
dup2(int oldd, int newd);dup2(int oldd, int newd);
Flock(2)Flock(2)
SYNOPSISSYNOPSIS
#include <sys/file.h>#include <sys/file.h>
#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 */
intint
flock(int fd, int operation);flock(int fd, int operation);
Fcntl(2)Fcntl(2)
SYNOPSISSYNOPSIS
#include <fcntl.h>#include <fcntl.h>
intint
fcntl(int fd, int cmd, int arg);fcntl(int fd, int cmd, int arg);
LinkingLinking
Symlink(2)Symlink(2)
Link(2)Link(2)
Create soft and hard linkCreate soft and hard link
Memory mappingMemory mapping
Mmap(2)Mmap(2)
Munmap(2)Munmap(2)
Msync(2)Msync(2)
Mmap(2)Mmap(2)
Copy a file to memory Copy a file to memory
This memory can be shared (among process) or lockedThis memory can be shared (among process) or locked
Check mprotect(2) for locking option (beyond our Check mprotect(2) for locking option (beyond our scope)scope)
Use msync(2) to write changesUse msync(2) to write changes
Use munmap(2) to write and free memoryUse munmap(2) to write and free memory
Homework : use read(2) and write(2) to copy file. Then Homework : use read(2) and write(2) to copy file. Then use mmap and memory copy. What works faster?use mmap and memory copy. What works faster?
Mmap(2)Mmap(2)
NAMENAME
mmap -- map files or devices into memorymmap -- map files or devices into memory
SYNOPSISSYNOPSIS
#include <sys/mman.h>#include <sys/mman.h>
void *void *
mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t offset);offset);
Munmap(2)Munmap(2)
NAMENAME
munmap -- remove a mappingmunmap -- remove a mapping
SYNOPSISSYNOPSIS
#include <sys/mman.h>#include <sys/mman.h>
int munmap(void *addr, size_t len);int munmap(void *addr, size_t len);
Msync(2)Msync(2)
NAMENAME
msync -- synchronize a mapped regionmsync -- synchronize a mapped region
SYNOPSISSYNOPSIS
#include <sys/mman.h>#include <sys/mman.h>
int msync(void *addr, size_t len, int int msync(void *addr, size_t len, int flags);flags);
Example (1/2)Example (1/2)
#include <sys/types.h>#include <sys/types.h>
#include <sys/stat.h>#include <sys/stat.h>
#include <sys/mman.h> /* mmap() is defined in this header */#include <sys/mman.h> /* mmap() is defined in this header */
#include <fcntl.h>#include <fcntl.h>
int main (int argc, char *argv[]) // will be used as cp source destint main (int argc, char *argv[]) // will be used as cp source dest
{{
int fdin, fdout;int fdin, fdout;
char *src, *dst;char *src, *dst;
struct stat statbuf;struct stat statbuf;
fdin = open (argv[1], O_RDONLY);fdin = open (argv[1], O_RDONLY);
fdout = open (argv[2], O_RDWR | O_CREAT | O_TRUNC, FILE_MODE);fdout = open (argv[2], O_RDWR | O_CREAT | O_TRUNC, FILE_MODE);
Example 2/2Example 2/2
fstat (fdin,&statbuf); // get sizefstat (fdin,&statbuf); // get size
lseek (fdout, statbuf.st_size - 1, SEEK_SET); // go to the location lseek (fdout, statbuf.st_size - 1, SEEK_SET); // go to the location corresponding to the last bytecorresponding to the last byte
write (fdout, "", 1) // output file now the size of input filewrite (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);src = mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED, fdin, 0);
dst = mmap (0, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, dst = mmap (0, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fdout, 0);fdout, 0);
memcpy (dst, src, statbuf.st_size);memcpy (dst, src, statbuf.st_size);
} // if we wanted to write and continue we had to use munmap or msync } // if we wanted to write and continue we had to use munmap or msync
SignalsSignalsDebt from homeworkDebt from homework
Signal(3)Signal(3)
SYNOPSISSYNOPSIS
#include <signal.h>#include <signal.h>
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 or in the equivalent but easier to read typedef'd version:version:
typedef void (*sig_t) (int);typedef void (*sig_t) (int);
sig_tsig_t
signal(int sig, sig_t func);signal(int sig, sig_t func);
Sigaction(2) (1/2)Sigaction(2) (1/2)
SYNOPSISSYNOPSIS
#include <signal.h>#include <signal.h>
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/2Sigaction(2) 2/2
#define sa_handler #define sa_handler __sigaction_u.__sa_handler__sigaction_u.__sa_handler
#define sa_sigaction #define sa_sigaction __sigaction_u.__sa_sigaction__sigaction_u.__sa_sigaction
intint
sigaction(int sig, const struct sigaction * sigaction(int sig, const struct sigaction * restrict act,restrict act,
struct sigaction * restrict oact);struct sigaction * restrict oact);
Kill(2)Kill(2)
SYNOPSISSYNOPSIS
#include <signal.h>#include <signal.h>
intint
kill(pid_t pid, int sig);kill(pid_t pid, int sig);
What you may do in a What you may do in a sig handlersig handler
Signal handler are very low level code. They are Signal handler are very low level code. They are invoked by the kernel, as an interrupt to the invoked by the kernel, as an interrupt to the program (even while doing other system call) – program (even while doing other system call) – some of you got EINTR on select(2)some of you got EINTR on select(2)
Therefore we must not get another interrupt on Therefore we must not get another interrupt on signal handler. signal handler.
Use only memory operation and non blocking Use only memory operation and non blocking system calls. (specifically avoid file I/O on signal system calls. (specifically avoid file I/O on signal hanglers)hanglers)
Getting another signal on signal handler may result Getting another signal on signal handler may result in undefined behavior or even damned recursionin undefined behavior or even damned recursion
FAQFAQFrom homeworkFrom homework
debuggingdebugging
Gdb – lowest levelGdb – lowest level
Ddd – graphical front endDdd – graphical front end
Compile flag for debugging - -gCompile flag for debugging - -g
Create coredump Create coredump (tcsh) setenv coredumpsize 100000000(tcsh) setenv coredumpsize 100000000 (bash) ulimit –c 10000000000(bash) ulimit –c 10000000000
Traces: ptrace(1) strace(1)Traces: ptrace(1) strace(1)
Top : display list of process with CPU usageTop : display list of process with CPU usage
Kill -9 pid = kill one of your processesKill -9 pid = kill one of your processes
findfind
Grep = find in file (grep terlala *.h */*.h)Grep = find in file (grep terlala *.h */*.h)
/usr/include = where most of the include are/usr/include = where most of the include are
/usr/bin = where most exe are/usr/bin = where most exe are
Vi commands for Vi commands for programming (1/2)programming (1/2)
~/.vimrc = commands that run on each new vi ~/.vimrc = commands that run on each new vi sessionsession
<esc>:set nu = line numbers<esc>:set nu = line numbers
<esc>:set ai = auto ident<esc>:set ai = auto ident
<esc>:set ts=4 = tabstop=4<esc>:set ts=4 = tabstop=4
<esc>:set sw=4 = shiftwidth=4<esc>:set sw=4 = shiftwidth=4
>> << = ident one shift width inside or outside>> << = ident one shift width inside or outside
sy on = syntax enlightment on and offsy on = syntax enlightment on and off
Vi commands for Vi commands for programming 2/2programming 2/2
Name completion, jumping to function Name completion, jumping to function declaration /definition = man ctags(1)declaration /definition = man ctags(1)
map = create shortcut for command modemap = create shortcut for command mode
map! = create shortcut for insert modemap! = create shortcut for insert mode
r = read filer = read file
r! = read the output of execuabler! = read the output of execuable
sh = move to shellsh = move to shell
Interrupted system callInterrupted system call
Check chapter 10.5 in advanced Check chapter 10.5 in advanced programming in the unix environmentprogramming in the unix environment
Also do man select and check EINTRAlso do man select and check EINTR
Basicly if you get a signal such a SIGCHLD Basicly if you get a signal such a SIGCHLD select will fail. This is condition you have to select will fail. This is condition you have to check but you don’t have to die (actually you check but you don’t have to die (actually you should not die) should not die)
SyslogSyslog
In order for syslog to work you need to use In order for syslog to work you need to use LOG_LOCAL5 as facility (in openlog(3))LOG_LOCAL5 as facility (in openlog(3))
Because syslog didn’t work initially we Because syslog didn’t work initially we accepted file logging (but syslog is now accepted file logging (but syslog is now required for homework 2)required for homework 2)
The log file is at /var/log/messagesThe log file is at /var/log/messages