|
38 | 38 | \item The text is currently being translated to English.
|
39 | 39 | \item This material is published under the
|
40 | 40 | \href{http://creativecommons.org/licenses/by-nc-sa/3.0/cz/}{Creative Commons
|
41 |
| -BY-NC-SA 3.0} license and is always work in progress, see the history on |
| 41 | +BY-NC-SA 3.0} license and is always a work in progress, see the history on |
42 | 42 | GitHub:\\
|
43 | 43 | \url{https://github.com/devnull-cz/unix-linux-prog-in-c}
|
44 | 44 | \item To download the latest version, go to the \emph{releases} tab on GitHub.
|
|
48 | 48 | \item The source code files can be found on GitHub here:\\
|
49 | 49 | \url{https://github.com/devnull-cz/unix-linux-prog-in-c-src}
|
50 | 50 | \item In case you find any errors either in the text or in the example programs,
|
51 |
| -we appreciate if you let us know. Especially do not hesitate to create new |
| 51 | +we would appreciate you letting us know. Especially do not hesitate to create new |
52 | 52 | issues on \url{https://github.com/devnull-cz/unix-linux-prog-in-c/issues}.
|
53 | 53 | \end{itemize}
|
54 | 54 |
|
|
118 | 118 |
|
119 | 119 | \begin{itemize}
|
120 | 120 | \item Note that \emsl{Linux is a kernel}, not the whole system. In contrast to
|
121 |
| -FreeBSD, for example, which covers both the kernel and the userland. It is |
| 121 | +FreeBSD for example, which covers both the kernel and the userland. It is |
122 | 122 | better to say a ``Linux distribution'' if you discuss a whole system that is
|
123 | 123 | built around the Linux kernel.
|
124 | 124 | \item FreeBSD and NetBSD forked from 386BSD (now defunct) in 1993, OpenBSD
|
|
128 | 128 | \item Presently, the ``UNIX'' trademark can be only used by systems that passed
|
129 | 129 | conformance tests defined in the Single UNIX Specification (SUS).
|
130 | 130 | \item From those systems listed above, only Solaris, macOS, AIX, and HP-UX are
|
131 |
| -UNIX~03 compliant (\url{http://www.opengroup.org/openbrand/register/}). Other, |
132 |
| -not certified systems, are often described as ``Unix-like'', even that in many |
133 |
| -cases they closely follow the standard. However, word ``Unix'' is often used |
| 131 | +UNIX~03 compliant (\url{http://www.opengroup.org/openbrand/register/}). Other |
| 132 | +uncertified systems, are often described as ``Unix-like'', even when in many |
| 133 | +cases they closely follow the standard. However, the word ``Unix'' is often used |
134 | 134 | for systems from either group.
|
135 | 135 | \item The above list is a tiny fraction of the whole Unix world. Every
|
136 |
| -proprietary Unix variant usually came from either UNIX~V or BSD, and added its |
137 |
| -own features. That is why there were quite a few standards as well, see page |
138 |
| -\pageref{UNIXSTANDARDS}. In the end vendors agreen upon a small set of those. |
139 |
| -\item If you are interested in detailed and up-to-date Unix system version |
| 136 | +proprietary Unix variant likely came from either UNIX~V or BSD, and added its |
| 137 | +own features. This resulted in quite a few standards as well, see page |
| 138 | +\pageref{UNIXSTANDARDS}. In the end vendors, agreed upon a small set of those. |
| 139 | +\item If you are interested in a detailed and up-to-date Unix system version |
140 | 140 | history, go check \url{https://www.levenez.com/unix/}.
|
141 | 141 | \end{itemize}
|
142 | 142 |
|
|
168 | 168 | \item standard of the The Open Group organization founded in 1996 via
|
169 | 169 | merging X/Open and OSF
|
170 | 170 | \item today at Version~4 (\emsl{SUSv4})
|
171 |
| - \item the compliance is requisite condition for using the UNIX trademark |
| 171 | + \item compliance is a requisite condition for using the UNIX trademark |
172 | 172 | \end{itemize2}
|
173 | 173 | \end{itemize}
|
174 | 174 | \end{slide}
|
|
180 | 180 | complex and incomprehensible on a first sight.
|
181 | 181 | \item AT\&T allowed the producers to call its own commercial UNIX variant
|
182 | 182 | ``System V'' only if it complied to the SVID standard conditions. AT\&T also
|
183 |
| -published \emph{System~V Verification Suite} (SVVS), that checked whether given |
| 183 | +published \emph{System~V Verification Suite} (SVVS), that checked whether a given |
184 | 184 | system complies to the standard.
|
185 |
| -\item POSIX (Portable Operating System Interface) is standardization effort |
| 185 | +\item POSIX (Portable Operating System Interface) is a standardization effort |
186 | 186 | of the IEEE organization (Institute of Electrical and Electronics Engineers).
|
187 |
| -\item SUSv4 is common standard of The Open Group, IEEE (Std. 1003.1, 2008 |
| 187 | +\item SUSv4 is a common standard of The Open Group, IEEE (Std. 1003.1, 2008 |
188 | 188 | Edition) and ISO (ISO/IEC 9945-2008).
|
189 |
| -\item To certify given system to Single Unix Specification it is necessary |
| 189 | +\item To certify a given system for the Single Unix Specification, it is necessary |
190 | 190 | to pass a series of tests (on given architecture, e.g. 64-bit x86).
|
191 |
| -The results of the tests are then evaluated. The tests itself are unified into |
| 191 | +The results of the tests are then evaluated. The tests themselves are unified into |
192 | 192 | so called \emph{test suites}, which are sets of automatic tests that go through
|
193 | 193 | the system and verify if it implements the interfaces specified in the standard.
|
194 |
| -For example for SUSv3 there are 10 such test suites. |
| 194 | +For example, for SUSv3 there are 10 such test suites. |
195 | 195 | \item The interfaces specified by the POSIX.1-2008 standard are divided into 4
|
196 | 196 | basic groups: XSH (System Interfaces), XCU (Shell and Utilities), XBD
|
197 |
| -(Base definitions). W.r.t. number of interfaces the biggest of them is XSH that |
| 197 | +(Base definitions). W.r.t. number of interfaces, the biggest of them is XSH which |
198 | 198 | contains more than 1000 interfaces.
|
199 |
| -\item The interface groups of POSIX together with the Xcurses group, that is |
200 |
| -part of Single Unix Specification (however not part of POSIX base in the IEEE Std |
201 |
| -1003.1-2001 standard) includes 1742 interfaces in total, which form Single Unix |
| 199 | +\item The interface groups of POSIX together with the Xcurses group, are part |
| 200 | +of the Single Unix Specification (however not part of POSIX base in the IEEE Std |
| 201 | +1003.1-2001 standard) which includes 1742 interfaces in total, which form the Single Unix |
202 | 202 | Specification (2003). The SUS interface tables are here:
|
203 | 203 | \url{http://www.unix.org/version3/inttables.pdf}
|
204 | 204 | \item Commercial UNIXes largely follow the Single UNIX Specification, compliance
|
|
207 | 207 | UNIX V7 - do not mix it up with historical V7 UNIX). It is built on the POSIX
|
208 | 208 | base.
|
209 | 209 | \item We are going to follow SUSv4 for APIs in this lecture. The data structure
|
210 |
| -definitions and algorithms used by kernel will be mostly based on |
| 210 | +definitions and algorithms used by the kernel will be mostly based on |
211 | 211 | System~V Rel.~4 to keep things simple.
|
212 |
| -\item On Solaris there is a extensive \texttt{standards(5)} manual page, where |
| 212 | +\item On Solaris there is an extensive \texttt{standards(5)} manual page, where |
213 | 213 | lots of information about standards can be found in one place.
|
214 | 214 | Individual commands compliant to the standard are moreover placed
|
215 | 215 | into special directories, e.g. the \texttt{tr} program is located in
|
216 | 216 | \texttt{/usr/xpg4/bin/} and \texttt{/usr/xpg6/bin/} directories, in each there
|
217 | 217 | is a version of the program compliant to the respective standard.
|
218 |
| -The options and behavior specified by the standard can be then relied on e.g. |
| 218 | +The options and behavior specified by the standard can be then relied upon e.g. |
219 | 219 | when writing shell scripts.
|
220 | 220 | \item Also on Solaris, look into the
|
221 | 221 | \texttt{/usr/inc{}lude/sys/fea\-ture\-\_tests.h} header file.
|
|
270 | 270 | POSIX.4, because that was its original naming before renumbering.
|
271 | 271 | Most of this extension is optional, therefore the claim ``system supports
|
272 | 272 | POSIX.1b'' gives even worse testimony that ``system is POSIX compatible'', i.e.
|
273 |
| -practially zero. The only mandatory part of POSIX.4 is simple addendum to |
| 273 | +practially zero. The only mandatory part of POSIX.4 is a simple addendum to |
274 | 274 | signals compared to POSIX1990. It is therefore always necessary to state
|
275 | 275 | what exactly out of POSIX.4 is implemented -- e.g. shared memory, semaphores,
|
276 | 276 | real-time signals, memory locking, asynchronous I/O, timers, etc.
|
|
280 | 280 | \pageref{RWLOCKS}.
|
281 | 281 | \item \dots
|
282 | 282 | \end{itemize}
|
283 |
| -\item The POSIX standars can be found on \url{http://www.open-std.org/}. |
284 |
| -The HTML version is freely available, PDF documents have to be paid. |
| 283 | +\item The POSIX standards can be found on \url{http://www.open-std.org/}. |
| 284 | +The HTML version is freely available, PDF documents have to be purchased. |
285 | 285 | \end{itemize}
|
286 | 286 |
|
287 | 287 |
|
|
313 | 313 |
|
314 | 314 | \begin{enumerate}
|
315 | 315 | \item A great book on Unix in general and compares SVR4.2, 4.4BSD, Solarix~2.x
|
316 |
| -and Mach systems. The 2nd edition scheduled for 2005 never happened, |
| 316 | +and Mach systems. The 2nd edition, scheduled for 2005, never happened, |
317 | 317 | unfortunately.
|
318 | 318 | \item UNIX classic book. On UNIX System~V Rel.~2, and partially 3 as well.
|
319 | 319 | While outdated, it is one of the best books ever written on Unix. In 1993 a
|
|
357 | 357 | net\-work\-ing. Not as detailed as books 1 and 3 but that could be to your
|
358 | 358 | advantage. We very much recommend this book, especially if you want just one.
|
359 | 359 | The author can see the big picture which is quite rare.
|
360 |
| -\item Unix network programming classics, one of the best on the topic; there is |
| 360 | +\item Unix network programming classic, one of the best on the topic; there is |
361 | 361 | also volume 2, \emsl{UNIX Network Programming, Volume 2: Interprocess
|
362 | 362 | Communications}, covering interprocess communication in great detail.
|
363 | 363 | \item Great book on programming with threads using POSIX API. Highly
|
|
397 | 397 | Solaris, the manual page section needs to be provided with the \texttt{-s}
|
398 | 398 | option, ie. ``\texttt{man -s 2 chmod}''.
|
399 | 399 | \item The \texttt{man} command uses a list of system directories to search for
|
400 |
| -man pages. If you have manual pages some place else, perhaps in a local subtree |
401 |
| -after you unpacked a tar file you downloaded and want to check the |
| 400 | +man pages. If you have manual pages someplace else, perhaps in a local subtree |
| 401 | +after you unpacked a tar file you downloaded and you want to check the |
402 | 402 | documentation, the \texttt{-M} option may come in handy.
|
403 | 403 | \item Sometimes there are entries for the same name in several sections. If
|
404 | 404 | unsure what you are looking for, use the \texttt{-a} option to get all manual
|
|
504 | 504 | \item Be careful when using tools like \texttt{hexdump} that by default print
|
505 | 505 | out a file as 16-bit numbers. The ordering of individual bytes may not be how
|
506 | 506 | they are stored in a file. For example, take FreeBSD on i386. The first number
|
507 |
| -in the file is character ``i'' which represents lower 8 bits of the first 16-bit |
| 507 | +in the file is character ``i'' which represents the lower 8 bits of the first 16-bit |
508 | 508 | number, so when the first two bytes are printed out as a 16-bit number, the byte
|
509 | 509 | representing ``i'', ie. ``69'', is shown as the second byte. Similarly for
|
510 | 510 | ``kl''.
|
|
516 | 516 | 0000004
|
517 | 517 | \end{verbatim}
|
518 | 518 |
|
519 |
| -You can use other output formats though, for example as hexa bytes and |
| 519 | +You can use other output formats though, for example as hex bytes and |
520 | 520 | characters in the same output:
|
521 | 521 |
|
522 | 522 | \begin{verbatim}
|
|
595 | 595 |
|
596 | 596 | \begin{itemize}
|
597 | 597 | \item One of the most important thing of a C style (well, any style) is
|
598 |
| -consistency. And often it is not that important what an exact C style a group |
| 598 | +consistency. And often it is not that important what exact C style a group |
599 | 599 | of coders is going to pick as it is that one specific style is chosen and then
|
600 | 600 | religiously followed by all in the group. A good and rigorously followed cstyle
|
601 | 601 | leads to a smaller number of bugs in code.
|
|
730 | 730 | \item Non-trivial programs are often split into several source code files that
|
731 | 731 | contain related functions. Such files can be compiled independently, and you
|
732 | 732 | can even use different languages and different compilers for each file. The
|
733 |
| -advantage is the speed of building as only modified files are re-compiled (see |
734 |
| -page \pageref{MAKE} on the \texttt{make} utility), and also flexibility as you |
| 733 | +advantage is the speed of building, as only modified files are re-compiled (see |
| 734 | +page \pageref{MAKE} on the \texttt{make} utility), and also flexibility, as you |
735 | 735 | can use some of the files in other programs as well.
|
736 | 736 | \item The \emph{compiler} compiles each file into a corresponding object file.
|
737 | 737 | Instead of external function pointers in the compiled code, the object file
|
|
772 | 772 | \item In a situation where you need to fix code full of includes and conditional
|
773 | 773 | compilation, the output after the preprocessor phase may be very helpful to
|
774 | 774 | locate the problem.
|
775 |
| -\item \texttt{cpp} (or \texttt{cc -E}) also allow you to see the whole tree of |
| 775 | +\item \texttt{cpp} (or \texttt{cc -E}) also allows you to see the whole tree of |
776 | 776 | included files, printed on the standard error output. For that, use a separate
|
777 | 777 | \texttt{-H} option (not \texttt{-EH}) and redirect the output to
|
778 | 778 | \texttt{/dev/null}:
|
|
792 | 792 | etc...
|
793 | 793 | \end{verbatim}
|
794 | 794 |
|
795 |
| -\item You cannot nest comments in C so in order to temporarily disable code with |
| 795 | +\item You cannot nest comments in C, so in order to temporarily disable code with |
796 | 796 | comments without deleting it, wrapping it in another comment will not work.
|
797 | 797 | So, the preprocessor to your rescue -- use the conditional compilation feature:
|
798 | 798 |
|
|
845 | 845 | \sltitle{Compiler}
|
846 | 846 | \renewcommand{\arraystretch}{1.1}
|
847 | 847 | \begin{itemize}
|
848 |
| -\item volání:\\ |
| 848 | +\item volání:\\ |
849 | 849 | \texttt{cc [\emph{options}] \emph{file} \dots}
|
850 | 850 | \item the most important options:\\
|
851 | 851 | \begin{tabular}{ll}
|
|
856 | 856 | \texttt{-L\emph{directory}} & add a directory to search when using \texttt{-l}\\
|
857 | 857 | \texttt{-O\emph{level}} & optimalization level\\
|
858 | 858 | \texttt{-g} & compile with debug information\\
|
859 |
| -\texttt{-D\emph{jméno}} & define a macro for the preprocessor\\ |
| 859 | +\texttt{-D\emph{jméno}} & define a macro for the preprocessor\\ |
860 | 860 | \texttt{-I\emph{directory}} & add a directory to search for \texttt{\#include} files
|
861 | 861 | \end{tabular}
|
862 | 862 | \end{itemize}
|
|
907 | 907 | \end{slide}
|
908 | 908 |
|
909 | 909 | \begin{itemize}
|
910 |
| -\item The way how it works is that you use specific macros to define what you |
| 910 | +\item The way it works is that you use specific macros to define what you |
911 | 911 | want (eg. \texttt{\_POSIX\_SOURCE}), and then you use other macros (eg.
|
912 | 912 | \texttt{\_POSIX\_VERSION}) to find out what you actually got. You always have
|
913 | 913 | to include \texttt{unistd.h} after you set the macros and use a compiler that
|
|
932 | 932 | the use of c99"
|
933 | 933 | cc: acomp failed for standards.c
|
934 | 934 | \end{verbatim}
|
935 |
| -%\item zdroj maker pro standard tedy mù¾e být ji¾ na stranì |
936 |
| -%\pageref{UNIXSTANDARDS} zmiòovaný hla\-viè\-ko\-vý soubor |
| 935 | +%\item zdroj maker pro standard tedy mù¾e být ji¾ na stranì |
| 936 | +%\pageref{UNIXSTANDARDS} zmiòovaný hla\-viè\-ko\-vý soubor |
937 | 937 | %\texttt{feature\_tests.h} na Solarisu.
|
938 | 938 | \item See the documentation for your compiler about what other macros can be
|
939 | 939 | used.
|
|
1122 | 1122 | \end{slide}
|
1123 | 1123 |
|
1124 | 1124 | \begin{itemize}
|
1125 |
| -\item \emsl{Note that the line with a command starts with a tabulator, not |
| 1125 | +\item \emsl{Note that the line with a command starts with a tab, not |
1126 | 1126 | spaces.} Every command line is executed via its own shell invocation. If
|
1127 | 1127 | multiple commands need to be executed via the same shell process, all but the
|
1128 | 1128 | last line needs to be terminated with a backslash. See the following example
|
|
1182 | 1182 | \item defining a macro on a command line: \\
|
1183 | 1183 | \hspace*{5em}\texttt{make \emph{target} \emph{name}=\emph{string}}
|
1184 | 1184 | \item macro invocation: \\
|
1185 |
| -\hspace*{5em}\texttt{\$\emph{name}} (pouze jednoznakové |
| 1185 | +\hspace*{5em}\texttt{\$\emph{name}} (pouze jednoznakové |
1186 | 1186 | \texttt{\emph{name}}), \\
|
1187 | 1187 | \hspace*{5em}\texttt{\$\{\emph{name}\}} or \texttt{\$(\emph{name})}
|
1188 | 1188 | \item environment variables are accessible as macros (eg. \texttt{\$\{EDITOR\}})
|
|
1208 | 1208 | or BSD.
|
1209 | 1209 | \item To write a non-trivial \texttt{Makefile} that will work with
|
1210 | 1210 | different \texttt{make} implementations is not a simple task. Therefore
|
1211 |
| -projects as GNU Automake exist. For a simple conditional compilation where |
| 1211 | +projects as GNU Automake exist. For a simple conditional compilation, where |
1212 | 1212 | based on the system we need to set different options, the following code might
|
1213 | 1213 | come in handy. A character ` is a back quote, and a ' is a normal single quote:
|
1214 | 1214 |
|
|
1292 | 1292 | information.
|
1293 | 1293 | \end{itemize}
|
1294 | 1294 | \item Note that the dynamic linker does not run as a separate process (unless
|
1295 |
| -you run it like that) even that it has its own \texttt{main()} function. It is |
| 1295 | +you run it like that) even though it has its own \texttt{main()} function. It is |
1296 | 1296 | used within an address space of an executed program. The program, dynamic
|
1297 | 1297 | linker, and dynamic libraries constitute a single process.
|
1298 | 1298 |
|
|
1305 | 1305 | \item What a dynamic linker is used is in section \texttt{.interp}, see
|
1306 | 1306 | ''\texttt{elf\-dump -i}'' and ``\texttt{ld -I}''. It means you could write your
|
1307 | 1307 | own dynamic linker and set it via the \texttt{-I} option for \texttt{ld} to your
|
1308 |
| -program. It is needed to say such an enterprise would not be an easy feat at |
| 1308 | +program. Needless to say, such an enterprise would not be an easy feat at |
1309 | 1309 | all.
|
1310 | 1310 | \item To list the dynamic section, use \texttt{elfdump -d}, dynamic libraries
|
1311 | 1311 | are set as \texttt{NEEDED}.
|
|
1319 | 1319 | use \texttt{otool -L} instead.
|
1320 | 1320 | \item What libraries are eventually used when running the program could be
|
1321 | 1321 | different from what \texttt{ldd} shows. For example,
|
1322 |
| -one could use the \texttt{LD\_PRELOAD} mechanism. For that reasons, |
1323 |
| -Solaris has a \texttt{pldd} command which provides for a running process |
1324 |
| -library dependencies. Example on \texttt{LD\_PRELOAD} with \texttt{gcc}: use |
| 1322 | +one could use the \texttt{LD\_PRELOAD} mechanism. For that reason, |
| 1323 | +Solaris has a \texttt{pldd} command which provides the library dependcies for a |
| 1324 | +running process. For an example on \texttt{LD\_PRELOAD} with \texttt{gcc}: use |
1325 | 1325 | already mentioned \texttt{Ma\-ke\-file01}, and compile
|
1326 | 1326 | \example{basic-utils/preload.c} like this: ``\texttt{gcc -shared -o
|
1327 | 1327 | lib\-pre\-load.so preload.c}''. Run the program then which interposes a system
|
1328 | 1328 | call \texttt{close()}: ``\verb#LD_PRELOAD=./libpreload.so ./a.out#''.
|
1329 |
| -\item Most of the information listed here can be found in a manual page for the |
1330 |
| -Solaris dynamic linker, \texttt{ld.so.1(1)}, and much more then in an excellent |
| 1329 | +\item Most of the information listed here can be found in the manual page for the |
| 1330 | +Solaris dynamic linker, \texttt{ld.so.1(1)}, and much more in the excellent |
1331 | 1331 | \emph{Linkers and Libraries Guide} on \texttt{docs.oracle.com}. If you use
|
1332 | 1332 | FreeBSD, its dynamic linker is \texttt{ld-elf.so.1}, on Linux distros it is
|
1333 | 1333 | usually \texttt{ld-linux.so.1}, it is \texttt{rld} on SGI IRIX etc.
|
|
1340 | 1340 | \begin{verbatim}
|
1341 | 1341 | LD_DEBUG=help date
|
1342 | 1342 | \end{verbatim}
|
1343 |
| -\item To tell the linker to look for dynamic libraries also in directories other |
| 1343 | +\item To tell the linker to also look for dynamic libraries in directories other |
1344 | 1344 | than the default ones (paths from \texttt{LD\_LIBRARY\_PA\-TH} are searched
|
1345 | 1345 | first), use it like the following:
|
1346 | 1346 | \begin{verbatim}
|
|
1362 | 1362 | \url{http://xahlee.org/UnixResource\_dir/\_/ldpath.html}.
|
1363 | 1363 |
|
1364 | 1364 | \par
|
1365 |
| -This varible is often misused in the start-up scripts because the command(s) |
| 1365 | +This variable is often misused in the start-up scripts because the command(s) |
1366 | 1366 | is/are incorrectly linked and the dynamic linker would not otherwise find the
|
1367 | 1367 | correct libraries. The typical side effect, however, is that the program(s)
|
1368 | 1368 | subsequently start(s) additional programs that use the same libraries but as all
|
|
1811 | 1811 | \end{itemize}
|
1812 | 1812 |
|
1813 | 1813 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
1814 |
| -% Konvence pøedávání parametrù a environmentových promìnných |
| 1814 | +% Konvence pøedávání parametrù a environmentových promìnných |
1815 | 1815 |
|
1816 | 1816 | \pdfbookmark[1]{argv, argc}{args}
|
1817 | 1817 |
|
|
0 commit comments