Interlude: File and Directories Flashcards

1
Q

Persistent memory

A

Hard disk drives, solid-state storage devices stores memory permanently and keeps memory intact even without power.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Two key abstractions in the virtualization of storage: file and directory.

A

File: linear array of bytes, each of which you can read or write. Each file has an inode number.
Directory: Also contains inode number, contains a list of pairs (user readable name, low level name), each entry in a directory refers files or other directories. Can make a directory tree or directory hierarchy.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Creating files

A

open system call and passing in the O_CREAT flag
example(
int fd = open(“foo”, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

File descriptor

A

integer, private per process and used in UNIX systems to access files

  • read or write with the file descriptor
  • fd is a capability, an opaque handle that gives you power to perform certain operations
  • can think of it as pointer to an object of type file
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

->echo hello > foo
->cat foo
hello

A
open("foo", O_RDONLY|O_LARGEFILE) = 3 //open for reading and 64 bit offset? returns 3 because each running process already has three files open: stdin, stdout and sterr
read(3, "hello\n", 4096) = 6 //
write(1, "hello\n", 6) = 6
hello
read(3, "", 4096) = 0
close(3)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Open File Table

A

each process maintains an array of file descriptors, each of which refers to an entry in the system-wide open file table.
each entry in this table tracks which underlying file the descriptor refers to, the current offset, and other details like if the file is readable and/or writable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

read() and write() vs lseek()

A

read and write will update the current offset

processes can use lseek() to change the value and enable random access to different parts of the file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
lseek() system call
function prototype: off_t lseek(int fildes, off_t offset, int whence);
A

first argument: file descriptor
second: offset, which positions the file offset to a particular location within the file
third: whence, determines how the seek is performed
= SEEK_SET, offset = offset bytes
= SEEK_CUR, offset = currentlocation + offsetbytes
= SEEK_END, offset = size of the file + offset bytes
- calling lseek() does NOT perform a disk seek, or when a read or write issued to the disk is not on the same track as the last read or write and thus necessitates a head movement
- lseek only changes a variable in OS memory that tracks at which offest its next read or write will start

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

“current” offset for each file a process opens

A
  • abstraction of an open files is that is has a current offset
  • updated in two ways:
    implicitly: when a read or write of N bytes takes place, N is added to the current offset
    explicitly: lseek which changes the offset as specified above
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
struct file {
int red;
char readable;
char writable;
struct inode *ip;
uint off;
}
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Shared File Table Entries: fork()

A

parent process creates a child process with fork() and child adjust the current offset with a call to lseek() and then exits.
when a file table entry is shared, its reference count is incremented, only when both processes close the file or exit will the entry be removed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Shared File Table Entries: dup()

A

dup() call allows a process to create a new file descriptor that refers to the same underlying open file as an existing descriptor
- useful when writing a UNIX shell and performing operations like output redirection

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Renaming Files

A

mv command in command line

  • using strace on mv: it uses system call rename(charold, charnew)
  • rename call is implemented as an atomic call, file is either old or new and no inbetween state
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Meta data

A
  • information about each file it is storing
  • to see the metadata for a certain file, we can use stat() or fstat() system calls
  • stat() takes a file name or a file descriptor and fill in a stat structure
  • command line stat to see
  • files keep this type of information in a structure called inode, or a persistent data structure kept by the file system that has metadata
  • all inodes reside on disk, copy of active ones are usually cached in memory to speed up access
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Removing files

A

-rm uses unlink(“name of file”) = 0 system call

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Making Directories

A
  • mkdir system call
  • can never write to a directory directly, format of directory is considered file system metadata and thus you can only update directory indirectly by creating files, directories inside of it
  • an empty directory has two entries, one entry that refers to itself (.) and one that refers to its parent (..)
17
Q

MMap() and persistent memory

A
  • mmap() system call creates a correspondence between byte offsets in a file and virtual addresses in the calling process; former called backing file and the latter its in-memory image
  • when mapped to a process address space, the file can be accessed like an array in the program
    mmap(address, length, protect, flags, fildes, offset)
18
Q

Reading Directories

A

-ls reads directories
opendir(), readdir(), closedir()
-use a loop to read directory entries one at a time (struct dirent)
- directories are light on informations, basically mapping the name to the inode number, to get more information on a each file, call stat() on each file (this is what happens when you pass the -l flag)

19
Q

Deleting Directories

A
  • rmdir()
  • dangerous, potentially delete large amount of data with a single command
  • rmdir() has requirement that a directory be empty (has “.” and “..” entries before it is deleted)
  • trying to delete a non-empty directory with rmdir() will fail
20
Q

Hard Links

A
  • link() system call makes an entry in the file system tree
  • link( old path name, new path name): you link a new file name to an old one and create another way to refer to the same file
  • refers to the same inode number of the original file, it is not copied, instead we have two readable names that both refer to the same file
21
Q

Link() and unlink()

A
  • When you create a file, you are 1. making a structure (inode) that will track all relevant information abut the file, it’s size, etc
    2. Linking a human readable name to that file and putting that link into a directory
  • When a file system unlinks a file, it checks the reference count (link count )within the inode number, only when the reference count is 0 does the file system free the inode and data blocks and you truly “delete” the file
22
Q

Symbolic Links, or soft links

A
  • hard links are limited, cannot create one to a directory (fear that you will create a cycle in the directory tree), cannot hard link to files in other disk partitions (inode numbers are only unique within a particular file system, not across file systems)
  • symbolic link: ln -s
  • behavior is same
  • symbolic link is actually a file itself or a different type
  • holds the pathname of the linked-to file as the data of the link file
23
Q

Dangling reference

A
  • if you remove the file that another file was pointing to through a symbolic link, it will no longer exists
24
Q

Permission bits

A

-mechanisms to enable and disable sharing
-rudimentary form of controls are the permission bits
Permissions consist of three groupings
1. what the owner of the file can do to it
2. what someone in a group can do to the file
3. what anyone (other) can do to it

25
Q

Access Control Lists

A

Allow for more precise control over exactly can access and manipulate information

26
Q

chmod command

A
  • owner of the file can change the file mode using chmod
  • to remove the ability for anyone except the owner to access the file, you can do chmod 600 name.txt
  • enables the readable bit (4) and the writable bit (2) for the owner (rw——–)
27
Q

Execute bit for directories

A
  • enables a user to do things like change directories cd into a given directory, or create files in it
28
Q

Assemble a full directory tree from underlying file systems

A
  • ## Make a file system: mkfs, writes an empty file system starting with a root directory
29
Q

Time of Check to Time of Use

A
  • gap between the check and the update can lead to invalid operations being performed by the control program
30
Q

Mount()

A
  • makes the file system accessible within the uniform file-system tree
  • takes an existing directory as a target mount point and paste a new file system onto the directory tree at that point