170 likes | 184 Views
IT 252 Computer Organization and Architecture. Interaction Between Systems: File Sharing. R. Helps. System Interaction. As usual this can happen on many levels Motherboard level: through buses OS and app level: Files and file-sharing Distributed system level: Networking Databases
E N D
IT 252Computer Organizationand Architecture Interaction Between Systems: File Sharing R. Helps
System Interaction • As usual this can happen on many levels • Motherboard level: through buses • OS and app level: Files and file-sharing • Distributed system level: • Networking • Databases • Cloud • Useful form of interaction is through shared file access • This discussion will focus on file access (chapter 10 of CS:APP)
Sidenote: “Cloud” Basics • “Cloud” = remote shared computer services • Many existing technologies such as remote storage systems and remote databases are now labeled cloud services • Term “cloud” is generic and is being defined by marketplace • There are a few general types of cloud services • IaaS (Infrastructure as a Service) • Remote physical or virtual machines • Managed completely by the customer • PaaS (Platform as a Service) • Remote system includes OS, database, web server (typically) • Service provider maintains OS etc. (updates (etc) • Customer develops on platform • SaaS (Software as a Service) • Application packages provided as a service • Other variants available • General benefits of cloud: remote access, scale computing resources up and down very quickly and relatively cheaply (need extra servers for one week—rent from cloud)
Files and IO • UNIX sees a file as a series of bytes • All IO in UNIX is seen as a file • Includes keyboards, displays, network connections, motors, sensors and anything else. • Some files can only be read (eg sensor, keypad) • Some files can only be written (eg display) • To use a file it must be opened, read, written, closed
Open or Close files #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> intfd; fd = open(char *filename, int flags, mode_t mode); close(fd); } • Returns fd, an integer, or (-1) if fail • fd = file descriptor • Different from file pointer FILE *fpused with fopen • Points to a file in a process table • Filename is text filename • Flags are Read, Write, RW • Mode defines permissions (user, group, other) (use umask());
File Descriptors and read/write • File descriptors are allocated on open() and are reclaimed on close() • File descriptor point to file table • Once file is opened it can be read or written with read() & write() • Read()/write() keeps a pointer to current place in the file • If time discuss stat() and fstat() (file metadata) p 873 text
Data structure for open files Open file table (shared by all processes) v-node table (shared by all processes) Descriptor table (one table per process) File A stdin File access fd 0 stdout fd 1 File size File pos stderr fd 2 File type refcnt=1 fd 3 ... ... fd 4 File B File access File size File pos File type refcnt=1 ... ...
File Descriptor assignment /* pp 10.1*/ #include "csapp.h" int main() { int fd1, fd2; fd1 = open(“foo.txt”, O_RDONLY, 0); close(fd1); fd2 = open(“baz.txt”, O_RDONLY, 0); printf(“fd2 = %d\n”, fd2); exit(0); } • What is the output?
File Sharing • Multiple descriptors can point to same file • File are accessed through three data structures • Descriptor Table • File Table • V-node table • See 10.6 for details
File Sharing Open file table (shared by all processes) v-node table (shared by all processes) Descriptor table (one table per process) File A stdin File access fd 0 stdout fd 1 File size File pos stderr fd 2 File type refcnt=1 fd 3 ... ... fd 4 File B File pos refcnt=1 ...
File Sharing /* pp 10.2*/ #include "csapp.h" int main() { int fd1, fd2; char c; fd1 = open(“foobar.txt”, O_RDONLY, 0); fd2 = open(“foobar.txt”, O_RDONLY, 0); read(fd1, &c, 1); read(fd2, &c, 1); printf(“c = %c\n”, c); exit(0); } • Assume foobar.txt contains six characters “foobar”. What is the output?
Child-Parent fork • An app can fork a child process using fork() • The child process inherits the parent’s descriptor table • Has access to parent’s data
Child inherits Parent’s Open Files Descriptor tables Open file table (shared by all processes) v-node table (shared by all processes) Parent's table File A File access fd 0 fd 1 File size File pos fd 2 File type refcnt=2 fd 3 ... ... fd 4 Child's table File B File access fd 0 File size fd 1 File pos fd 2 File type refcnt=2 fd 3 ... ... fd 4
Parent-child sharing /* pp 10.3*/ #include "csapp.h" int main() { intfd; char c; fd = open(“foobar.txt”, O_RDONLY, 0); if (fork() == 0) { read(fd, &c, 1); exit(0) } wait(NULL); read(fd, &c, 1); printf(“c = %c\n”, c); exit(0); } • Assume foobar.txt contains six characters “foobar”. What is the output?
Sharing without child process • File descriptors can be duplicated • Share same open file description • Share file offset data • Use dup() or dup2()
Dup() /* pp 10.5*/ #include "csapp.h" int main() { int fd1, fd2; char c; fd1 = open(“foobar.txt”, O_RDONLY, 0); fd2 = open(“foobar.txt”, O_RDONLY, 0); read(fd2, &c, 1); dup2(fd2, fd1); read(fd1, &c, 1); printf(“c = %c\n”, c); exit(0); } • Assume foobar.txt contains six characters “foobar”. What is the output?
Memory-mapped I/O fopenfdopen freadfwritefscanffprintfsscanfsprintffgetsfputsfflushfseek fclose C application program rio_readn rio_writen rio_readinitb rio_readlineb rio_readnb Standard I/O functions RIO functions Unix I/O functions (accessed via system calls) open read write lseek stat close