1
0

0intro 11 KB

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