100 likes | 248 Views
CSC 552.201 - Advanced Unix Programming, Fall, 2008. Welcome back to UNIX System Programming! Monday, September 8, class 3. Before next class (Sept. 8). cp –pr ~parson/UnixSysProg ~/UnixSysProg chmod 700 ~/UnixSysProg ls –l ~/UnixSysProg
E N D
CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome back to UNIX System Programming! Monday, September 8, class 3
Before next class (Sept. 8) • cp –pr ~parson/UnixSysProg ~/UnixSysProg • chmod 700 ~/UnixSysProg • ls –l ~/UnixSysProg • For each of UnixSysProg/ forkexec redirectio multiplex • “cd” into directory, then “gmake clean test” • Examine and play with examples until you understand them. • Bring any questions to our next class.
select() system call example • ~parson/UnixSysProg/lecture2/multiplex • usage: multiplex INFILE [ INFILE . . . ] INFILE0 INFILE1 … select() INFILE0 INFILEn read()/write() loop • select() can block on a SET of file IO handles (includes pipes and sockets) until some are ready for IO. There is no need to poll (yuck)!
Directory system calls and libraries • int chdir(const char *path); • int fchdir(int fildes); • Comparable to shell’s “cd” in an executable process. • You can open() and directory, cannot read() or write() • Current process directory is global to all threads. • char *getcwd(char *buf, size_t size); • Comparable to shell’s “pwd” in an executable process. • BUT, the O.S. does not track symbolic link crossage!!! • Unlike shell’s “pwd,” getcwd() always returns hard path.
Directory access • DIR *opendir(const char *dirname); • DIR *fdopendir(int fildes); • struct dirent *readdir(DIR *dirp); • Very loosely similar to “ls” command. • ino_t d_ino; /* serial number */ char d_name[]; • void rewinddir(DIR *dirp); • void seekdir(DIR *dirp, long int loc); • int closedir(DIR *dirp);
File and directory status • int stat OR lstat(const char *restrict path, struct stat *restrict buf); • lstat returns data about a symbolic link, stat about the target file of a symbolic link • S_ISDIR, S_ISREG, S_ISFIFO, S_ISLNK, ... on buf.st_mode • S_I[RWX][USR][GRP][OTH], S_IRWX[UGO], S_IS[UG]ID • file size, number of links, and other attributes of the file • int access(const char *path, int amode); • R_OK | W_OK | X_OK | F_OK
UNIX file system, Fig. 5.3 in text inode size (in bytes) owner UID and GID relevant times (creation, modification, access) link and block counts permissions direct pointers to beginning file blocks single indirect pointer block of direct pointers double indirect pointer triple indirect pointer
Hard and soft links • int link(const char *existing, const char *new); • like “ln” command. • The non-directory files must be in same file system. • int unlink(const char *path); • like “rm” command • File not removed until after last hard link is gone. • int symlink(const char *name1, const char *name2); • Like “ln –s” command, a soft link may cross file systems.
UNIX 3C library file IO • These are buffered library functions, with limited formatting and parsing, that are counterparts to many level 2 system calls. • Declared in stdio.h and stdlib.h, an open file is represented by a FILE * opaque type. • fopen(), popen(), fclose(), fgets(), fputs(), fprintf(), fscanf() • Standard IO files and stdin, stdout and stderr.
Level 2 pipes and FIFOs • int pipe(int fildes[2]); • Like the shell command “|” operation. • Returns an open input on [0] and output on [1]. • This anonymous “file” is used for inter-process communication (IPC) across threads or fork/exec. • int mkfifo(const char *path, mode_t mode); • int mknod(const char *path, mode_t mode, dev_t dev); // mode includes S_IFIFO or S_IFREG • Shell “mkfifo” or “mknod … p”, can be used for barrier.