Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 PART II System Programming. 2 Chapter 10 The File and I/O System.

Similar presentations


Presentation on theme: "1 PART II System Programming. 2 Chapter 10 The File and I/O System."— Presentation transcript:

1 1 PART II System Programming

2 2 Chapter 10 The File and I/O System

3 3 The System architecture of UNIX Hardware CPU, Memory, Disk, Peripherals Kernel - Process management - File management - Memory management - Device management System call the programmer's functional interface to the UNIX kernel Commands, Utilities, Application programs kernel services using library routines or system calls

4 4 System Calls

5 5 Objectives describe the characteristics of a file open and close a file read and write data to and from a file change the read/write position in a file control a file

6 6 What is a file ? l a file is a contiguous sequence of bytes l no format imposed by the operating system l each byte is individually addressable in a disk file l a file is also a uniform interface to external devices

7 7 System Call :File Descriptor l File descriptor – open() returns a fd, an integer value – used in subsequent I/O operations on that file – close(fd) closes that file described by fd – all of a process's open files are automatically closed when it terminates

8 8 System Call :File Descriptor l file descriptor : 0 ~ 19

9 9 System Call :Open

10 10 System Call : Examples – open a file for reading fd = open("account",O_RDONLY); – open a file for writing fd = open("tmpfile", O_WRONLY|O_CREAT|O_TRUNC,0600); – open a file for appending fd = open("/sys/log”,O_WRONLY|O_APPEND|O_CREAT,0600); – open a file for reading and writing fd = open(argv[1], O_RDWR); – create a new file for writing if ((fd = open("tmpfile", O_WRONLY|O_CREAT|O_EXCL, 0666))==-1)

11 11 System Call :Read/Write l Reading from a file int read(int fd, char* buf, int count); l Writing to a file int write(int fd, char* buf, int count); l Examples charsRead = read(fd,buffer,BUFFER_SIZE); if (charsRead == 0) break; /* EOF */ if (charsREad == -1) fatalError(); /*error */ charsWritten = write(tmpfd,buffer,charsRead); if (charsWritten != charsRead) fatalError();

12 12 System Call :lseek l Moving file position long lseek(int fd, long offset, int mode);

13 13 System Call :lseek l Examples - rewind : lseek(fd, 0L, 0); - append : lseek(fd, 0L, 2); - update read(fd, (char *) &record, sizeof(record)); lseek(fd, (long) -sizeof(record), 1); write(fd, (char *) &record, sizeof(record)); - record position : loc = lseek(fd, 0L, 1); - increase file lseek(fd, (long) MAX*sizeof(record), 2); write(fd, (char *) &reocrd, sizeof(record));

14 14 System Call :lseek main() { int i, fd; fd = open(“sparse.txt”, O_CREAT|O_RDWR,0600); write(fd, “sparse”, 6); lseek(fd, 60006, L_SET); write(fd, “file”, 4); close(fd); fd = open(“normal.txt”, O_CREAT|O_RDWR,0600); write(fd, “mormal”, 6); for (i=1; i<=60000,6) write(fd, “/0”, 1); write(fd, “file”, 4); close(fd); } /* Create a sparse file */ /* Create a normal file */

15 15 Example : Copy input to ouput main() { char buf[BUFSIZE]; int n; while((n=read(0, buf, BUFSIZE)) >0) write(1, buf, n); exit(0); }

16 16 Example : Copy Files #include main(argc, argv) int argc; char *argv[ ]; { int fdin, fdout, n; char buf[BUFSIZE]; if (argc != 3) { fprintf(stderr, "Usage: %s filein fileout\n", argv[0]); exit(1); } if ((fdin = open(argv[1], O_RDONLY)) == -1) { perror(argv[1]);exit(2); } if (( fdout=open(argv[2],O_WRONLY|O_CREAT|O_TRUNC, 0644))==-1) { perror(argv[2]);exit(3);} while ((n = read(fdin, buf, BUFSIZE)) > 0) write(fdout, buf, n); exit(0); }

17 17 Example: Creating an employee file #include #include "employee.h” main(argc, argv) int argc; char *argv[ ]; { int fd, open(), getpid(); struct employee record; if (argc < 2) { fprintf(stderr, "Usage: %s file\n", argv[0]); exit(1); } if ((fd = open(argv[1],O_WRONLY |O_CREAT|O_EXCL, 0640))==-1) { perror(argv[1]); exit(2); }

18 18 Example: Creating an employee file for (;;) { printf("Enter employee name salary:"); scanf("%s", &record.name); if (record.name[0] == '.') break; scanf("%d", &record.salary); record.pid = getpid(); write(fd, (char *) &record, sizeof(record) ); } close(fd);exit(0); }

19 19 Example: Creating an employee file employee.h #define NAMESIZE 24 struct employee { char name[NAMESIZE] int salary; int pid; }

20 20 Example: Querying the employee file #include #include "employee.h” main(argc, argv) int argc; char *argv[ ]; { int fd, recnum; struct employee record; if ((fd = open(argv[1], O_RDONLY)) == -1) { perror(argv[1]); exit(2); }

21 21 Example: Querying the employee file for (; ; ) { printf(“\n Enter record number :”); scanf(“%d”, &recnum); if (recnum < 0) break; lseek(fd, (long)recnum*sizeof(record), 0); if (read(fd, (char *) &record, sizeof(record))>0) printf(“Employee:%s\tSalary:%d\n”, record.name, record.salary); else printf(“Record %d not found\n”, recnum); } close(fd); exit(0); }

22 22 Example: reverse \n\n \n\n\n\n File LineStart... 0 1 2 3 n \n\n 3075102156 0 30 75 102

23 23 Example: reverse l Step 1 – Parse command line – parseCommandLine, processOptions l Step 2 – open input file for reading – pass1 l Step 3 – read from file, storing the starting offset of each line in an array – pass1, trackLines l Step 4 – read the input file again backward, copying each line to stdout – reverse the line if the -c option was chosen

24 24 Get File Status #include int stat(path, buf) int fstat(fd, buf) char *path; int fd; struct stat *buf struct stat { dev_t st_dev; /* disk device */ ino_t st_ino; /* inode number */ ushort st_mode; /* permission */ short st_nlink; /* the no. of links */ ushort st_uid; /* uid of owner */ ushort st_gid; /* gid of owner */ dev_t st_rdev; /* for special files */ off_t st_size; /* file size */ time_t st_atime; /* access time */ time_t st_mtime; /* modification time */ time_t st_ctime; /* file status change */ }; type specialpermission 439 st_mode

25 25 Example : Display File Status #include main(argc,argv) int argc; int argv[]; { struct stat stbuf; int stat(); if (stat(argv[1], &stbuf) == -1) { perror(argv[1]); exit(1); } printf(“file name: %s\t\t”, argv[1]); printf(“device: %d\t”, stbuf.st_dev); printf(“i-number : %d\n”,stbuf.st_ino); prntmode(&stbuf); printf(“links: %d\t”, stbuf.st_nlink); printf(“file size: %ld\n”, stbuf.st_size); prntuser(&stbuf); prnttimes(&stbuf); exit(0); }

26 26 Example : Display File Status #include main(argc,argv) int argc; int argv[]; { struct stat stbuf; int stat(); if (stat(argv[1], &stbuf) == -1) { perror(argv[1]); exit(1); } printf(“file name: %s\t\t”, argv[1]); printf(“device: %d\t”, stbuf.st_dev); printf(“i-number : %d\n”,stbuf.st_ino); prntmode(&stbuf); printf(“links: %d\t”, stbuf.st_nlink); printf(“file size: %ld\n”, stbuf.st_size); prntuser(&stbuf); prnttimes(&stbuf); exit(0); }

27 27 Change mode of file l int chmod(path, mode) char *path; int mode; l Exmaple : %setmode 755 file #include main(argc, argv) int argc; char *argv[]; { long strtol( ); int newmode; newmode = (int) strtol(argv[1],(char **) NULL, 8); if (chmod(argv[2], newmode) == -1) { perror(argv[2]); exit(1); } exit(0); }

28 28 Change owner and group of a file l int chown(path, owner, group) char *path; int owner, group; l Exmaple % setown file1 file2

29 29 Change owner and group of a file #include main(argc, argv) int argc; char *argv[]; { struct stat stbuf; int stat( ), chown( ); if (argc < 3) { printf(“Usage: %s other-file your-file\n”,argv[0]); exit(1); } if (stat(argv[1], &stbuf) == -1) { perror(argv[2]); exit(2); } if (chown(argv[2], stbuf.st_uid, stbuf.st_gid) == -1) { perror(argv[2]); exit(3); } exit(0); }

30 30 Set File Access and Modification Time #include int utime(path, times) char *path; struct utimebuf *times; l Example %settime - file %settime file1 file2

31 31 Example : Changing File Times #include struct utimbuf { time_t actime; time_t modtime; } main(argc, argv) int argc; char *argv[ ]; { int stat(), utime(); struct stat stbuf; struct utimbuf timestamp, *times=&timestamp;

32 32 Example : Changing File Times if (argv[1][0] != ‘-’) { if (stat(argv[1], &stbuf) == -1) { perror(argv[1]); exit(2); } times->actime = stbuf.st_atime; times->modtime = stbuf.st_mtime; } else times = (struct utimbuf *) NULL; if (utime(argv[2], times) == -1) { perror(argv[2]); exit(3); } exit(0); }

33 33 What is a Directory l Used to organize regular, pipe, special files and other directory l format imposed by Unix l file names are contained only in directory entries l permissions – r:readable – w:writable – x: searchable

34 34 Directories and Files Directory i-list name36 inode data blocks

35 35 Direct #define DIRSIZ 14 struct direct { ino_t d_ino; char d_name[DIRSIZ]; };

36 36 System Call : Make/Remove Directory l make a directory int mkdir(path, mode) char *path; int mode; l remove a directory int rmdir(path) char *path; main() { mkdir(“/tmp/D”, 0750); system(“ls -ld /tmp/D”); rmdir(“/tmp/D”); system(“ls -ld /tmp/D”); }

37 37 Example: List Directory Contents #include main(argc,argv) int argc; char *argv[]; { int dirfd; struct direct dentry; static char filename[DIRSIZ+1]; struct stat stbuf; if ((dirfd = open(“.”, O_RDONLY))==-1) { perror(“.”); exit(1); }

38 38 Example: List Directory Contents while (read(dirfd, (char *) &dentry, sizeof(struct direct)) > 0) { if (dentry.d_ino == 0) continue; memcpy(filename, dentry.d_name, DIRSIZ); if (stat(filename, &stbuf) == -1) { perror(filename); break; } if ((stbuf.st_mode & S_IFMT) == S_IFREG) printf(“%-14s %d\n”, filename, stbuf.st_size); else printf(“%-14s\n”, filename); }

39 39 FILE STATUS: st_mode #define S_IFMT 0170000 /* type of file */ #define S_IFDIR 0040000 /* directory */ #define S_IFREG 0100000 /* regular file */ #define S_IFBLK 0060000 /* block special */

40 40 System Call: Change Directory l change working directory int chdir(path) char *path; l List Directory Contents : Improved... main(argc,argv) int argc; char *argv[]; { int dirfd; struct direct dentry; static char filename[DIRSIZ+1]; struct stat stbuf; if ((dirfd = open(argv[1], O_RDONLY))==-1|| chdir(argv[1]) == -1) { perror(“.”); exit(1); }...

41 41 Link to a File l Link to a file int link(path1, path2) char *path1; char *path2; main(argc, argv) int argc; char *argv[ ]; { int link(); if (link(argv[1], argv[2]) == -1) { exit(1); } exit(0) } i-list Directory1 path1 36 inode data blocks Directory2 path2 36

42 42 Unlink l int unlink(path) char *path; - remove directory entry main(argc, argv) int argc; char *argv[ ]; { int unlink( ); if (unlink(argv[1]) == -1{ perror(argv[1]); exit(1); } exit(0); }

43 43 Internals of File Systems

44 44 System Calls Process System call interface File systemIPCProcess Management

45 45 User and Kernel Mode result=open(“/usr/glass/file.txt”, O_RDONLY); open(char *name, int mode) { <Execute trap instruction, switching to kernel code > } User process Kernel Address of kernel open() Address of kernel write() Address of kernel close() kernel code for open() {... } Kernel system call code C runtime library User code

46 46 Block I/O I/O is always done in terms of blocks Sequence of a read() system call

47 47 Inode (Index node) what is an inode ? – information about each file – a file has its inode Inode information – the type of the file regular, directory, block special, character special, etc – the size of the file – file permissions – the owner and group ids – the last modification and last access times – if it's a regular or directory, the location of blocks – if it's a special file, device numbers – if it's a symbolic link, the value of the symbolic link

48 48 The block map direct block pointers indirect block pointer an indirect block to address 1024 blocks double indirect block pointer an double indirect block to contain 1024 indirect block pointers How many blocks can be addressed ?

49 49 Block map Direct block pointers Indirect pointers Double indirect pointers... to blocks 0.. 9 To blocks 10.. 1033 To blocks 1034.. 2057 Locates up to 1000 indirects InodeDisk blocks

50 50 File system layout Boot block boot code that is used when UNIX is first activated Super block information about the entire file system – the total number of blocks in the file system – the number of inodes in the inode free list – a bit map of free blocks : see Figure 11.21 – the size of block in bytes – the number of free blocks – the number of used blocks

51 51 File system layout Inode list – all the inodes associated with the files on the disk – each block in the inode list can hold about 40 inodes User blocks – for storing file blocks Bad blocks – several blocks that cannot be used

52 52 File system layout Logical disk layout... Boot block Super block Inodes User block User blocks... Inodes 1..40 Inodes 41..80 0 1 2 3 200 201

53 53 Directory 2 bin 3 usr 4 ls 5 cp 7 test.c 6 200 dr-xr-xr-x 201 dr-xr-xr-x 202 -r-xr-xr-x 203 -r-xr-xr-x 204,206-r-xr-xr-x.2..2 bin3 usr4.3..2 ls5 cp7.4..2 test.c6 ls executable test.c 1st block cp executable test.c 2nd block Block number Permissions 201 200 202 203 204 205 206 1 2 3 4 5 6 Inode number

54 54 Implementation of System Calls fd= open(“file”, O_RDONLY); Disk Fd array (in user area) Open file table Active inode table 0 1 2 3 Offset 0 write/read Copy of inode #21 Inode list User blocks

55 55 Implementation of System Calls File descriptor array – inside the user area of the user's process – array of up to twenty pointers to file table entries Open file table (in kernel) – the read/write permission flags specified in the open() system call – the current file position, set to 0 by default – a reference count – a pointer to the entry in the active inode table Active inode table(in kernel) – inode copied from file system including the i-number and device number to identifies files on disk uniquely – a reference count

56 56 Example read(fd, buf1, 100); – copy the first block from disk into an buffer – copy the first 100 bytes from the buffer into buf1 – offset <- 100 read(fd, buf2, 200); – copy the next 200 bytes from the buffer into buf2 read(fd, buf3, 5000); – copy the remaning (3796bytes) from the buffer to buf3 – copy the second block from disk into an buffer – copy the remainig(1204 bytes) from the buffer into buf3 write(fd, buf4, 100); write(fd, buf5, 4000);

57 57 Implementation of System Calls fd= open(“file”, O_RDONLY); Disk Fd array (in user area) Open file table Active inode table 0 1 2 3 Offset 0 write/read Copy of inode #21 Inode list User blocks

58 58 Implementation of System Calls lseek(fd, 3000, L_SET) Disk Fd array (in user area) Open file table Active inode table 0 1 2 3 Offset 3000 write/read Copy of inode #21 Inode list User blocks

59 59 Implementation of System Calls fd=open(“file”, O_RDONLY); Disk Fd array (in user area) Open file table Active inode table 0 1 2 3 Offset 3000 write/read Copy of inode #21 Inode list User blocks 4 Offset 0 write/read

60 60 Implementation of System Calls fd2 = dup(fd); Disk Fd array (in user area) Open file table Active inode table 0 1 2 3 Offset 3000 write/read Copy of inode #21 Inode list User blocks 4 fd fd2


Download ppt "1 PART II System Programming. 2 Chapter 10 The File and I/O System."

Similar presentations


Ads by Google