p3 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. .NH
  2. Implementation
  3. .PP
  4. It is now time to describe the implementation of some of the occam-specific
  5. features such as channels and \fBNOW\fP. Also the way communication with
  6. UNIX\(dg is performed must be described.
  7. .FS
  8. \(dg UNIX is a trademark of Bell Laboratories
  9. .FE
  10. For a thorough description of the library routines to simulate parallelism,
  11. which are e.g. used by the channel routines and by the \fBPAR\fP construct
  12. in Appendix B, see [6].
  13. .NH 2
  14. Channels
  15. .PP
  16. There are currently two types of channels (see Figure 1.) indicated by the type
  17. field of a channel variable:
  18. .IP -
  19. An interprocess communication channel with two additional fields:
  20. .RS
  21. .IP -
  22. A synchronization field to hold the state of an interprocess communication
  23. channel.
  24. .IP -
  25. An integer variable to hold the value to be send.
  26. .RE
  27. .IP -
  28. An outside world communication channel. This is a member of an array of
  29. channels connected to UNIX files. Its additional fields are:
  30. .RS
  31. .IP -
  32. A flags field holding a readahead flag and a flag that tells if this channel
  33. variable is currently connected to a file.
  34. .IP -
  35. A preread character, if readahead is done.
  36. .IP -
  37. An index field to find the corresponding UNIX file.
  38. .RE
  39. .LP
  40. .PS
  41. box ht 3.0 wid 3.0
  42. box ht 0.75 wid 0.75 with .nw at 1st box.nw + (0.5, -0.5) "Process 1"
  43. box ht 0.75 wid 0.75 with .ne at 1st box.ne + (-0.5, -0.5) "Process 2"
  44. box ht 0.75 wid 0.75 with .sw at 1st box.sw + (0.5, 0.5) "Process 3"
  45. box ht 0.75 wid 0.75 with .se at 1st box.se + (-0.5, 0.5) "Process 4"
  46. line right from 5/12 <2nd box.ne, 2nd box.se> to 5/12 <3nd box.nw, 3nd box.sw>
  47. line right from 7/12 <2nd box.ne, 2nd box.se> to 7/12 <3nd box.nw, 3nd box.sw>
  48. line right from 5/12 <4th box.ne, 4th box.se> to 5/12 <5nd box.nw, 5nd box.sw>
  49. line right from 7/12 <4th box.ne, 4th box.se> to 7/12 <5nd box.nw, 5nd box.sw>
  50. line down from 5/12 <2nd box.sw, 2nd box.se> to 5/12 <4nd box.nw, 4nd box.ne>
  51. line down from 7/12 <2nd box.sw, 2nd box.se> to 7/12 <4nd box.nw, 4nd box.ne>
  52. line down from 5/12 <3rd box.sw, 3rd box.se> to 5/12 <5nd box.nw, 5nd box.ne>
  53. line down from 7/12 <3rd box.sw, 3rd box.se> to 7/12 <5nd box.nw, 5nd box.ne>
  54. line right 1.0 from 5/12 <5th box.ne, 5th box.se>
  55. line right 1.0 from 7/12 <5th box.ne, 5th box.se>
  56. line left 1.0 from 5/12 <2nd box.nw, 2nd box.sw>
  57. line left 1.0 from 7/12 <2nd box.nw, 2nd box.sw>
  58. .PE
  59. .DS C
  60. \fIFigure 1. Interprocess and outside world communication channels\fP
  61. .DE
  62. The basic channel handling is done by \f(CWchan_in\fP and \f(CWchan_out\fP. All
  63. other routines are based on them. The routine \f(CWchan_any\fP only checks if
  64. there's a value available on a given channel. (It does not read this value!)
  65. \f(CWC_init\fP initializes an array of interprocess communication channels.
  66. .LP
  67. The following table shows Occam statements paired with the routines used to
  68. execute them.
  69. .TS H
  70. center, box;
  71. c | c | c
  72. lf5 | lf5 | lf5.
  73. Occam statement Channel handling routine Called as
  74. =
  75. .sp 0.5
  76. .TH
  77. T{
  78. .nf
  79. CHAN c:
  80. CHAN c[z]:
  81. .fi
  82. T} T{
  83. .nf
  84. c_init(c, z)
  85. chan *c; unsigned z;
  86. .fi
  87. T} T{
  88. .nf
  89. c_init(&c, 1);
  90. c_init(&c, z);
  91. .fi
  92. T}
  93. .sp 0.5
  94. _
  95. .sp 0.5
  96. T{
  97. .nf
  98. c ? v
  99. .fi
  100. T} T{
  101. .nf
  102. chan_in(v, c)
  103. long *v; chan *c;
  104. .fi
  105. T} T{
  106. .nf
  107. chan_in(&v, &c);
  108. .fi
  109. T}
  110. .sp 0.5
  111. T{
  112. .nf
  113. c ? b[byte i]
  114. .fi
  115. T} T{
  116. .nf
  117. cbyte_in(b, c)
  118. char *b; chan *c;
  119. .fi
  120. T} T{
  121. .nf
  122. cbyte_in(&b[i], &c);
  123. .fi
  124. T}
  125. .sp 0.5
  126. T{
  127. .nf
  128. c ? a[i for z]
  129. .fi
  130. T} T{
  131. .nf
  132. c_wa_in(a, z, c)
  133. long *a; unsigned z; chan *c;
  134. .fi
  135. T} T{
  136. .nf
  137. c_wa_in(&a[i], z, &c);
  138. .fi
  139. T}
  140. .sp 0.5
  141. T{
  142. .nf
  143. c ? a[byte i for z]
  144. .fi
  145. T} T{
  146. .nf
  147. c_ba_in(a, z, c)
  148. long *a; unsigned z; chan *c;
  149. .fi
  150. T} T{
  151. .nf
  152. c_ba_in(&a[i], z, &c);
  153. .fi
  154. T}
  155. .sp 0.5
  156. _
  157. .sp 0.5
  158. T{
  159. .nf
  160. c ! v
  161. .fi
  162. T} T{
  163. .nf
  164. chan_out(v, c)
  165. long *v; chan *c;
  166. .fi
  167. T} T{
  168. .nf
  169. chan_out(&v, &c);
  170. .fi
  171. T}
  172. .sp 0.5
  173. T{
  174. .nf
  175. c ! a[i for z]
  176. .fi
  177. T} T{
  178. .nf
  179. c_wa_out(a, z, c)
  180. long *a; unsigned z; chan *c;
  181. .fi
  182. T} T{
  183. .nf
  184. c_wa_out(&a[i], z, &c);
  185. .fi
  186. T}
  187. .sp 0.5
  188. T{
  189. .nf
  190. c ! a[byte i for z]
  191. .fi
  192. T} T{
  193. .nf
  194. c_ba_out(a, z, c)
  195. long *a; unsigned z; chan *c;
  196. .fi
  197. T} T{
  198. .nf
  199. c_ba_out(&a[i], z, &c);
  200. .fi
  201. T}
  202. .sp 0.5
  203. _
  204. .sp 0.5
  205. T{
  206. .nf
  207. alt
  208. c ? ....
  209. ....
  210. .fi
  211. T} T{
  212. .nf
  213. int chan_any(c)
  214. chan *c;
  215. .fi
  216. T} T{
  217. .nf
  218. deadlock=0;
  219. for(;;) {
  220. if (chan_any(&c)) {
  221. ....
  222. ....
  223. .fi
  224. T}
  225. .sp 0.5
  226. .TE
  227. The code of \f(CWc_init\fP, \f(CWchan_in\fP, \f(CWchan_out\fP and \f(CWchan_any\fP
  228. can be found in Appendix A.
  229. .NH 3
  230. Synchronization on interprocess communication channels
  231. .PP
  232. The synchronization field can hold three different values indicating the
  233. state the channel is in:
  234. .IP "- \fBC\(ulS\(ulFREE\fP:" 15
  235. Ground state, channel not in use.
  236. .IP "- \fBC\(ulS\(ulANY\fP:" 15
  237. Channel holds a value, the sending process is waiting for an acknowledgement
  238. about its receipt.
  239. .IP "- \fBC\(ulS\(ulACK\fP:" 15
  240. Channel data has been removed by a receiving process, the sending process can
  241. set the channel free now.
  242. .LP
  243. A sending process cannot simply wait until the channel changes state C\(ulS\(ulANY
  244. to state C\(ulS\(ulFREE before it continues. There is a third state needed to prevent
  245. a third process from using the channel before our sending process is
  246. acknowledged. Note, however that it is not allowed to use a channel for input
  247. or output in more than one parallel process. This is too difficult to check
  248. in practice, so we tried to smooth it a little.
  249. .NH 2
  250. NOW
  251. .PP
  252. \fBNOW\fP evaluates to the current time returned by the time(2) system call.
  253. The code is simply:
  254. .DS
  255. .ft CW
  256. .nf
  257. long now()
  258. {
  259. deadlock=0;
  260. return time((long *) 0);
  261. }
  262. .fi
  263. .ft
  264. .DE
  265. The ``deadlock=0'' prevents deadlocks while using the clock.
  266. .NH 2
  267. UNIX interface
  268. .PP
  269. To handle the communication with the outside world the following channels are
  270. defined:
  271. .IP -
  272. \fBinput\fP, that corresponds with the standard input file,
  273. .IP -
  274. \fBoutput\fP, that corresponds with the standard output file,
  275. .IP -
  276. \fBerror\fP, that corresponds with the standard error file.
  277. .IP -
  278. \fBfile\fP, an array of channels that can be subscripted with an index
  279. obtained by the builtin named process ``\f(CWopen\fP''. Note that
  280. \fBinput\fP=\fBfile\fP[0], \fBoutput\fP=\fBfile\fP[1] and
  281. \fBerror\fP=\fBfile\fP[2].
  282. .LP
  283. Builtin named processes to open and close files are defined as
  284. .DS
  285. .nf
  286. .ft CW
  287. proc open(var index, value name[], mode[]) = ..... :
  288. proc close(value index) = ..... :
  289. .fi
  290. .ft
  291. .DE
  292. To open a file `junk', write nonsense onto it, and close it, goes as follows:
  293. .DS
  294. .ft CW
  295. .nf
  296. var i:
  297. seq
  298. open(i, "junk", "w")
  299. file[i] ! nonsense
  300. close(i)
  301. .fi
  302. .ft
  303. .DE
  304. Errors opening a file are reported by a negative index, which is the
  305. negative value of the error number (called \fIerrno\fP in UNIX).
  306. .LP
  307. Bytes read from or written onto these channels are taken from occam variables.
  308. As these variables can hold more than 256 values, some negative values are used
  309. to control channels. These values are:
  310. .IP "- \fBEOF\fP" 9
  311. (-1): Eof from file channel is read as -1.
  312. .IP "- \fBTEXT\fP" 9
  313. (-2): A -2 written onto any channel connected to a terminal puts this
  314. terminal in the normal line oriented mode (i.e. characters typed are echoed
  315. and lines are buffered before they are read).
  316. .IP "- \fBRAW\fP" 9
  317. (-3): A -3 written onto any channel connected to a terminal puts it in raw mode
  318. (i.e. no echoing of typed characters and no line buffering).
  319. .LP
  320. To exit an Occam program, e.g. after an error, a builtin named process
  321. \f(CWexit\fP is available that takes an exit code as its argument.
  322. .NH 2
  323. Replicators and slices
  324. .PP
  325. Both the base and the count of replicators like in
  326. .DS
  327. .ft CW
  328. par i = [ base for count ]
  329. .ft
  330. .DE
  331. may be arbitrary expressions. The count in array slices like in
  332. .DS
  333. .ft CW
  334. c ? A[ base for count ]
  335. .ft
  336. .DE
  337. must be a constant expression however, the base is again free.