Download presentation
Presentation is loading. Please wait.
Published byRosamond Smith Modified over 9 years ago
1
1 IT 252 Computer Organization and Architecture Interaction Between Systems: File Sharing R. Helps
2
2 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)
3
3 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)
4
4 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
5
5 Open or Close files #include int fd; 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 *fp used 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() );
6
6 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
7
fd 0 fd 1 fd 2 fd 3 fd 4 Descriptor table (one table per process) Open file table (shared by all processes) v-node table (shared by all processes) File pos refcnt=1... File pos refcnt=1... stderr stdout stdin File access... File size File type File access... File size File type File A File B Data structure for open files
8
8 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?
9
9 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
10
fd 0 fd 1 fd 2 fd 3 fd 4 Descriptor table (one table per process) Open file table (shared by all processes) v-node table (shared by all processes) File pos refcnt=1... File pos refcnt=1... stderr stdout stdin File access... File size File type File A File B File Sharing
11
11 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?
12
fd 0 fd 1 fd 2 fd 3 fd 4 Descriptor table (one table per process) Open file table (shared by all processes) v-node table (shared by all processes) File pos refcnt=0... File pos refcnt=2... File access... File size File type File access... File size File type File A File B ??
13
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 13
14
fd 0 fd 1 fd 2 fd 3 fd 4 Descriptor tables Open file table (shared by all processes) v-node table (shared by all processes) File pos refcnt=2... File pos refcnt=2... Parent's table fd 0 fd 1 fd 2 fd 3 fd 4 Child's table File access... File size File type File access... File size File type File A File B Child inherits Parent’s Open Files
15
15 Parent-child sharing /* pp 10.3*/ #include "csapp.h" int main() { int fd; 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?
16
Sharing without child process File descriptors can be duplicated Share same open file description Share file offset data Use dup() or dup2() 16
17
17 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?
18
Unix I/O functions (accessed via system calls) Standard I/O functions C application program fopen fdopen fread fwrite fscanf fprintf sscanf sprintf fgets fputs fflush fseek fclose open read write lseek stat close rio_readn rio_writen rio_readini tb rio_readlin eb rio_readnb RIO functions Memory-mapped I/O
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.