0% found this document useful (0 votes)
12 views17 pages

22 fileIO

Uploaded by

manjunathsg85
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views17 pages

22 fileIO

Uploaded by

manjunathsg85
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

File System Implementation

CS 241 Lecture 22
R: Ch 4 pp92-116

Roy Campbell

08/13/24 CS241 © 2005 Roy Campbell, All R 1


ights Reserved
Contents
 V& UNIX example
 Writing POSIX Code for File Read…
 Tables for Files

08/13/24 CS241 © 2005 Roy Campbell, All R 2


ights Reserved
The UNIX V7 File System (1)

A UNIX V7 directory entry


08/13/24 CS241 © 2005 Roy Campbell, All R 3
ights Reserved
The UNIX V7 File System (2)

A UNIX i-node

08/13/24 CS241 © 2005 Roy Campbell, All R 4


ights Reserved
The UNIX V7 File System (3)

The steps in looking up /usr/ast/mbox

08/13/24 CS241 © 2005 Roy Campbell, All R 5


ights Reserved
UNIX I/O R:Ch 4 pp92-116
 Device drivers
 Pseudodevice drivers
 Open, close, read,write, ioctl
 Special files /dev
 Block special file – disk like
 Character special file – stream of
chars

08/13/24 CS241 © 2005 Roy Campbell, All R 6


ights Reserved
Sequential Files
#include <unitstd.h>
ssize_t read(int fildes, void *buf, size_t nbyte);

Returns number of bytes read in buf-


*buf must be initialized.
read on end-of-file char returns 0
read on pipe may return < nbytes

08/13/24 CS241 © 2005 Roy Campbell, All R 7


ights Reserved
Standard files
 stdin, stdout, stderr (fd = 0, 1, 2)
 End of line char is “\n”.

08/13/24 CS241 © 2005 Roy Campbell, All R 8


ights Reserved
Sequential write operation
#include <unistd.h>
ssize_t write(int fildes, const void *buf, size_t nbyte);
•Returns number of bytes actually written
•Can only write to end of file (or file truncates)

read(STDIN_FILENO, buf, BLKSIZE);


write(STDOUT_FILENO, buf, BLKSIZE);

May fail!!!

08/13/24 CS241 © 2005 Roy Campbell, All R 9


ights Reserved
Copyfile
#include <errno.h>
#include <unistd.h>
#define BLKSIZE 1024
int copyfile(int fromfd, int tofd) {
char *bp;
char buf[BLKSIZE];
int bytesread;
int byteswritten = 0;
int totalbytes = 0;
for ( ; ; ) {
while (((bytesread = read(fromfd, buf, BLKSIZE)) == -1) &&
(errno == EINTR)) ; /* handle interruption by signal */
if (bytesread < 0) /* real error or end-of-file on fromfd */
break;
bp = buf;

08/13/24 CS241 © 2005 Roy Campbell, All R 10


ights Reserved
Copyfile
while (bytesread > 0) {
while(((byteswritten = write(tofd, bp, bytesread)) == -1 ) &&
(errno == EINTR)) ; /* handle interruption by signal */
if (byteswritten <= 0) /* real error on tofd */
break;
totalbytes += byteswritten;
bytesread -= byteswritten;
bp += byteswritten;
}
if (byteswritten == -1) /* real error on tofd */
break;
}
return totalbytes;
}

08/13/24 CS241 © 2005 Roy Campbell, All R 11


ights Reserved
Use the restart library for
read/write P99 – e.g. r_write
ssize_t r_write(int fd, void *buf, size_t size) {
char *bufp;
size_t bytestowrite;
ssize_t byteswritten;
size_t totalbytes;
for (bufp = buf, bytestowrite = size, totalbytes = 0;
bytestowrite > 0;
bufp += byteswritten, bytestowrite -= byteswritten) {
byteswritten = write(fd, bufp, bytestowrite);
if ((byteswritten) == -1 && (errno != EINTR))
return -1;
if (byteswritten == -1)
byteswritten = 0;
totalbytes += byteswritten;
}
return totalbytes;
}
08/13/24 CS241 © 2005 Roy Campbell, All R 12
ights Reserved
It makes programs simpler!
#include <unistd.h>
#include "restart.h"
#define BLKSIZE 1024
int copyfile(int fromfd, int tofd) {
char buf[BLKSIZE];
int bytesread, byteswritten;
int totalbytes = 0;
for ( ; ; ) {
if ((bytesread = r_read(fromfd, buf, BLKSIZE)) <= 0)
break;
if ((byteswritten = r_write(tofd, buf, bytesread)) == -1)
break;
totalbytes += byteswritten;
}
return totalbytes;
}
08/13/24 CS241 © 2005 Roy Campbell, All R 13
ights Reserved
Use readblock to read #bytes
 Use it to read structures
 Either works or returns error

08/13/24 CS241 © 2005 Roy Campbell, All R 14


ights Reserved
Opening and Closing Files
 Open associates a file descriptor with
a file or physical device
 File descriptor is an index into a table
per process and can be inherited to
child processes allowing sharing
 Should close open files
 Oflag access modes and status

08/13/24 CS241 © 2005 Roy Campbell, All R 15


ights Reserved
UNIX file structure implementation
Parent Open file description inode
File descriptor File position Mode
table R/W Link Count
Pointer to inode
File position UID
Child R/W GID
File Pointer to inode File size
descr
Times
iptor
table Address of
first 10
disk blocks
Single Indirect
Double Indirect
Unrelated process Triple Indirect
File descriptor table
08/13/24 CS241 © 2005 Roy Campbell, All R 16
ights Reserved
Summary
 V& UNIX example
 Writing POSIX Code for File Read…
 Tables for Files

08/13/24 CS241 © 2005 Roy Campbell, All R 17


ights Reserved

You might also like