0intro 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. .TH INTRO 2
  2. .SH NAME
  3. intro \- introduction to library functions
  4. .SH SYNOPSIS
  5. .nf
  6. .B #include <u.h>
  7. .PP
  8. .B #include <libc.h>
  9. .PP
  10. .B #include <auth.h>
  11. .PP
  12. .B #include <bio.h>
  13. .PP
  14. .B #include <draw.h>
  15. .PP
  16. .B #include <fcall.h>
  17. .PP
  18. .B #include <frame.h>
  19. .PP
  20. .B #include <mach.h>
  21. .PP
  22. .B #include <ndb.h>
  23. .PP
  24. .B #include <regexp.h>
  25. .PP
  26. .B #include <stdio.h>
  27. .PP
  28. .B #include <thread.h>
  29. .fi
  30. .SH DESCRIPTION
  31. This section describes functions
  32. in various libraries.
  33. For the most part, each library is defined by a single C include
  34. file, such as those listed above, and a single archive file containing
  35. the library proper. The name of the archive is
  36. .BI /$objtype/lib/lib x .a \f1,
  37. where
  38. .I x
  39. is the base of the include file name, stripped of a leading
  40. .B lib
  41. if present.
  42. For example,
  43. .B <draw.h>
  44. defines the contents of library
  45. .BR /$objtype/lib/libdraw.a ,
  46. which may be abbreviated when named to the loader as
  47. .BR -ldraw .
  48. In practice, each include file contains a
  49. .B #pragma
  50. that directs the loader to pick up the associated archive
  51. automatically, so it is rarely necessary to tell the loader
  52. which
  53. libraries a program needs.
  54. .PP
  55. The library to which a function belongs is defined by the
  56. header file that defines its interface.
  57. The `C library',
  58. .IR libc ,
  59. contains most of the basic subroutines such
  60. as
  61. .IR strlen .
  62. Declarations for all of these functions are
  63. in
  64. .BR <libc.h> ,
  65. which must be preceded by
  66. .RI ( needs )
  67. an include of
  68. .BR <u.h> .
  69. The graphics library,
  70. .IR draw ,
  71. is defined by
  72. .BR <draw.h> ,
  73. which needs
  74. .B <libc.h>
  75. and
  76. .BR <u.h> .
  77. The Buffered I/O library,
  78. .IR libbio ,
  79. is defined by
  80. .BR <bio.h> ,
  81. which needs
  82. .B <libc.h>
  83. and
  84. .BR <u.h> .
  85. The ANSI C Standard I/O library,
  86. .IR libstdio ,
  87. is defined by
  88. .BR <stdio.h> ,
  89. which has no prerequisites.
  90. There are a few other, less commonly used libraries defined on
  91. individual pages of this section.
  92. .PP
  93. The include file
  94. .BR <u.h> ,
  95. a prerequisite of several other include files,
  96. declares the architecture-dependent and -independent types, including:
  97. .IR ushort ,
  98. .IR uchar ,
  99. and
  100. .IR ulong ,
  101. the unsigned integer types;
  102. .IR schar ,
  103. the signed char type;
  104. .IR vlong ,
  105. a very long integral type;
  106. .IR jmp_buf ,
  107. the type of the argument to
  108. .I setjmp
  109. and
  110. .IR longjmp ,
  111. plus macros that define the layout of
  112. .IR jmp_buf
  113. (see
  114. .IR setjmp (2));
  115. definitions of the bits in the floating-point control register
  116. as used by
  117. .IR getfcr (2);
  118. the macros
  119. .B va_arg
  120. and friends for accessing arguments of variadic functions (identical to the
  121. macros defined in
  122. .B <stdarg.h>
  123. in ANSI C);
  124. and
  125. .IR Length ,
  126. for historical reasons a union,
  127. representing the 64-bit length of a file, declared something like
  128. .IP
  129. .EX
  130. .ta 6n +6n +6n
  131. typedef union
  132. {
  133. vlong length;
  134. } Length;
  135. .EE
  136. .SS "Name space
  137. Files are collected into a hierarchical organization called a
  138. .I "file tree
  139. starting in a
  140. .I directory
  141. called the
  142. .IR root .
  143. File names, also called
  144. .IR paths ,
  145. consist of a number of
  146. .BR / -separated
  147. .IR "path elements"
  148. with the slashes corresponding to directories.
  149. A path element must contain only printable
  150. characters (those outside
  151. .SM ASCII
  152. and Latin-1 control space)
  153. that occupy no more than
  154. .B NAMELEN-1
  155. bytes.
  156. A path element cannot contain a space or slash.
  157. .PP
  158. When a process presents a file name to Plan 9, it is
  159. .I evaluated
  160. by the following algorithm.
  161. Start with a directory that depends on the first
  162. character of the path:
  163. .L /
  164. means the root of the main hierarchy,
  165. .L #
  166. means the separate root of a kernel device's file tree (see Section 3),
  167. and anything else means the process's current working directory.
  168. Then for each path element, look up the element
  169. in the directory, advance to that directory,
  170. do a possible translation (see below), and repeat.
  171. The last step may yield a directory or regular file.
  172. The collection of files reachable from the root is called the
  173. .I "name space
  174. of a process.
  175. .PP
  176. A program can use
  177. .I bind
  178. or
  179. .I mount
  180. (see
  181. .IR bind (2))
  182. to say that whenever a specified file is reached during evaluation,
  183. evaluation instead continues from a second specified file.
  184. Also, the same system calls create
  185. .IR "union directories" ,
  186. which are concatenations of ordinary directories
  187. that are searched sequentially until the desired element is found.
  188. Using
  189. .I bind
  190. and
  191. .I mount
  192. to do name space adjustment affects only
  193. the current process group (see below).
  194. Certain conventions about the layout of the name space should
  195. be preserved; see
  196. .IR namespace (4).
  197. .SS "File I/O"
  198. Files are opened for input or output
  199. by
  200. .I open
  201. or
  202. .I create
  203. (see
  204. .IR open (2)).
  205. These calls return an integer called a
  206. .IR "file descriptor"
  207. which identifies the file
  208. to subsequent I/O calls,
  209. notably
  210. .IR read (2)
  211. and
  212. .IR write .
  213. The system allocates the numbers by selecting the lowest unused descriptor.
  214. They are allocated dynamically; there is no visible limit to the number of file
  215. descriptors a process may have open.
  216. They may be reassigned using
  217. .IR dup (2).
  218. File descriptors are indices into a
  219. kernel resident
  220. .IR "file descriptor table" .
  221. Each process has an associated file descriptor table.
  222. In some cases (see
  223. .I rfork
  224. in
  225. .IR fork (2))
  226. a file descriptor table may be shared by several processes.
  227. .PP
  228. By convention,
  229. file descriptor 0 is the standard input,
  230. 1 is the standard output,
  231. and 2 is the standard error output.
  232. With one exception, the operating system is unaware of these conventions;
  233. it is permissible to close file 0,
  234. or even to replace it by a file open only for writing,
  235. but many programs will be confused by such chicanery.
  236. The exception is that the system prints messages about broken processes
  237. to file descriptor 2.
  238. .PP
  239. Files are normally read or written in sequential order.
  240. The I/O position in the file is called the
  241. .IR "file offset"
  242. and may be set arbitrarily using the
  243. .IR seek (2)
  244. system call.
  245. .PP
  246. Directories may be opened and read much like regular files.
  247. They contain an integral number of records, called
  248. .IR "directory entries" ,
  249. of length
  250. .B DIRLEN
  251. (defined in
  252. .BR <libc.h> ).
  253. Each entry is a machine-independent representation of
  254. the information about an existing file in the directory,
  255. including the name, ownership,
  256. permission,
  257. access dates,
  258. and so on.
  259. The entry
  260. corresponding to an arbitrary file can be retrieved by
  261. .IR stat (2)
  262. or
  263. .IR fstat ;
  264. .I wstat
  265. and
  266. .I fwstat
  267. write back entries, thus changing the properties of a file.
  268. An entry may be translated into a more convenient, addressable
  269. form called a
  270. .B Dir
  271. structure;
  272. .IR dirstat ,
  273. .IR dirfstat,
  274. .IR dirwstat ,
  275. and
  276. .I dirfwstat
  277. execute the appropriate translations (see
  278. .IR stat (2)).
  279. .PP
  280. New files are made with
  281. .I create
  282. (in
  283. .IR open (2))
  284. and deleted with
  285. .IR remove (2).
  286. Directories may not directly be written;
  287. .IR create ,
  288. .IR remove ,
  289. .IR wstat ,
  290. and
  291. .I fwstat
  292. alter them.
  293. .PP
  294. The operating system kernel records the file name used to access each open file or directory.
  295. If the file is opened by a local name (one that does not begin
  296. .B /
  297. or
  298. .BR # ),
  299. the system makes the stored name absolute by prefixing
  300. the string associated with the current directory.
  301. Similar lexical adjustments are made for path names containing
  302. .B .
  303. (dot) or
  304. .B ..
  305. (dot-dot).
  306. By this process, the system maintains a record of the route by which each file was accessed.
  307. Although there is a possibility for error\(emthe name is not maintained after the file is opened,
  308. so removals and renamings can confound it\(emthis simple method usually
  309. permits the system to return, via the
  310. .IR fd2path (2)
  311. system call and related calls such as
  312. .IR getwd (2),
  313. a valid name that may be used to find a file again.
  314. This is also the source of the names reported in the name space listing of
  315. .IR ns (1)
  316. or
  317. .B /dev/ns
  318. (see
  319. .IR proc (3)).
  320. .PP
  321. .IR Pipe (2)
  322. creates a connected pair of file descriptors,
  323. useful for bidirectional local communication.
  324. .SS "Process execution and control"
  325. A new process is created
  326. when an existing one calls
  327. .I rfork
  328. with the
  329. .B RFPROC
  330. bit set, usually just by calling
  331. .IR fork (2).
  332. The new (child) process starts out with
  333. copies of the address space and most other attributes
  334. of the old (parent) process.
  335. In particular,
  336. the child starts out running
  337. the same program as the parent;
  338. .IR exec (2)
  339. will bring in a different one.
  340. .PP
  341. Each process has a unique integer process id;
  342. a set of open files, indexed by file descriptor;
  343. and a current working directory
  344. (changed by
  345. .IR chdir (2)).
  346. .PP
  347. Each process has a set of attributes \(em memory, open files,
  348. name space, etc. \(em that may be shared or unique.
  349. Flags to
  350. .IR rfork
  351. control the sharing of these attributes.
  352. .PP
  353. The memory of a process is divided into
  354. .IR segments .
  355. Every program has at least a
  356. .I text
  357. (instruction) and
  358. .I stack
  359. segment.
  360. Most also have an initialized
  361. .I data
  362. segment and a segment of zero-filled data called
  363. .IR bss .
  364. Processes may
  365. .IR segattach (2)
  366. other segments for special purposes.
  367. .PP
  368. A process terminates by calling
  369. .IR exits (2).
  370. A parent process may call
  371. .I wait
  372. (in
  373. .IR exits (2))
  374. to wait for some child to terminate.
  375. A string of status information
  376. may be passed from
  377. .I exits
  378. to
  379. .IR wait .
  380. A process can go to sleep for a specified time by calling
  381. .IR sleep (2).
  382. .PP
  383. There is a
  384. .I notification
  385. mechanism for telling a process about events such as address faults,
  386. floating point faults, and messages from other processes.
  387. A process uses
  388. .IR notify (2)
  389. to register the function to be called (the
  390. .IR "notification handler" )
  391. when such events occur.
  392. .SS Multithreading
  393. By calling
  394. .I rfork
  395. with the
  396. .B RFMEM
  397. bit set, a program may create several independently executing processes sharing the same
  398. memory (except for the stack segment, which is unique to each process).
  399. Where possible according to the ANSI C standard,
  400. the main C library works properly in multiprocess programs;
  401. .IR malloc ,
  402. .IR print ,
  403. and the other routines use locks (see
  404. .IR lock (2))
  405. to synchronize access to their data structures.
  406. The graphics library defined in
  407. .B <draw.h>
  408. is also multi-process capable; details are in
  409. .IR graphics (2).
  410. In general, though, multiprocess programs should use some form of synchronization
  411. to protect shared data.
  412. .PP
  413. The thread library, defined in
  414. .BR <thread.h> ,
  415. provides support for multiprocess programs.
  416. It includes a data structure called a
  417. .B Channel
  418. that can be used to send messages between processes,
  419. and coroutine-like
  420. .IR threads ,
  421. which enable multiple threads of control within a single process.
  422. The threads within a process are scheduled by the library, but there is
  423. no pre-emptive scheduling within a process; thread switching occurs
  424. only at communication or synchronization points.
  425. .PP
  426. Most programs using the thread library
  427. comprise multiple processes
  428. communicating over channels, and within some processes,
  429. multiple threads. Since Plan 9 I/O calls may block, a system
  430. call may block all the threads in a process.
  431. Therefore, a program that shouldn't block unexpectedly will use a process
  432. to serve the I/O request, passing the result to the main processes
  433. over a channel when the request completes.
  434. For an example of this design, see
  435. .IR mouse (2).
  436. .SH SEE ALSO
  437. .IR nm (1),
  438. .IR 2l (1),
  439. .IR 2c (1)
  440. .SH DIAGNOSTICS
  441. Math functions in
  442. .I libc
  443. return
  444. special values when the function is undefined for the
  445. given arguments or when the value is not representable
  446. (see
  447. .IR nan (2)).
  448. .PP
  449. Some of the functions in
  450. .I libc
  451. are system calls and many others employ system calls in their implementation.
  452. All system calls return integers,
  453. with \-1 indicating that an error occurred;
  454. .IR errstr (2)
  455. recovers a string describing the error.
  456. Some user-level library functions also use the
  457. .I errstr
  458. mechanism to report errors.
  459. Functions that may affect the value of the error string are said to ``set
  460. .IR errstr '';
  461. it is understood that the error string is altered only if an error occurs.
  462. .SH BUGS
  463. The limitation of path elements to 27 bytes
  464. .RB ( NAMELEN-1 )
  465. is a liability in the networked world.