Unix System Overview part_2 Flashcards
describe utime function
the access time and the modifiaction time of a file can be changed with the utime function.
#include int utime(const char *pathname, const struct utimbuf *times); what can this function return?
Returns 0 if ok.
and -1 on error
what is the structure used by the utime function?
struct utimbuf { time_t actime; /* access time */ time_t modtime; /* modification time */ }
The operation of utime function, and the privileges required to execute it, depend on whether the times argument is NULL.
if it is not null then?
If times is a non-null pointer, the access time and the modification time are set to the values in the structure pointed to by times. For this case, the effective user ID of the process must equal the owner ID of the file, or the process must be a superuser process. Merely having write permission for the file is not adequate.
what field is automatically updated when the utime function is called.
we are unable to specify a value for the changed-status time, st_ctime—the time the i-node was last changed
what does this return? #include int mkdir(const char *pathname, mode_t mode);
return 0 if OK
and return -1 on error.
what function creates directories?
mkdir.
This function creates a new, empty directory. The entries for dot and dot-dot are automatically created. The specified file access permissions, mode, are modified by the file mode creation mask of the process.
what functions delete directories?
rmdir
An empty directory is deleted with the rmdir function. Recall that an empty directory is one that contains entries only for dot and dot-dot.
#include int rmdir(const char *pathname); what does this return?
Return 0 if OK
and return -1 on error
what is a common mistake that occurs sometimes from using mkdir?
A common mistake is to specify the same mode as for a file: read and write permissions only. But for a directory, we normally want at least one of the execute bits enabled, to allow access to filenames within the directory.
explain how rmdir works?
If the link count of the directory becomes 0 with this call, and if no other process has the directory open, then the space occupied by the directory is freed. If one or more processes have the directory open when the link count reaches 0, the last link is removed and the dot and dot-dot entries are removed before this function returns. Additionally, no new files can be created in the directory. The directory is not freed, however, until the last process closes it. (Even though some other process has the directory open, it can’t be doing much in the directory, as the directory had to be empty for the rmdir function to succeed.)
what does this return? #include int chdir(const char *pathname);
returns 0 if OK
returns -1 on Error
what does this return? #include int fchdir(int filedes)
returns 0 if OK
returns -1 on Error
what functions can change the current working directory of the calling process?
chdir or fchdir fucntions.
we can specify the new current working directory either as a pathname or through an open file descriptor
the fchdir function provides us with an easy way to accomplish a task?
The fchdir function provides us with an easy way to accomplish this task. Instead of calling getcwd, we can open the current directory and save the file descriptor before we change to a different location in the file system. When we want to return to where we started, we can simply pass the file descriptor to fchdir.
what does this return? #include DIR *opendir(const char *pathname);
return pointer if OK
return NULL if error
what does this return?
struct dirent *readdir(DIR *dp);
returns pointer if OK
returns Null at end of directory or error
what does this return? void rewinddir(DIR *dp);
returns 0 if OK
returns -1 of error
what does this return? int closedir(DIR *dp);
returns 0 if OK
returns -1 on error
what does this return? long telldir(DIR *dp);
returns current location in directory associated with dp
what does this return? void seekdir(DIR *dp, long loc);
nothing its void
what are the 3 streams that are predefined and automatically available to a process? and what header are they defined in?
standard input standard output standard error. defined in note: there streams refer to the same files as the file descriptors STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO,
what is the goal of buffering provided by the standard I/O library?
is to use the minimum number of read and write calls. (Recall Figure 3.5, where we showed the amount of CPU time required to perform I/O using various buffer sizes.) Also, it tries to do its buffering automatically for each I/O stream, obviating the need for the application to worry about it.
describe fully buffered
Fully buffered. In this case, actual I/O takes place when the standard I/O buffer is filled. Files residing on disk are normally fully buffered by the standard I/O library. The buffer used is usually obtained by one of the standard I/O functions calling malloc (Section 7.8) the first time I/O is performed on a stream.
The term flush describes the writing of a standard I/O buffer. A buffer can be flushed automatically by the standard I/O routines, such as when a buffer fills, or we can call the function fflush to flush a stream. Unfortunately, in the UNIX environment, flush means two different things. In terms of the standard I/O library, it means writing out the contents of a buffer, which may be partially filled. In terms of the terminal driver, such as the tcflush function in Chapter 18, it means to discard the data that’s already stored in a buffer.
describe line buffered
Line buffered. In this case, the standard I/O library performs I/O when a newline character is encountered on input or output. This allows us to output a single character at a time (with the standard I/O fputc function), knowing that actual I/O will take place only when we finish writing each line. Line buffering is typically used on a stream when it refers to a terminal: standard input and standard output, for example.
Line buffering comes with two caveats. First, the size of the buffer that the standard I/O library is using to collect each line is fixed, so I/O might take place if we fill this buffer before writing a newline. Second, whenever input is requested through the standard I/O library from either (a) an unbuffered stream or (b) a line-buffered stream (that requires data to be requested from the kernel), all line-buffered output streams are flushed. The reason for the qualifier on (b) is that the requested data may already be in the buffer, which doesn’t require data to be read from the kernel. Obviously, any input from an unbuffered stream, item (a), requires data to be obtained from the kernel.
what requirements do buffering characteristics do ISO C require?
Standard input and standard output are fully buffered, if and only if they do not refer to an interactive device. Standard error is never fully buffered.
what defult buffering types for the standard I/Os
standard error is always unbuffered.
all other streams are line buffered if they refer to a terminal device.
otherwise they are fully buffered
what happens if we specify an unbuffered stream?
the fuf and size arguments are ignored.
what happens if we specify fully buffered or line buffered?
buf and size can be optionally specify a buffer and its size
what does this do and return? #include int fflush(FILE *fp);
returns 0 if OK.
returns EOF on error.
this function allows us at any time, we can force a stream to be flushed. this function causes any unwritten data for the stream to be passed to the kernel.
As a special case, if fp is null this function causes all output streams to be flushed
what does this do and return? #include void setbuf(FILE *restrict fp, char *restrict buf);
return 0 if OK
return nonzero on error
used to change the buffering. This function must be called after the stream has been opened. we can turn buffering on or off.
what does this do and return? #include int setvbuf(FILE *restrict fp, char *restrict buf, int mode, size_t size);
return 0 if OK
return nonzero on error
used to change the buffering. This function must be called after the stream has been opened. we specify exactly which type of buffering we want. done by these mode arguments:
_IOFBF fully buffered _IOLBF line buffered _IONBF unbuffered
what does this do and return? #include FILE *fopen(const char *restrict pathname, const char *restrict type);
return file pointer if OK
return NULL on error.
opens a specified file.
part of ISO C.
what does this do and return? #include FILE *freopen(const char *restrict pathname, const char *restrict type, FILE *restrict fp);
return file pointer if OK
return NULL on error.
opens a specified file on a specified stream, closing the stream first if its already open. If the stream previously had an orientation, freopen clears it. This function is typically used to open a specified file as one of the predefined streams: standard input, standard output, or standard error.
part of ISO C
what does this do and return? #include FILE *fdopen(int filedes, const char *type);
The fdopen function takes an existing file descriptor, which we could obtain from the open, dup, dup2,
fcntl, pipe, socket, socketpair, or accept functions, and associates a standard I/O stream with the
descriptor. This function is often used with descriptors that are returned by the functions that create
pipes and network communication channels. Because these special types of files cannot be opened with
the standard I/O fopen function, we have to call the device-specific function to obtain a file descriptor,
and then associate this descriptor with a standard I/O stream using fdopen.
#include int fclose(FILE *fp); what does this do and return?
returns 0 if OK
returns EOF on error
Any buffered output data is flushed before the file is closed. Any input data that may be buffered is discarded. If the standard I/O library had automatically allocated a buffer for the stream, that buffer is released.
When a process terminates normally, either by calling the exit function directly or by returning from the main
function, all standard I/O streams with unwritten buffered data are flushed, and all open standard I/O streams are closed.
describe Character-at-a-time I/O
type of unformatted I/O.
We can read or write one character at a time, with the standard I/O functions handling all the buffering, if the stream is buffered.
describe Line-at-a-time I/O
type of unformatted I/O
If we want to read or write a line at a time, we use fgets and fputs. Each line is terminated with a newline character, and we have to specify the maximum line length that we can handle
when we call fgets
describe Direct I/O
type of unformatted I/O
This type of I/O is supported by the fread and fwrite functions. For each I/O operation, we
read or write some number of objects, where each object is of a specified size. These two functions are
often used for binary files where we read or write a structure with each operation.
what does this do and return? #include int fgetc(FILE *fp);
cannot be implemented as as a macro.
Since fgetc is guaranteed to be a function, we can take its address. This allows us to pass the address of
fgetc as an argument to another function. Calls to fgetc probably take longer than calls to getc, as it usually takes more time to call a function
what does this do and return? #include int ferror(FILE *fp);
returns nonzero (true) if condition is true (when error occurs) returns 0 (false) otherwise
what does this do and return? #include int feof(FILE *fp);
returns nonzero (true) if condition is true (when end of file is reached) returns 0 (false) otherwise
what does this do and return? #include int ungetc(int c, FILE *fp);
after reading from a stream, we can push back characters by calling this function.
returns c if OK,
returns EOF on error.
what does this do and return? #include void clearerr(FILE *fp);
the error flag and end-of-file flag are cleared by calling this function.
does not return anything since void.
what does this do and return? #include char *fgets(char *restrict buf, int n, FILE *restrict fp);
returns buf if OK.
returns null on end-of-file or error.
the line-at-a-time I/O provides the function.
specify the address of the buffer to read the line into. reads from the specified stream. with fgets we have to specify the size of the buffer n.
what does this do and return? #include int fputs(const char *restrict str, FILE *restrict fp);
returns non-negative value if OK.
returns EOF on error.
The function fputs writes the null-terminated string to the specified stream. The null byte at the end is not
written. Note that this need not be line-at-a-time output, since the string need not contain a newline as the last
non-null character. Usually, this is the case—the last non-null character is a newline—but it’s not required.