|
234 | 234 | the file} (regular, device, pipe, or socket) and whether the file is in a
|
235 | 235 | blocking or non-blocking mode (flag \texttt{O\_NONBLOCK} on file opening, see
|
236 | 236 | 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. |
239 | 240 | \item \texttt{read} returns a non-zero number of bytes less than \emph{nbyte} if
|
240 | 241 | less than \emph{nbyte} bytes remain in a file, if the call was interrupted
|
241 | 242 | 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}. |
245 | 247 | \item \texttt{write} returns a non-zero number of bytes less than \emph{nbyte}
|
246 | 248 | if less then \emph{nbyte} bytes can fit into the file (e.g. disk full), if the call
|
247 | 249 | was interrupted by a signal, or if \verb#O_NONBLOCK# was set and only part of
|
|
293 | 295 | descriptor is closed (and all of its possible duplicates), the file data is
|
294 | 296 | released.
|
295 | 297 | \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. |
297 | 299 | \item If you forget to close file descriptors when you no longer need them,
|
298 | 300 | and it is a long living process, a daemon perhaps, depending on the system
|
299 | 301 | 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. |
301 | 303 | \item \label{SIMPLE_CAT} A very simple \texttt{cat(1)} program:
|
302 | 304 | \example{read/cat.c}
|
303 | 305 | \end{itemize}
|
|
348 | 350 | overhead.
|
349 | 351 | \item If you need to work with little pieces of data read from files, you can
|
350 | 352 | 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. |
353 | 355 | \item Note that we always only write as many bytes as we actually read. See
|
354 | 356 | page \pageref{READCALL} for more information on when we can read/write less data
|
355 | 357 | than requested.
|
|
392 | 394 | \item If the pipe is not yet open for writing by any process, to open the pipe
|
393 | 395 | for reading without blocking on the call, one needs to use \texttt{O\_NONBLOCK},
|
394 | 396 | 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}!). |
396 | 399 | \item However, trying to read a previously opened pipe that has no producer
|
397 | 400 | results in a return value of 0, indicating the end of file -- the process will
|
398 | 401 | not block waiting for a producer. It is irrelevant whether the pipe was opened
|
|
433 | 436 | \item When opening a pipe for writing only with \texttt{O\_NONBLOCK} and without
|
434 | 437 | an existing consumer, the call returns -1 and \texttt{errno} is set to
|
435 | 438 | \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. |
442 | 445 | \item If you want to create a process that sits on a named pipe and processes
|
443 | 446 | data from producers, you need to open it with the flag \texttt{O\_RDWR} even
|
444 | 447 | if you do not intend to write to it. If you do not use the flag, you might end
|
|
580 | 583 | \texttt{int \funnm{dup2}(int \emph{fildes}, int \emph{fildes2});}
|
581 | 584 | \begin{itemize}
|
582 | 585 | \item duplicates \texttt{fildes} to \texttt{fildes2}.
|
583 |
| -\item almost the same as\\ |
| 586 | +\item almost the same as:\\ |
584 | 587 | \texttt{close(fildes2);\\ fcntl(fildes, F\_DUPFD, fildes2);}
|
585 | 588 | \end{itemize}
|
586 | 589 | \end{slide}
|
|
870 | 873 | also archive utilities to make sure the times are the same for the originals and
|
871 | 874 | copies. (e.g. \texttt{tar} or \texttt{rsync}).
|
872 | 875 | \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. |
873 | 880 | \end{itemize}
|
874 | 881 |
|
875 | 882 | %%%%%
|
|
999 | 1006 | \item \texttt{readdir} is a stateful function. To read the directory from the
|
1000 | 1007 | beginning again, \texttt{rewinddir} can be used. If you want to read the
|
1001 | 1008 | 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 |
1003 | 1010 | \texttt{read} call. In that case, of course, you need to know the internal
|
1004 | 1011 | 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. |
1006 | 1013 | \item \texttt{d\_ino} might not be very useful if the directory is a
|
1007 | 1014 | mountpoint; this member contains the i-node number of the directory where the
|
1008 | 1015 | filesystem is mounted and not the root of the mounted filesystem as one might
|
|
1078 | 1085 | \end{slide}
|
1079 | 1086 |
|
1080 | 1087 | \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. |
1081 | 1091 | \item The descriptor for \texttt{fchdir} is from \texttt{open} called on the
|
1082 | 1092 | directory (i.e. not from \texttt{opendir}).
|
1083 | 1093 | \item There is also a function \texttt{chroot} which changes the root
|
|
0 commit comments