Beruflich Dokumente
Kultur Dokumente
System Programming
Files
1
File Access Methods
• Sequential Access
• Direct Access
File system
2
File system
3
Layered File System
Directories
4
Directory Structure
Files F1 F2 F3 F4
Fn
10
5
Single-Level Directory
• Naming problem
• Grouping problem
11
Two-Level Directory
• Separate directory for each
user
12
6
Multi-Level Directory
• Efficient searching
• Grouping Capability
13
14
7
Directory Implementation
15
8
File System Structures
• In memory: (volatile)
• Info is used for file-system management and to
improve performance
• Partition table
• Directory structures of recently accessed
directories
• System-wide open-file table, which includes a
copy of the FCB of each open file
• Per-process open-file table, which contains a
pointer to the appropriate FCB in the system-
wide open-file table
17
File Permissions
File Dates (create, access, write)
File Owner, Group
File Size
File Data Blocks
18
9
Open-File Table
20
10
File System Operations (open a file)
21
22
11
Allocation Methods
23
Contiguous Allocation
24
12
Contiguous Allocation
26
13
Linked Allocation
27
Linked Allocation
14
File-Allocation Table
29
15
Indexed Allocation
31
Indexed Allocation
32
16
Indexed Allocation
33
• Linked scheme
• Have as many index block as needed to
accommodate a file
• Last pointer in the current index block points to the
next index block
34
17
Index blocks and big files
• Multilevel index
• Use the first-level index block to point to a set of
second-level index blocks
• Second-level index block point to the file blocks
• This approach could continue to a third of fourth
level
• 4 KB blocks can store 1 K of 4B pointers in an
index block Two level of indexes allow 1M 4B
pointers 4GB file
35
Multilevel index
outer-index
18
Index blocks and big files
• Combined scheme
• Use a combination of direct blocks and indirect
blocks
• The indirect blocks may use multilevel index
• UNIX use direct blocks, indirect blocks, double
and triple indirect blocks
37
38
19
Free-Space Management
• Bit vector (n blocks)
678
1 ⇒ block[i] free
bit[i] =
0 ⇒ block[i] occupied
39
40
20
Standard ‘C’ File Read and Write
Opening Files
42
21
Closing Files
if (!fclose(f_readwrite))
{ perror("Failed closing file'/usr/local/lib/db/users':");
exit(1);
}
43
44
22
Writing to a File
int c;
char buf[201];
/* write the character 'a' to the given file. */ c = 'a';
fputc(c, f_readwrite);
/* write the string "hello world" to the given file. */
strcpy(buf, "hello world");
fputs(buf, f_readwrite);
/* write the string "hi there, mate" to the (screen) */
fprintf(stdout, "hi there, mate\n");
/* write out any buffered writes to the given file stream. */
fflush(stdout);
45
46
23
Moving Read/Write Location (other functions)
#include <stdio.h>
47
48
24
File Descriptors
49
Creating Files
50
25
Creating Files
/usr/include/sys/stat.h:
51
Opening Files
• int open(char *filename, int flag, int perms)
52
26
File Operations
• int close(int handle)
• int read(int handle, char *buffer, unsigned
length)
• int write(int handle, char *buffer,unsigned
length)
• Length: is used to specify the number of bytes to be read or
written into a file
• The sizeof() is commonly used to specify the length
53
Example
54
27
Example
main(int argc, char **argv)
{
int fd;
int bytes_read;
int file_length;
if ( (fd = open(argv[1],O_RDONLY)) = -1)
{ /* error file not open */.... perror("Datafile");
exit(1); }
if ( (bytes_read = read(fd,&file_length, sizeof(int))) ==
-1)
{ /* error reading file */... exit(1); }
if ( file_length > 999 ) {/* file too big */ ....}
if ( (bytes_read =
read(fd,bigbuff,file_length*sizeof(float))) == -1)
{ /* error reading open */... exit(1); }
}
55
fsync()
56
28
access()
57
58
29
Reading the Contents of a Directory
• DIR structure for directory reading is what the FILE
structure is for files
• contains information used by other calls to read the
contents of the directory
• When reading the contents of a directory, we need to
open a directory (returns a DIR structure).
• The data regarding a directory entry is returned in a
dirent structure.
• The only relevant field in this structure is d_name,
• A null-terminated character array, containing the name of
the entry (a file or a directory).
59
When we are done reading from a directory, we can close it using the
closedir() function:
if (closedir(dir) == -1)
{
perror("closedir"); exit(1);
}
60
30
Reading The Contents Of A Directory
/* this structure is used for storing the name of each entry in turn.
*/
struct dirent* entry;
/* read the directory's contents, print out the name of each entry.
*/
printf("Directory contents:\n");
while ( (entry = readdir(dir)) != NULL)
{
printf("%s\n", entry->d_name);
}
61
31