Files in SP Flashcards

1
Q

What is posix?

A

POSIX is a standard that attempts to define a common API among Operating Systems

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

What is the prototype of perror and what does perror do?

A

Prot: void perror(char*str);

It print the error to the standard out

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

What is the prototype of open.

What is the prototype of read and what is the header file for it?

A

int open(char* filename, int flags): //flags = O_RDONLY, O_WRONLY, O_RDWR

read: ssize_t read(int f, void* buf, size_t count)

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

What are the different flags that can be used with open to read a file?

A

O_RDONLY, O_WRONLY, O-RDWR | O_CREAT

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

How do you give permissions to files for read, write and execute?

A

Using the flag S_IRWXU

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

How do you write to a file using system calls, what is the prototype?

A

You write a file using the write function

protype: size_t write(int fd, void* buf, size-t n);

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

What are the integer symbols for stdin, stdout and stderror?

A

stdin: 0
stdout: 1
stderror: 2

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

Why is it not guarenteed that a successul close does not save the data to the disk?

A

This is because the kernel defers writes.

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

What is the difference between buffered an unbuffered IO?

A

Buffered IO means that the data read and written in blocks or chunks to the file system. The disk is accessed less frequently.

Whereas, unbuffered IO means that the data is written to the disk immediately when there is a request to do so.

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

How do you randomly access a file ( which function )?

What is the prototype and describe how it works completely

A

The lseek function can be used to randomly access a file.

Prot: off_t lseek (int fd, off_t offset, int whence);

offset: This is the offset from the where the reading should take place.
whence: used as a reference point for the offset

  • SEEK_SET: Sets the offset position to the start of the file
  • SEEK_CUR: Sets the offset from the current position of the file.
  • SEEK_END: Sets the position to the end of the file. This can be used to extend the file as well but it would fill in the extra spaces with the null characters.

Once lseek is used to set the position, use the regular read function to start reading from that position how many ever bytes are requried.

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

Describe the function fread ( the parameters and what they mean )

A

fread is used to read a file. It reads in a block that contains count number of members, each of size msize.

fread (void* ptr, size_t msize, size_t count, FILE* fp)

  • void* ptr: Pointer to the buffer where the contents of the file is going to be stored. It’s size should be msize * count
  • msize: Size of each member in the block
  • count: The total number of blocks
  • fp: The file pointer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Describe fwrite and how it is used.

A

fread is used to read a file. It reads in a block that contains count number of members, each of size msize.

fwrite (const void* ptr, size_t msize, size_t count, FILE* fp)

  • void* ptr: Pointer to the buffer where the contents of the file is stored.
  • msize: Size of each member in the block
  • count: The total number of blocks
  • fp: The file pointer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what are the flags that can be used for:

  1. Opening an existing file for update
  2. Creating a new file for update
  3. Open or create file for update
A
  1. r+
  2. w+
  3. a+
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the 3 components of a hard disk drive

A
  • 3 platters
  • Multiple headers
  • RAM/Cache
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How are accessing details of files and directories programatically done (There are 3 functions that can be used for this )?

What are the differences between these functions?

A
  • int stat(const char* path, struct stat *buf);
  • int fstat ( const char*path, struct stat* buf )
  • int lstat ( const char *path, struct stat* buf )

stat: retrieve information about the file pointer to by stat
lstat: is identical to stat() except for symbolink links, it gives you back the value of the link.
fstat: is identical to the stat() function but instead of taking the path name, it takes in a file desciptor

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

What member of stat struct can be used to obtain important information of files?

A

the st_mode member of the stat file and contains important information in a small space.

17
Q

What macros are used to obtain information from the st_mode in the stats structure?

Give some examples of these macro functions and their use ( there are many but just few )

A

The s_*(m) macros are used to obtain additional information.

The macros can be used to answer some common questions

  • is regular files?: S_ISREG(m)
  • is directory? : S_ISDIR(m)
18
Q

how do you check for user write permissions for a file?

A

In order to check for user write permissions, you need to do a bit mask with S_IWUSR on the st_mode

st_mode & S_IWUSR

19
Q

What permissions do you need when you want to open a file apart from read permissions from the file.

A

In order to open a file, you need to have execute permissions on all of the directories in the path.

20
Q

Do you need read permissions on the file in order to use the stat function?

A

You don’t need read permissions on the file to execute the stats functions.

21
Q

What data structure does the organisation of files resemble

A

The file system is organized like a tree. You may have to backtrack if you want to access another file.

22
Q

What approach is used to read or write files faster to memory?

A

Files are written in blocks to the memory.

23
Q

What are the advantages of reading and writing files in a block?

A

When file is read and written in blocks then it is a lot faster.

24
Q

What are the disadvantages of working with files in a blocks?

A

If the block sizes are too large in comparison to the files, then there will be lot of wasted space inside the blocks that is not taken up by the file as only one file can be stored inside a file.

One block can only contain one file but

one file can be constituted by multiple blocks

If the block sizes are too small then you will spend an enormous amount of time reading and writing blocks.

Also, the information needed to track and manage all these blocks for a file will grow very large.

25
Q

What is inode?

A

Inode is a data structure in Unix that is used to represent files in Unix.

Inode tells you everything about the file except for the filename and the contents of the file.

Apart from the metadata of the file, inode stores the location of the actual file.

26
Q
  1. The disk is divided into files and each of these blocks are assigned to files. The inode keeps track of which all blocks are assigned to a file.
    2.
27
Q

What is a hardlink?

A

A hardlink of a file is the mirror copy of the file in the system which share the same inode value.

It represents entries in the directory.

hardlinks can only be created in a file, it cannot be created for a directory.

28
Q

What are symbolic links?

A

Symbolic links in file systems are files that don’t reallye exist in the disk, but they just redirect you to another file in the system.

30
Q

How does a softlink differ from a hardlink?

A

HardLink:

  • Link to the same inodes
  • Cannot create hardlinks for directories
  • Links have actual files

SoftLink:

  • Link to different inodes
  • Can create softlinks for directories
31
Q

How do you open a directory and read through it in C?

A

To open a directory, you would use the opendir() function

DIR* opendir(const char* name);

This would return a DIR*, which can be used to read a directory using the readdir function.

The readdir function returns a dirent struct which can be used to read access the directory.

32
Q

What are the 2 important (typically) items from the dirent structure?

A

The 2 important members in the direct structure are the inode number and the filename

33
Q

Which system function can be used to get the current working directory and to chagne the directory?

A

The current working directory can be obtained by using the getcwd() function.

chdir() to change the directory