程序代写代做代考 C file system CSci 4061 Introduction to Operating Systems

CSci 4061 Introduction to Operating Systems
File Systems: Basics

File as Abstraction
• Container for related information • Named
• Associated attributes

Naming a File
creat/open (“path/name”, …);

Links: files with multiple names
Each name is an alias
#include
int link (const char *original_path,
const char *new_path)
cannot exist as a file already
link (“foo”, “bar”); // “bar” refers to file “foo” unlink (“bar”); // remove name “bar”
// if file is open by someone, will not actually get deleted until // all fd’s to it are closed

File Attributes: Access to metadata
#include
int fstat (int filedes, struct stat *buf)
int stat (const char *pathname,
struct stat *buf)
Structure contains file/directory info:
off_t st_size; // file size
nlink_t st_nlink; // links
mode_t st_mode; // type + permission time_t st_mtime; // last modification time
fcntl can also be used to set or get lower-level attrs

Exercise: Metadata
• Write a program that monitors a given file every minute and if the file size has changed, it outputs the new size to stdout
#include
int stat (const char *pathname,
struct stat *buf)
Structure contains file/directory level info: off_t st_size; // file size nlink_t st_nlink; // links

Storing File Meta-data: Unix inode
inode disk sector address
“map” for a file on-disk in-memory
ls –i shows inode #

Filesystem
• Directory is a file as well • it has an inode
• what are file contents?
• list of file_name, inode pairs
• Filesystem • Files
• Directories
• Free disk sectors • Root dir

Filesystem (cont’d) • On-disk organization
• inode for root dir of filesystem “/” stored in well-known sector on the disk
• inode for disk sector free-list also stored in a well-known sector on the disk
• These are stored in the superblock

Unix file types/modes
• Indicated by the first character in ls –l • – regular file
• d directory
• c character special file
• b block special file • p pipe
• s socket
• l symbolic link

File types
• Within stat structure: struct_t stat st; stat (“foo”, &st);
Macros:
int S_ISDIR (st.st_mode); int S_ISREG (st.st_mode); int S_ISSOCK (st.st_mode); …
P. 158

Another look at ls -l Example:
drwx-xr-x 3 jon fac 4066 Nov 2 09:14 st
file type # hard allocation links size

Filesystem semantics: Unix
• Two processes open the same file
• Reader sees most recent write
• One reader and one writer – run together
• File “foo” contains “aaaaaaaaaaaaaaaaaaaaaa”

Filesystem semantics (cont’d)
// reader.c
#include
#include
void main () {
int fd;
// writer.c
#include
#include
void main () { int fd;
}
char c, buf[100];
read (0, &c, 1);
fd = open (“foo”, O_RDONLY); n = read (fd, buf, 10); buf[n] = ‘\0’;
printf (“buf=%s\n”, buf);

char c, buf[100] = “bbbbbbbbbb…”;
read (0, &c, 1);
fd = open (“foo”, O_WRONLY);
write (fd, buf, 10);
read (0, &c, 1);
close (fd); }
What is the output for reader? Writer does write
Reader then does read
Does machine matter?

File permissions
Operations (r, w, x): read, write, execute
Subjects (u: user/owner, g: group, o: others)
Users may belong to any number of groups (type groups at the shell)

File permissions (cont’d)
shell> ls –l
drwxr-xr-w 3 jon fac 46
u g o owner group
When a file is created it is given a restricted
permission and a default group
You can broader or further restrict permissions

File permissions (cont’d)
#include
#include
int chmod (char *path, mode_t mode); prefer symbolic flags: man fstat, e.g. S_IRGRP : GRouP has Read)
Also at the command-line (absolute)
chmod 0077 st.txt
Also at the command-line (relative)
chmod go-xr st.txt chmod u+xrw st.txt
0077
each octal digit is rwx (or 1) for ugo 000, 111, 111

Power of IDs
• Real user-id: user that actually initiated a process
• Not executable owner!
-r-x … 1 jon fac 203 Feb 10 10:47 test Bill> /usr/jon/fac
• Effective user-id: user that system associates with the process for purposes of protection
• Usually the same as the real user-id: this would be?
• Sometimes want effective user-id to that of the file owner and not the user … why?

Power of IDs (cont’d)
• How do to it?
shell> chmod u+s my_file
-r-s… 1 jon fac 203 Feb 10 10:47 test
Bill> /usr/jon/test has priviledge of ‘jon’
shell> chmod g+s
Has priviledge of group ‘fac’

Masks
creat (“my_file”, 0777);
• Expectation:
• shell> ls –l
• -rwxrwxrwx jon • Instead:
• -rw——– …. • What happened?
… my_file … my_file
• To prevent against accidental exposure, Unix sets a default mask with your process (type umask)
• Typically: 077 (1 means mask out)
creat (“name”, PERM & (~mask)); AND regular files also mask out execute

Masks (cont’d)
creat (“name”, PERM & (~mask)); umask is 022: what is this one?
To change the mask:
mode_t umask (mode_t newmask);

Memory-mapped files: discussion
• Map file into memory
#include
void *mmap (void *start, // ignore, 0
size_t length, // how much to map
int prot,
int flags,
int fd,
off_t offset)
• Return memory address of file data
• Can use normal memory operations to read/write data
// access type
// the file
// starting where

Mmap: discussion
fildes = open(…) lseek(fildes, some_offset) read(fildes, buf, len)
becomes:
fildes = open(…)
address = mmap (0, len, PROT_READ,
MAP_PRIVATE, fildes,
some_offset) /* Use data at address. */
Advantage?