Skip to content

Commit bc777b1

Browse files
committed
More on current working directory.
1 parent 30946fd commit bc777b1

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

file-api.tex

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,16 @@
234234
the file} (regular, device, pipe, or socket) and whether the file is in a
235235
blocking or non-blocking mode (flag \texttt{O\_NONBLOCK} on file opening, see
236236
page \pageref{O_NONBLOCK}).
237-
\item For both calls there are quite a few situations that can happen, and
238-
which are listed in the next few paragraphs. If unsure, consult the standard.
237+
\item For both calls there are quite a few situations that may happen, and
238+
those most important are listed in the next few paragraphs. For the full
239+
disclosure, consult the standard or a manual page on your system.
239240
\item \texttt{read} returns a non-zero number of bytes less than \emph{nbyte} if
240241
less than \emph{nbyte} bytes remain in a file, if the call was interrupted
241242
by a signal, or if the file is a pipe, device, or socket and there is less than
242-
\emph{nbyte} bytes available at the moment. If there is no data, a blocking
243-
\texttt{read} will block unless some data gets available, a non-blocking
244-
\texttt{read} returns -1 and sets \texttt{errno} to \texttt{EAGAIN}.
243+
\emph{nbyte} bytes available at the moment. For the last case, if there is no
244+
data, a blocking \texttt{read} will block unless some data gets available, a
245+
non-blocking \texttt{read} returns -1 and sets \texttt{errno} to
246+
\texttt{EAGAIN}.
245247
\item \texttt{write} returns a non-zero number of bytes less than \emph{nbyte}
246248
if less then \emph{nbyte} bytes can fit into the file (e.g. disk full), if the call
247249
was interrupted by a signal, or if \verb#O_NONBLOCK# was set and only part of
@@ -293,11 +295,11 @@
293295
descriptor is closed (and all of its possible duplicates), the file data is
294296
released.
295297
\item Even \texttt{close} may fail. For example, some filesystems may write the
296-
data on file closure, and if that write fails, \texttt{close} fails.
298+
data on file closure (NFS), and if that write fails, \texttt{close} fails.
297299
\item If you forget to close file descriptors when you no longer need them,
298300
and it is a long living process, a daemon perhaps, depending on the system
299301
configuration you may hit memory starvation as the system memory will be filled
300-
with the ever growing file descriptor table.
302+
with an ever growing file descriptor table.
301303
\item \label{SIMPLE_CAT} A very simple \texttt{cat(1)} program:
302304
\example{read/cat.c}
303305
\end{itemize}
@@ -348,8 +350,8 @@
348350
overhead.
349351
\item If you need to work with little pieces of data read from files, you can
350352
use stream oriented functions that internally buffer the data -- \texttt{fopen},
351-
\texttt{fread}, \dots However, please do not use those functions in your
352-
semester assignment and at the exam.
353+
\texttt{fread}, \dots However, those functions are prohibited in your semester
354+
assignment and at the exam.
353355
\item Note that we always only write as many bytes as we actually read. See
354356
page \pageref{READCALL} for more information on when we can read/write less data
355357
than requested.
@@ -392,7 +394,8 @@
392394
\item If the pipe is not yet open for writing by any process, to open the pipe
393395
for reading without blocking on the call, one needs to use \texttt{O\_NONBLOCK},
394396
see page \pageref{O_NONBLOCK}. Without that flag the consumer will block
395-
waiting for a producer.
397+
waiting for a producer (in the \texttt{open} system call, not in
398+
\texttt{read}!).
396399
\item However, trying to read a previously opened pipe that has no producer
397400
results in a return value of 0, indicating the end of file -- the process will
398401
not block waiting for a producer. It is irrelevant whether the pipe was opened
@@ -433,12 +436,12 @@
433436
\item When opening a pipe for writing only with \texttt{O\_NONBLOCK} and without
434437
an existing consumer, the call returns -1 and \texttt{errno} is set to
435438
\texttt{ENXIO}. This asymmetry in opening a pipe for reading in non-blocking
436-
mode is due to the fact that it is not desirable to have data in a pipe that may
437-
not be read in a short period of time. The Unix system does not allow for
438-
storing pipe data for an arbitrary length of time. Without the
439-
\texttt{O\_NONBLOCK} flag, the process will block while waiting for a consumer.
440-
By asymmetry, we mean that the system allows consumers without
441-
producers but it tries to avoid writers without existing readers.
439+
mode (see above) is due to the fact that it is not desirable to have data in a
440+
pipe that may not be read in a short period of time. The Unix system does not
441+
allow for storing pipe data for an arbitrary length of time. By asymmetry we
442+
mean that the system allows consumers without producers but it tries to avoid
443+
writers without existing readers. Without the \texttt{O\_NONBLOCK} flag, the
444+
process will block while waiting for a consumer.
442445
\item If you want to create a process that sits on a named pipe and processes
443446
data from producers, you need to open it with the flag \texttt{O\_RDWR} even
444447
if you do not intend to write to it. If you do not use the flag, you might end
@@ -580,7 +583,7 @@
580583
\texttt{int \funnm{dup2}(int \emph{fildes}, int \emph{fildes2});}
581584
\begin{itemize}
582585
\item duplicates \texttt{fildes} to \texttt{fildes2}.
583-
\item almost the same as\\
586+
\item almost the same as:\\
584587
\texttt{close(fildes2);\\ fcntl(fildes, F\_DUPFD, fildes2);}
585588
\end{itemize}
586589
\end{slide}
@@ -870,6 +873,10 @@
870873
also archive utilities to make sure the times are the same for the originals and
871874
copies. (e.g. \texttt{tar} or \texttt{rsync}).
872875
\item The shell interface for \texttt{utime} is the command \texttt{touch}.
876+
\item There is an excellent section \emph{1.7 Dates and Times} in [Butenhof]
877+
that goes through various representations of time in Unix systems, and functions
878+
that use those. It is 12 pages of good stuff, highly recommended if you ever
879+
need to work with time in C and/or Unix.
873880
\end{itemize}
874881

875882
%%%%%
@@ -999,10 +1006,10 @@
9991006
\item \texttt{readdir} is a stateful function. To read the directory from the
10001007
beginning again, \texttt{rewinddir} can be used. If you want to read the
10011008
directory from multiple threads, use the reentrant version, \texttt{readdir\_r}.
1002-
\item Some older systems might support reading a directory via a normal
1009+
\item Some (very) old systems might support reading a directory via a normal
10031010
\texttt{read} call. In that case, of course, you need to know the internal
10041011
representation of the directory data so the use of this is questionable. Linux
1005-
does not allow it at all.
1012+
does not allow this at all.
10061013
\item \texttt{d\_ino} might not be very useful if the directory is a
10071014
mountpoint; this member contains the i-node number of the directory where the
10081015
filesystem is mounted and not the root of the mounted filesystem as one might
@@ -1078,6 +1085,9 @@
10781085
\end{slide}
10791086

10801087
\begin{itemize}
1088+
\item The current working directory for a process is stored in a process
1089+
structure in kernel, usually it is a link to the directory's \emph{vnode}. More
1090+
on vnodes later.
10811091
\item The descriptor for \texttt{fchdir} is from \texttt{open} called on the
10821092
directory (i.e. not from \texttt{opendir}).
10831093
\item There is also a function \texttt{chroot} which changes the root

0 commit comments

Comments
 (0)