home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / MAILBOX.ZIP / MPTHD.TEX < prev    next >
LaTeX Document  |  1988-06-27  |  33.9 KB

open in: MacOS 8.1     |     Win98     |     DOS

view JSON data     |     view as text

This file was processed as: LaTeX Document (document/latex).

You can browse this item here: MPTHD.TEX

ConfidenceProgramDetectionMatch TypeSupport
100% dexvert LaTeX Document (document/latex) magic Supported
1% dexvert Corel 10 Texture (image/corel10Texture) ext Unsupported
1% dexvert Croteam texture file (image/croteamTextureFile) ext Unsupported
1% dexvert Text File (text/txt) fallback Supported
100% file LaTeX document text default
99% file LaTeX document, ASCII text, with CRLF line terminators default
100% TrID LaTeX 2e document (with rem) default
100% checkBytes Printable ASCII default
100% perlTextCheck Likely Text (Perl) default
100% siegfried fmt/281 LaTeX (Subdocument) default
100% detectItEasy Format: plain text[CRLF] default (weak)
100% xdgMime text/x-matlab default (weak)



hex view
+--------+-------------------------+-------------------------+--------+--------+
|00000000| 25 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |%*******|********|
|00000010| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00000020| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00000030| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00000040| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2f 0d 0a |********|*****/..|
|00000050| 25 2a 20 6d 70 74 68 64 | 2e 74 65 78 20 2d 2d 20 |%* mpthd|.tex -- |
|00000060| 45 44 55 43 41 54 49 4f | 4e 41 4c 20 41 52 54 49 |EDUCATIO|NAL ARTI|
|00000070| 43 4c 45 20 44 45 53 43 | 52 49 42 49 4e 47 20 54 |CLE DESC|RIBING T|
|00000080| 48 45 20 54 48 52 45 41 | 44 20 43 4c 55 53 54 45 |HE THREA|D CLUSTE|
|00000090| 52 20 20 09 20 20 20 20 | 2a 2f 0d 0a 25 2a 20 43 |R . |*/..%* C|
|000000a0| 72 65 61 74 65 64 3a 20 | 20 73 65 65 20 62 65 6c |reated: | see bel|
|000000b0| 6f 77 09 09 52 65 6c 65 | 61 73 65 3a 20 20 73 65 |ow..Rele|ase: se|
|000000c0| 65 20 62 65 6c 6f 77 09 | 56 65 72 73 69 6f 6e 3a |e below.|Version:|
|000000d0| 20 20 73 65 65 20 62 65 | 6c 6f 77 20 2a 2f 0d 0a | see be|low */..|
|000000e0| 25 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |%*******|********|
|000000f0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00000100| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00000110| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00000120| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 0d 0a 25 |********|*****..%|
|00000130| 28 63 29 20 43 6f 70 79 | 72 69 67 68 74 20 31 39 |(c) Copy|right 19|
|00000140| 38 37 20 62 79 20 4d 69 | 63 68 61 65 6c 20 42 65 |87 by Mi|chael Be|
|00000150| 6e 6a 61 6d 69 6e 20 50 | 61 72 6b 65 72 20 20 20 |njamin P|arker |
|00000160| 20 20 20 20 20 20 20 20 | 28 55 53 41 20 53 53 23 | |(USA SS#|
|00000170| 20 35 35 37 2d 34 39 2d | 34 31 33 30 29 0d 0a 25 | 557-49-|4130)..%|
|00000180| 0d 0a 25 41 6c 6c 20 52 | 69 67 68 74 73 20 52 65 |..%All R|ights Re|
|00000190| 73 65 72 76 65 64 20 75 | 6e 6c 65 73 73 20 73 70 |served u|nless sp|
|000001a0| 65 63 69 66 69 65 64 20 | 69 6e 20 74 68 65 20 66 |ecified |in the f|
|000001b0| 6f 6c 6c 6f 77 69 6e 67 | 20 69 6e 63 6c 75 64 65 |ollowing| include|
|000001c0| 20 66 69 6c 65 73 3a 20 | 2a 2f 0d 0a 25 23 69 6e | files: |*/..%#in|
|000001d0| 63 6c 75 64 65 20 22 6d | 70 74 73 6b 2e 63 70 79 |clude "m|ptsk.cpy|
|000001e0| 22 20 2f 2a 0d 0a 25 0d | 0a 25 44 4f 20 4e 4f 54 |" /*..%.|.%DO NOT|
|000001f0| 20 52 45 4d 4f 56 45 20 | 4f 52 20 41 4c 54 45 52 | REMOVE |OR ALTER|
|00000200| 20 54 48 49 53 20 4e 4f | 54 49 43 45 20 41 4e 44 | THIS NO|TICE AND|
|00000210| 20 49 54 53 20 50 52 4f | 56 49 53 49 4f 4e 53 2e | ITS PRO|VISIONS.|
|00000220| 0d 0a 25 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |..%*****|********|
|00000230| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00000240| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00000250| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00000260| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2f |********|*******/|
|00000270| 0d 0a 25 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |..%*****|********|
|00000280| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00000290| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|000002a0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|000002b0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2f |********|*******/|
|000002c0| 0d 0a 25 20 4f 56 45 52 | 56 49 45 57 3a 0d 0a 25 |..% OVER|VIEW:..%|
|000002d0| 20 54 68 69 73 20 61 72 | 74 69 63 6c 65 20 74 61 | This ar|ticle ta|
|000002e0| 6c 6b 73 20 61 62 6f 75 | 74 20 74 68 65 20 63 6f |lks abou|t the co|
|000002f0| 6e 63 65 70 74 20 6f 66 | 20 63 6f 6e 74 65 78 74 |ncept of| context|
|00000300| 20 73 77 69 74 63 68 69 | 6e 67 2c 20 61 6e 64 20 | switchi|ng, and |
|00000310| 69 74 73 0d 0a 25 20 65 | 78 61 6d 70 6c 65 20 69 |its..% e|xample i|
|00000320| 6d 70 6c 65 6d 65 6e 74 | 61 74 69 6f 6e 20 62 79 |mplement|ation by|
|00000330| 20 74 68 65 20 4d 50 54 | 48 44 20 43 4c 55 53 54 | the MPT|HD CLUST|
|00000340| 45 52 2e 0d 0a 25 0d 0a | 25 20 49 74 20 69 73 20 |ER...%..|% It is |
|00000350| 66 6f 72 6d 61 74 74 65 | 64 20 75 73 69 6e 67 20 |formatte|d using |
|00000360| 74 68 65 20 70 75 62 6c | 69 63 20 64 6f 6d 61 69 |the publ|ic domai|
|00000370| 6e 20 4c 61 54 65 58 2c | 20 61 6e 20 65 6e 68 61 |n LaTeX,| an enha|
|00000380| 6e 63 65 6d 65 6e 74 20 | 6f 66 0d 0a 25 20 44 6f |ncement |of..% Do|
|00000390| 6e 61 6c 64 20 4b 6e 75 | 74 68 27 73 20 54 65 58 |nald Knu|th's TeX|
|000003a0| 20 74 65 78 74 20 66 6f | 72 6d 61 74 74 69 6e 67 | text fo|rmatting|
|000003b0| 20 73 79 73 74 65 6d 2e | 20 20 20 42 6f 74 68 20 | system.| Both |
|000003c0| 61 72 65 20 69 6e 20 74 | 68 65 20 70 75 62 6c 69 |are in t|he publi|
|000003d0| 63 20 64 6f 6d 61 69 6e | 2e 0d 0a 25 2a 2a 2a 2a |c domain|...%****|
|000003e0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|000003f0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00000400| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00000410| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00000420| 2a 2a 2a 2a 2a 2a 2a 2a | 2f 0d 0a 25 2a 2a 2a 2a |********|/..%****|
|00000430| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00000440| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00000450| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00000460| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00000470| 2a 2a 2a 2a 2a 2a 2a 2a | 2f 0d 0a 5c 64 6f 63 75 |********|/..\docu|
|00000480| 6d 65 6e 74 73 74 79 6c | 65 7b 61 72 74 69 63 6c |mentstyl|e{articl|
|00000490| 65 7d 0d 0a 5c 70 61 67 | 65 73 74 79 6c 65 7b 68 |e}..\pag|estyle{h|
|000004a0| 65 61 64 69 6e 67 73 7d | 0d 0a 0d 0a 5c 74 69 74 |eadings}|....\tit|
|000004b0| 6c 65 7b 0d 0a 42 75 69 | 6c 64 20 41 5c 5c 0d 0a |le{..Bui|ld A\\..|
|000004c0| 7b 5c 65 6d 20 50 6f 72 | 74 61 62 6c 65 7d 20 4d |{\em Por|table} M|
|000004d0| 75 6c 74 69 74 61 73 6b | 69 6e 67 20 45 6e 76 69 |ultitask|ing Envi|
|000004e0| 72 6f 6e 6d 65 6e 74 0d | 0a 5c 5c 20 69 6e 20 60 |ronment.|.\\ in `|
|000004f0| 60 43 27 27 5c 5c 0d 0a | 2d 2d 2d 2d 2d 2d 2d 2d |`C''\\..|--------|
|00000500| 2d 2d 2d 2d 2d 2d 2d 2d | 2d 2d 2d 2d 2d 2d 2d 2d |--------|--------|
|00000510| 2d 2d 2d 2d 2d 2d 2d 2d | 2d 0d 0a 7b 5c 65 6d 20 |--------|-..{\em |
|00000520| 5c 23 31 20 2d 2d 20 53 | 77 69 74 63 68 69 6e 67 |\#1 -- S|witching|
|00000530| 20 43 6f 6e 74 65 78 74 | 73 7d 7d 0d 0a 0d 0a 5c | Context|s}}....\|
|00000540| 61 75 74 68 6f 72 7b 0d | 0a 43 6f 70 79 72 69 67 |author{.|.Copyrig|
|00000550| 68 74 20 5c 63 6f 70 79 | 72 69 67 68 74 20 31 39 |ht \copy|right 19|
|00000560| 38 37 5c 74 68 61 6e 6b | 73 7b 0d 0a 54 68 69 73 |87\thank|s{..This|
|00000570| 20 69 73 20 61 20 62 65 | 74 61 20 72 65 6c 65 61 | is a be|ta relea|
|00000580| 73 65 20 6f 66 20 61 6e | 20 61 72 74 69 63 6c 65 |se of an| article|
|00000590| 20 69 6e 74 65 6e 64 65 | 64 20 66 6f 72 20 42 79 | intende|d for By|
|000005a0| 74 65 20 4d 61 67 61 7a | 69 6e 65 2e 20 20 44 6f |te Magaz|ine. Do|
|000005b0| 0d 0a 6e 6f 74 20 63 6f | 70 79 20 6e 6f 72 20 64 |..not co|py nor d|
|000005c0| 69 73 74 72 69 62 75 74 | 65 2e 20 20 54 68 69 73 |istribut|e. This|
|000005d0| 20 76 65 72 73 69 6f 6e | 20 6f 66 20 74 68 65 20 | version| of the |
|000005e0| 61 72 74 69 63 6c 65 20 | 69 73 20 69 6e 74 65 6e |article |is inten|
|000005f0| 64 65 64 20 66 6f 72 0d | 0a 74 68 65 20 72 65 76 |ded for.|.the rev|
|00000600| 69 65 77 20 61 6e 64 20 | 63 6f 6d 6d 65 6e 74 20 |iew and |comment |
|00000610| 6f 66 20 74 68 65 20 4d | 49 54 20 63 6f 6d 6d 75 |of the M|IT commu|
|00000620| 6e 69 74 79 20 70 72 69 | 6f 72 20 74 6f 20 74 68 |nity pri|or to th|
|00000630| 65 20 72 65 6c 65 61 73 | 65 20 69 6e 0d 0a 42 79 |e releas|e in..By|
|00000640| 74 65 20 4d 61 67 61 7a | 69 6e 65 20 6f 72 20 6f |te Magaz|ine or o|
|00000650| 74 68 65 72 20 70 65 72 | 69 6f 64 69 63 61 6c 2e |ther per|iodical.|
|00000660| 20 20 59 6f 75 72 20 74 | 68 6f 75 67 68 74 73 20 | Your t|houghts |
|00000670| 61 6e 64 20 73 75 67 67 | 65 73 74 69 6f 6e 73 0d |and sugg|estions.|
|00000680| 0a 61 70 70 72 65 63 69 | 61 74 65 64 2e 0d 0a 7d |.appreci|ated...}|
|00000690| 5c 5c 0d 0a 4d 69 63 68 | 61 65 6c 20 42 65 6e 6a |\\..Mich|ael Benj|
|000006a0| 61 6d 69 6e 20 50 61 72 | 6b 65 72 2c 20 4d 49 54 |amin Par|ker, MIT|
|000006b0| 20 27 38 39 20 5c 5c 0d | 0a 7c 5c 5c 0d 0a 45 61 | '89 \\.|.|\\..Ea|
|000006c0| 73 74 20 43 61 6d 70 75 | 73 20 2d 20 4d 75 6e 2e |st Campu|s - Mun.|
|000006d0| 20 33 30 33 5c 5c 0d 0a | 33 20 41 6d 65 73 20 53 | 303\\..|3 Ames S|
|000006e0| 74 72 65 65 74 5c 5c 0d | 0a 43 61 6d 62 72 69 64 |treet\\.|.Cambrid|
|000006f0| 67 65 2c 20 4d 41 5c 5c | 0d 0a 36 31 37 2d 32 32 |ge, MA\\|..617-22|
|00000700| 35 2d 36 33 30 33 0d 0a | 7d 0d 0a 0d 0a 5c 64 61 |5-6303..|}....\da|
|00000710| 74 65 7b 43 72 65 61 74 | 65 64 3a 20 31 31 2f 32 |te{Creat|ed: 11/2|
|00000720| 30 2f 38 37 20 5c 5c 20 | 52 65 6c 65 61 73 65 3a |0/87 \\ |Release:|
|00000730| 20 30 2e 37 20 5c 5c 20 | 56 65 72 73 69 6f 6e 3a | 0.7 \\ |Version:|
|00000740| 20 31 32 2f 31 30 2f 38 | 37 7d 0d 0a 0d 0a 5c 62 | 12/10/8|7}....\b|
|00000750| 65 67 69 6e 7b 64 6f 63 | 75 6d 65 6e 74 7d 0d 0a |egin{doc|ument}..|
|00000760| 5c 6d 61 6b 65 74 69 74 | 6c 65 0d 0a 0d 0a 5c 62 |\maketit|le....\b|
|00000770| 65 67 69 6e 7b 71 75 6f | 74 65 7d 0d 0a 0d 0a 54 |egin{quo|te}....T|
|00000780| 68 69 73 20 69 73 20 74 | 68 65 20 66 69 72 73 74 |his is t|he first|
|00000790| 20 69 6e 20 61 20 73 65 | 72 69 65 73 20 6f 66 20 | in a se|ries of |
|000007a0| 74 68 72 65 65 20 63 6f | 6e 73 74 72 75 63 74 69 |three co|nstructi|
|000007b0| 6f 6e 20 61 72 74 69 63 | 6c 65 73 20 69 6e 20 77 |on artic|les in w|
|000007c0| 68 69 63 68 0d 0a 79 6f | 75 20 62 75 69 6c 64 20 |hich..yo|u build |
|000007d0| 61 20 7b 5c 65 6d 20 70 | 6f 72 74 61 62 6c 65 7d |a {\em p|ortable}|
|000007e0| 20 6d 75 6c 74 69 74 61 | 73 6b 69 6e 67 20 65 6e | multita|sking en|
|000007f0| 76 69 72 6f 6e 6d 65 6e | 74 20 66 6f 72 20 70 72 |vironmen|t for pr|
|00000800| 6f 67 72 61 6d 6d 69 6e | 67 2e 0d 0a 54 68 65 20 |ogrammin|g...The |
|00000810| 70 75 72 70 6f 73 65 20 | 6f 66 20 74 68 65 20 73 |purpose |of the s|
|00000820| 65 72 69 65 73 20 69 73 | 20 74 77 6f 2d 66 6f 6c |eries is| two-fol|
|00000830| 64 3a 0d 0a 0d 0a 5c 62 | 65 67 69 6e 7b 65 6e 75 |d:....\b|egin{enu|
|00000840| 6d 65 72 61 74 65 7d 0d | 0a 0d 0a 09 5c 69 74 65 |merate}.|....\ite|
|00000850| 6d 20 7b 5c 62 66 20 54 | 6f 20 45 78 70 6c 61 69 |m {\bf T|o Explai|
|00000860| 6e 7d 20 74 68 65 20 70 | 72 6f 67 72 61 6d 6d 69 |n} the p|rogrammi|
|00000870| 6e 67 20 63 6f 6e 63 65 | 70 74 73 20 61 6e 64 20 |ng conce|pts and |
|00000880| 63 6f 6e 73 74 72 75 63 | 74 73 0d 0a 77 68 69 63 |construc|ts..whic|
|00000890| 68 20 63 6f 6d 65 20 69 | 6e 74 6f 20 70 6c 61 79 |h come i|nto play|
|000008a0| 20 77 68 65 6e 20 62 75 | 69 6c 64 69 6e 67 2c 20 | when bu|ilding, |
|000008b0| 70 72 6f 67 72 61 6d 6d | 69 6e 67 2c 20 61 6e 64 |programm|ing, and|
|000008c0| 20 6f 70 65 72 61 74 69 | 6e 67 20 77 69 74 68 69 | operati|ng withi|
|000008d0| 6e 0d 0a 61 20 6d 75 6c | 74 69 74 61 73 6b 69 6e |n..a mul|titaskin|
|000008e0| 67 20 65 6e 76 69 72 6f | 6e 6d 65 6e 74 2e 0d 0a |g enviro|nment...|
|000008f0| 0d 0a 09 5c 69 74 65 6d | 20 7b 5c 62 66 20 54 6f |...\item| {\bf To|
|00000900| 20 43 6f 6e 73 74 72 75 | 63 74 7d 20 61 20 67 65 | Constru|ct} a ge|
|00000910| 6e 65 72 61 6c 2d 70 75 | 72 70 6f 73 65 20 6d 75 |neral-pu|rpose mu|
|00000920| 6c 74 69 74 61 73 6b 69 | 6e 67 20 65 6e 76 69 72 |ltitaski|ng envir|
|00000930| 6f 6e 6d 65 6e 74 0d 0a | 66 6f 72 20 70 72 6f 67 |onment..|for prog|
|00000940| 72 61 6d 6d 69 6e 67 20 | 77 68 69 63 68 20 69 73 |ramming |which is|
|00000950| 20 7b 5c 65 6d 20 70 6f | 72 74 61 62 6c 65 7d 20 | {\em po|rtable} |
|00000960| 74 6f 20 6d 6f 73 74 20 | 73 74 61 63 6b 2d 62 61 |to most |stack-ba|
|00000970| 73 65 64 20 6c 61 6e 67 | 75 61 67 65 73 0d 0a 61 |sed lang|uages..a|
|00000980| 6e 64 20 74 6f 20 6d 6f | 73 74 20 6d 6f 73 74 20 |nd to mo|st most |
|00000990| 6d 69 6e 69 2c 20 6d 69 | 63 72 6f 2c 20 61 6e 64 |mini, mi|cro, and|
|000009a0| 20 6d 61 69 6e 66 72 61 | 6d 65 20 63 6f 6d 70 75 | mainfra|me compu|
|000009b0| 74 65 72 73 20 28 73 6f | 20 79 6f 75 72 0d 0a 6d |ters (so| your..m|
|000009c0| 75 6c 74 69 74 61 73 6b | 69 6e 67 20 70 72 6f 67 |ultitask|ing prog|
|000009d0| 72 61 6d 73 20 77 69 6c | 6c 20 62 65 20 70 6f 72 |rams wil|l be por|
|000009e0| 74 61 62 6c 65 29 2e 0d | 0a 0d 0a 5c 65 6e 64 7b |table)..|...\end{|
|000009f0| 65 6e 75 6d 65 72 61 74 | 65 7d 0d 0a 0d 0a 54 68 |enumerat|e}....Th|
|00000a00| 65 20 60 60 74 61 73 6b | 27 27 20 69 73 20 64 69 |e ``task|'' is di|
|00000a10| 76 69 64 65 64 20 75 70 | 20 69 6e 74 6f 20 74 68 |vided up| into th|
|00000a20| 72 65 65 20 63 6f 6e 73 | 74 72 75 63 74 69 6f 6e |ree cons|truction|
|00000a30| 20 61 72 74 69 63 6c 65 | 73 2c 20 65 61 63 68 20 | article|s, each |
|00000a40| 6f 66 20 77 68 69 63 68 | 0d 0a 62 75 69 6c 64 73 |of which|..builds|
|00000a50| 20 75 70 6f 6e 20 74 68 | 65 20 63 6f 64 65 20 61 | upon th|e code a|
|00000a60| 6e 64 20 63 6f 6e 63 65 | 70 74 73 20 6f 66 20 74 |nd conce|pts of t|
|00000a70| 68 65 20 70 72 65 76 69 | 6f 75 73 20 6f 6e 65 20 |he previ|ous one |
|00000a80| 62 75 74 20 69 73 20 63 | 6f 6d 70 6c 65 74 65 20 |but is c|omplete |
|00000a90| 69 6e 0d 0a 69 74 73 65 | 6c 66 20 28 69 74 20 64 |in..itse|lf (it d|
|00000aa0| 6f 65 73 20 6e 6f 74 20 | 64 65 70 65 6e 64 20 6f |oes not |depend o|
|00000ab0| 6e 20 74 68 65 20 73 75 | 63 63 65 73 73 69 76 65 |n the su|ccessive|
|00000ac0| 20 61 72 74 69 63 6c 65 | 73 29 2e 20 20 54 68 65 | article|s). The|
|00000ad0| 20 74 68 72 65 65 20 61 | 72 74 69 63 6c 65 73 0d | three a|rticles.|
|00000ae0| 0a 61 72 65 3a 0d 0a 0d | 0a 5c 62 65 67 69 6e 7b |.are:...|.\begin{|
|00000af0| 64 65 73 63 72 69 70 74 | 69 6f 6e 7d 0d 0a 0d 0a |descript|ion}....|
|00000b00| 09 5c 69 74 65 6d 5b 5c | 23 31 5d 09 7b 5c 65 6d |.\item[\|#1].{\em|
|00000b10| 20 53 77 69 74 63 68 69 | 6e 67 20 43 6f 6e 74 65 | Switchi|ng Conte|
|00000b20| 78 74 73 7d 0d 0a 09 5c | 69 74 65 6d 5b 5c 23 32 |xts}...\|item[\#2|
|00000b30| 5d 09 7b 5c 65 6d 20 4f | 72 67 61 6e 69 7a 69 6e |].{\em O|rganizin|
|00000b40| 67 20 61 20 54 61 73 6b | 20 46 6f 72 63 65 7d 0d |g a Task| Force}.|
|00000b50| 0a 09 5c 69 74 65 6d 5b | 5c 23 33 5d 09 7b 5c 65 |..\item[|\#3].{\e|
|00000b60| 6d 20 4d 61 6e 61 67 69 | 6e 67 20 52 65 73 6f 75 |m Managi|ng Resou|
|00000b70| 72 63 65 73 7d 0d 0a 0d | 0a 5c 65 6e 64 7b 64 65 |rces}...|.\end{de|
|00000b80| 73 63 72 69 70 74 69 6f | 6e 7d 0d 0a 0d 0a 54 68 |scriptio|n}....Th|
|00000b90| 69 73 20 61 72 74 69 63 | 6c 65 20 69 73 20 74 68 |is artic|le is th|
|00000ba0| 65 20 66 69 72 73 74 20 | 69 6e 20 74 68 65 20 73 |e first |in the s|
|00000bb0| 65 72 69 65 73 2e 20 20 | 49 74 20 7b 5c 62 66 20 |eries. |It {\bf |
|00000bc0| 65 78 70 6c 61 69 6e 73 | 7d 20 74 68 65 0d 0a 63 |explains|} the..c|
|00000bd0| 6f 6e 63 65 70 74 20 6f | 66 20 63 6f 6e 74 65 78 |oncept o|f contex|
|00000be0| 74 20 73 77 69 74 63 68 | 69 6e 67 20 61 6e 64 20 |t switch|ing and |
|00000bf0| 7b 5c 62 66 20 63 6f 6e | 73 74 72 75 63 74 73 7d |{\bf con|structs}|
|00000c00| 20 61 20 67 65 6e 65 72 | 61 6c 2d 70 75 72 70 6f | a gener|al-purpo|
|00000c10| 73 65 0d 0a 61 6e 64 20 | 7b 5c 65 6d 20 70 6f 72 |se..and |{\em por|
|00000c20| 74 61 62 6c 65 7d 20 63 | 6f 6e 74 65 78 74 20 73 |table} c|ontext s|
|00000c30| 77 69 74 63 68 2e 2e 2e | 0d 0a 0d 0a 5c 65 6e 64 |witch...|....\end|
|00000c40| 7b 71 75 6f 74 65 7d 0d | 0a 0d 0a 5c 74 61 62 6c |{quote}.|...\tabl|
|00000c50| 65 6f 66 63 6f 6e 74 65 | 6e 74 73 0d 0a 0d 0a 48 |eofconte|nts....H|
|00000c60| 61 76 65 20 79 6f 75 20 | 65 76 65 72 20 70 6c 61 |ave you |ever pla|
|00000c70| 79 65 64 20 6f 6e 65 20 | 6f 66 20 74 68 6f 73 65 |yed one |of those|
|00000c80| 20 6e 65 61 74 2c 20 6e | 65 77 2c 20 73 6f 70 68 | neat, n|ew, soph|
|00000c90| 69 73 74 69 63 61 74 65 | 64 20 76 69 64 65 6f 20 |isticate|d video |
|00000ca0| 67 61 6d 65 73 0d 0a 61 | 6e 64 20 77 6f 6e 64 65 |games..a|nd wonde|
|00000cb0| 72 65 64 20 68 6f 77 20 | 69 74 20 77 6f 72 6b 73 |red how |it works|
|00000cc0| 3f 20 20 48 6f 77 20 69 | 74 20 77 61 73 20 77 72 |? How i|t was wr|
|00000cd0| 69 74 74 65 6e 3f 20 20 | 48 6f 77 20 74 68 65 20 |itten? |How the |
|00000ce0| 67 61 6d 65 20 66 6c 69 | 65 73 0d 0a 61 6c 69 65 |game fli|es..alie|
|00000cf0| 6e 20 73 70 61 63 65 73 | 68 69 70 73 20 61 63 72 |n spaces|hips acr|
|00000d00| 6f 73 73 20 74 68 65 20 | 73 63 72 65 65 6e 2c 20 |oss the |screen, |
|00000d10| 66 69 72 65 73 20 74 72 | 61 63 6b 69 6e 67 20 6d |fires tr|acking m|
|00000d20| 69 73 73 69 6c 65 73 20 | 77 68 65 6e 65 76 65 72 |issiles |whenever|
|00000d30| 0d 0a 79 6f 75 20 68 69 | 74 20 74 68 65 20 70 68 |..you hi|t the ph|
|00000d40| 61 73 65 72 20 62 75 74 | 74 6f 6e 2c 20 61 6e 64 |aser but|ton, and|
|00000d50| 20 62 6c 6f 77 73 20 61 | 77 61 79 20 74 68 65 20 | blows a|way the |
|00000d60| 75 6e 69 76 65 72 73 65 | 20 77 68 65 6e 20 6d 69 |universe| when mi|
|00000d70| 73 73 69 6c 65 0d 0a 61 | 6e 64 20 65 6e 65 6d 79 |ssile..a|nd enemy|
|00000d80| 20 63 6f 6c 6c 69 64 65 | 3f 20 20 49 74 27 73 20 | collide|? It's |
|00000d90| 61 20 73 63 72 65 65 6e | 20 66 75 6c 6c 20 6f 66 |a screen| full of|
|00000da0| 20 6d 61 6e 79 20 69 6e | 64 65 70 65 6e 64 65 6e | many in|dependen|
|00000db0| 74 20 61 63 74 69 76 69 | 74 69 65 73 0d 0a 67 6f |t activi|ties..go|
|00000dc0| 69 6e 67 20 6f 6e 20 61 | 74 20 6f 6e 63 65 2c 20 |ing on a|t once, |
|00000dd0| 79 65 74 20 73 79 6e 63 | 68 72 6f 6e 69 7a 69 6e |yet sync|hronizin|
|00000de0| 67 20 61 74 20 6a 75 73 | 74 20 74 68 65 20 72 69 |g at jus|t the ri|
|00000df0| 67 68 74 20 6d 6f 6d 65 | 6e 74 20 74 6f 20 67 69 |ght mome|nt to gi|
|00000e00| 76 65 0d 0a 74 68 65 20 | 76 69 64 65 6f 20 67 61 |ve..the |video ga|
|00000e10| 6d 65 20 69 74 73 20 6d | 61 67 69 63 21 0d 0a 0d |me its m|agic!...|
|00000e20| 0a 56 69 64 65 6f 20 67 | 61 6d 65 73 20 61 72 65 |.Video g|ames are|
|00000e30| 20 63 6f 6d 70 6c 65 78 | 20 73 79 73 74 65 6d 73 | complex| systems|
|00000e40| 2e 20 20 54 68 65 79 20 | 69 6e 76 6f 6c 76 65 20 |. They |involve |
|00000e50| 6d 61 6e 79 20 61 73 79 | 6e 63 68 72 6f 6e 6f 75 |many asy|nchronou|
|00000e60| 73 0d 0a 65 6e 74 69 74 | 69 65 73 20 28 74 68 65 |s..entit|ies (the|
|00000e70| 20 6d 69 73 73 69 6c 65 | 73 2c 20 74 68 65 20 61 | missile|s, the a|
|00000e80| 6c 69 65 6e 20 73 68 69 | 70 73 2c 20 65 76 65 6e |lien shi|ps, even|
|00000e90| 20 79 6f 75 72 20 6f 77 | 6e 20 73 68 69 70 29 20 | your ow|n ship) |
|00000ea0| 77 68 69 63 68 0d 0a 6d | 75 73 74 20 73 79 6e 63 |which..m|ust sync|
|00000eb0| 68 72 6f 6e 69 7a 65 20 | 6f 6e 20 62 6f 74 68 20 |hronize |on both |
|00000ec0| 69 6e 74 65 72 6e 61 6c | 20 65 76 65 6e 74 73 20 |internal| events |
|00000ed0| 28 63 6f 6c 6c 69 73 69 | 6f 6e 73 29 20 61 6e 64 |(collisi|ons) and|
|00000ee0| 20 65 78 74 65 72 6e 61 | 6c 20 6f 6e 65 73 0d 0a | externa|l ones..|
|00000ef0| 28 62 75 74 74 6f 6e 20 | 70 72 65 73 73 65 73 29 |(button |presses)|
|00000f00| 2e 20 20 54 68 65 79 20 | 63 61 6e 20 62 65 20 63 |. They |can be c|
|00000f10| 68 61 6c 6c 65 6e 67 69 | 6e 67 20 74 6f 20 70 6c |hallengi|ng to pl|
|00000f20| 61 79 2c 20 61 6e 64 20 | 65 76 65 6e 20 6d 6f 72 |ay, and |even mor|
|00000f30| 65 0d 0a 63 68 61 6c 6c | 65 6e 67 69 6e 67 20 74 |e..chall|enging t|
|00000f40| 6f 20 77 72 69 74 65 2c | 20 75 6e 6c 65 73 73 20 |o write,| unless |
|00000f50| 79 6f 75 20 70 6f 73 73 | 65 73 73 20 74 68 65 20 |you poss|ess the |
|00000f60| 72 69 67 68 74 20 61 62 | 73 74 72 61 63 74 69 6f |right ab|stractio|
|00000f70| 6e 73 2e 2e 2e 0d 0a 0d | 0a 50 72 6f 67 72 61 6d |ns......|.Program|
|00000f80| 73 20 6c 69 6b 65 20 76 | 69 64 65 6f 20 67 61 6d |s like v|ideo gam|
|00000f90| 65 73 20 77 69 74 68 20 | 6d 61 6e 79 20 69 6e 64 |es with |many ind|
|00000fa0| 65 70 65 6e 64 65 6e 74 | 20 65 6e 74 69 74 69 65 |ependent| entitie|
|00000fb0| 73 20 64 6f 20 6e 6f 74 | 0d 0a 72 65 61 64 69 6c |s do not|..readil|
|00000fc0| 79 20 64 65 63 6f 6d 70 | 6f 73 65 20 69 6e 74 6f |y decomp|ose into|
|00000fd0| 20 6f 70 65 72 61 74 69 | 6f 6e 73 20 61 6e 64 20 | operati|ons and |
|00000fe0| 73 75 62 2d 6f 70 65 72 | 61 74 69 6f 6e 73 2c 20 |sub-oper|ations, |
|00000ff0| 69 6e 74 6f 20 61 20 73 | 69 6e 67 6c 65 0d 0a 68 |into a s|ingle..h|
|00001000| 69 65 72 61 72 63 68 61 | 6c 20 74 61 73 6b 2e 20 |ierarcha|l task. |
|00001010| 20 41 20 7b 5c 62 66 20 | 73 65 71 75 65 6e 74 69 | A {\bf |sequenti|
|00001020| 61 6c 7d 20 6e 6f 72 20 | 61 20 7b 5c 62 66 20 66 |al} nor |a {\bf f|
|00001030| 75 6e 63 74 69 6f 6e 61 | 6c 20 66 6c 6f 77 7d 0d |unctiona|l flow}.|
|00001040| 0a 64 6f 65 73 6e 27 74 | 20 73 65 65 6d 20 74 6f |.doesn't| seem to|
|00001050| 20 66 69 74 2e 20 20 41 | 73 20 69 6e 20 6c 69 66 | fit. A|s in lif|
|00001060| 65 2c 20 74 68 69 6e 67 | 73 20 61 72 65 20 6e 61 |e, thing|s are na|
|00001070| 74 75 72 61 6c 6c 79 20 | 67 6f 69 6e 67 20 6f 6e |turally |going on|
|00001080| 20 69 6e 0d 0a 7b 5c 62 | 66 20 70 61 72 61 6c 6c | in..{\b|f parall|
|00001090| 65 6c 7d 2c 20 61 6e 64 | 20 65 76 65 6e 20 69 66 |el}, and| even if|
|000010a0| 20 79 6f 75 20 68 61 76 | 65 20 6f 6e 6c 79 20 6f | you hav|e only o|
|000010b0| 6e 65 20 70 65 72 73 6f | 6e 20 28 6f 72 20 70 72 |ne perso|n (or pr|
|000010c0| 6f 63 65 73 73 6f 72 29 | 20 74 6f 0d 0a 64 6f 20 |ocessor)| to..do |
|000010d0| 74 68 65 6d 2c 20 79 6f | 75 20 66 69 6e 64 20 74 |them, yo|u find t|
|000010e0| 68 65 72 65 20 61 72 65 | 20 6f 74 68 65 72 20 74 |here are| other t|
|000010f0| 68 69 6e 67 73 20 79 6f | 75 20 63 61 6e 20 64 6f |hings yo|u can do|
|00001100| 20 77 68 69 6c 65 20 77 | 61 69 74 69 6e 67 20 66 | while w|aiting f|
|00001110| 6f 72 0d 0a 73 6f 6d 65 | 74 68 69 6e 67 20 65 6c |or..some|thing el|
|00001120| 73 65 2e 20 20 4f 66 74 | 65 6e 2c 20 77 68 65 74 |se. Oft|en, whet|
|00001130| 68 65 72 20 62 75 69 6c | 64 69 6e 67 20 6f 70 65 |her buil|ding ope|
|00001140| 72 61 74 69 6e 67 20 73 | 79 73 74 65 6d 73 20 6f |rating s|ystems o|
|00001150| 72 20 6d 6f 64 65 6c 69 | 6e 67 0d 0a 73 70 61 63 |r modeli|ng..spac|
|00001160| 65 20 77 61 72 73 2c 20 | 74 68 65 20 70 72 6f 62 |e wars, |the prob|
|00001170| 6c 65 6d 20 64 65 63 6f | 6d 70 6f 73 65 73 20 69 |lem deco|mposes i|
|00001180| 6e 74 6f 20 6d 61 6e 79 | 2c 20 69 6e 64 65 70 65 |nto many|, indepe|
|00001190| 6e 64 65 6e 74 20 74 61 | 73 6b 73 2c 20 65 61 63 |ndent ta|sks, eac|
|000011a0| 68 0d 0a 6c 69 76 69 6e | 67 20 61 73 79 6e 63 68 |h..livin|g asynch|
|000011b0| 72 6f 6e 6f 75 73 6c 79 | 20 69 6e 20 69 74 73 20 |ronously| in its |
|000011c0| 6f 77 6e 20 7b 5c 62 66 | 20 63 6f 6e 74 65 78 74 |own {\bf| context|
|000011d0| 7d 2e 2e 2e 0d 0a 0d 0a | 7b 5c 62 66 20 49 4e 20 |}.......|{\bf IN |
|000011e0| 54 48 49 53 20 41 52 54 | 49 43 4c 45 7d 20 77 65 |THIS ART|ICLE} we|
|000011f0| 20 77 69 6c 6c 20 65 78 | 70 6c 6f 72 65 20 74 68 | will ex|plore th|
|00001200| 65 73 65 20 63 6f 6e 74 | 65 78 74 73 20 69 6e 20 |ese cont|exts in |
|00001210| 77 68 69 63 68 20 61 20 | 7b 5c 62 66 20 74 68 72 |which a |{\bf thr|
|00001220| 65 61 64 7d 0d 0a 6f 66 | 20 70 72 6f 67 72 61 6d |ead}..of| program|
|00001230| 20 63 6f 6e 74 72 6f 6c | 20 65 78 65 63 75 74 65 | control| execute|
|00001240| 73 2e 20 20 46 75 6e 64 | 61 6d 65 6e 74 61 6c 20 |s. Fund|amental |
|00001250| 74 6f 20 74 68 65 20 69 | 64 65 61 20 6f 66 20 7b |to the i|dea of {|
|00001260| 5c 62 66 0d 0a 6d 75 6c | 74 69 74 61 73 6b 69 6e |\bf..mul|titaskin|
|00001270| 67 7d 20 69 73 20 74 68 | 65 20 61 62 69 6c 69 74 |g} is th|e abilit|
|00001280| 79 20 74 6f 20 62 65 67 | 69 6e 2c 20 72 65 73 74 |y to beg|in, rest|
|00001290| 61 72 74 20 5c 26 20 73 | 75 73 70 65 6e 64 2c 20 |art \& s|uspend, |
|000012a0| 61 6e 64 20 66 69 6e 61 | 6c 6c 79 0d 0a 65 6e 64 |and fina|lly..end|
|000012b0| 20 74 68 72 65 61 64 73 | 20 6f 66 20 65 78 65 63 | threads| of exec|
|000012c0| 75 74 69 6f 6e 2e 20 20 | 54 68 65 20 63 6f 6e 74 |ution. |The cont|
|000012d0| 65 78 74 20 73 77 69 74 | 63 68 20 77 65 20 65 78 |ext swit|ch we ex|
|000012e0| 70 6c 6f 72 65 20 68 65 | 72 65 20 70 61 63 6b 73 |plore he|re packs|
|000012f0| 0d 0a 73 75 63 68 20 7b | 5c 65 6d 20 66 6c 6f 77 |..such {|\em flow|
|00001300| 20 63 6f 6e 74 72 6f 6c | 7d 20 69 6e 74 6f 20 61 | control|} into a|
|00001310| 20 73 69 6e 67 6c 65 20 | 63 6f 6e 73 74 72 75 63 | single |construc|
|00001320| 74 2c 20 61 6e 64 20 6f | 66 66 65 72 73 20 69 74 |t, and o|ffers it|
|00001330| 20 74 6f 20 74 68 65 0d | 0a 70 72 6f 67 72 61 6d | to the.|.program|
|00001340| 6d 65 72 20 69 6e 20 61 | 20 66 61 6d 69 6c 69 61 |mer in a| familia|
|00001350| 72 20 66 6f 72 6d 3a 20 | 74 68 65 20 66 75 6e 63 |r form: |the func|
|00001360| 74 69 6f 6e 20 63 61 6c | 6c 2e 20 20 43 75 72 72 |tion cal|l. Curr|
|00001370| 65 6e 74 6c 79 20 72 75 | 6e 6e 69 6e 67 0d 0a 6f |ently ru|nning..o|
|00001380| 6e 20 62 6f 74 68 20 42 | 65 72 6b 65 6c 65 79 20 |n both B|erkeley |
|00001390| 34 2e 33 20 55 4e 49 58 | 20 61 6e 64 20 4d 53 2d |4.3 UNIX| and MS-|
|000013a0| 44 4f 53 2c 20 69 74 73 | 20 60 60 43 27 27 20 69 |DOS, its| ``C'' i|
|000013b0| 6d 70 6c 65 6d 65 6e 74 | 61 74 69 6f 6e 20 69 73 |mplement|ation is|
|000013c0| 0d 0a 68 69 67 68 6c 79 | 20 70 6f 72 74 61 62 6c |..highly| portabl|
|000013d0| 65 2c 20 61 6e 64 20 69 | 74 73 20 63 6c 65 61 6e |e, and i|ts clean|
|000013e0| 20 66 75 6e 63 74 69 6f | 6e 61 6c 69 74 79 20 72 | functio|nality r|
|000013f0| 65 64 75 63 65 73 20 74 | 68 65 20 6e 65 65 64 20 |educes t|he need |
|00001400| 66 6f 72 0d 0a 67 72 75 | 6e 67 79 20 6d 61 63 68 |for..gru|ngy mach|
|00001410| 69 6e 65 2d 64 65 70 65 | 6e 64 65 6e 74 20 63 6f |ine-depe|ndent co|
|00001420| 64 65 20 6f 6e 20 68 69 | 67 68 65 72 20 6c 65 76 |de on hi|gher lev|
|00001430| 65 6c 73 2e 20 20 53 75 | 62 73 65 71 75 65 6e 74 |els. Su|bsequent|
|00001440| 20 61 72 74 69 63 6c 65 | 73 0d 0a 77 69 6c 6c 20 | article|s..will |
|00001450| 62 75 69 6c 64 20 75 70 | 6f 6e 20 74 68 69 73 20 |build up|on this |
|00001460| 66 6c 65 78 69 62 69 6c | 69 74 79 20 61 6e 64 20 |flexibil|ity and |
|00001470| 70 6f 77 65 72 20 74 6f | 20 63 6f 6e 73 74 72 75 |power to| constru|
|00001480| 63 74 20 61 20 6d 75 6c | 74 69 74 61 73 6b 69 6e |ct a mul|titaskin|
|00001490| 67 0d 0a 61 6e 64 20 70 | 6f 74 65 6e 74 69 61 6c |g..and p|otential|
|000014a0| 6c 79 20 7b 5c 62 66 20 | 6d 75 6c 74 69 70 72 6f |ly {\bf |multipro|
|000014b0| 63 65 73 73 69 6e 67 7d | 20 65 6e 76 69 72 6f 6e |cessing}| environ|
|000014c0| 6d 65 6e 74 20 77 69 74 | 68 69 6e 20 60 60 43 27 |ment wit|hin ``C'|
|000014d0| 27 3b 20 62 61 73 65 64 | 0d 0a 6f 6e 20 74 68 65 |'; based|..on the|
|000014e0| 20 63 6f 6e 74 65 78 74 | 20 73 77 69 74 63 68 2c | context| switch,|
|000014f0| 20 74 68 65 20 65 6e 76 | 69 72 6f 6e 6d 65 6e 74 | the env|ironment|
|00001500| 20 77 6f 75 6c 64 20 62 | 65 20 70 6f 72 74 61 62 | would b|e portab|
|00001510| 6c 65 20 74 6f 20 62 6f | 74 68 20 55 4e 49 58 0d |le to bo|th UNIX.|
|00001520| 0a 61 6e 64 20 4d 53 2d | 44 4f 53 2c 20 61 6e 64 |.and MS-|DOS, and|
|00001530| 20 70 72 6f 76 69 64 65 | 20 62 6f 74 68 20 7b 5c | provide| both {\|
|00001540| 62 66 20 69 6e 74 65 72 | 70 72 6f 63 65 73 73 20 |bf inter|process |
|00001550| 63 6f 6d 6d 75 6e 69 63 | 61 74 69 6f 6e 7d 20 61 |communic|ation} a|
|00001560| 6e 64 20 7b 5c 62 66 0d | 0a 72 65 73 6f 75 72 63 |nd {\bf.|.resourc|
|00001570| 65 20 6d 61 6e 61 67 65 | 6d 65 6e 74 7d 20 2d 2d |e manage|ment} --|
|00001580| 69 6e 20 6f 74 68 65 72 | 20 77 6f 72 64 73 2c 20 |in other| words, |
|00001590| 6c 6f 74 73 20 6f 66 20 | 70 6f 77 65 72 20 66 6f |lots of |power fo|
|000015a0| 72 20 70 72 6f 67 72 61 | 6d 73 2e 0d 0a 53 6f 75 |r progra|ms...Sou|
|000015b0| 6e 64 73 20 6c 69 6b 65 | 20 66 75 6e 3f 20 20 54 |nds like| fun? T|
|000015c0| 68 65 6e 20 6c 65 74 27 | 73 20 67 65 74 20 73 74 |hen let'|s get st|
|000015d0| 61 72 74 65 64 21 0d 0a | 0d 0a 5c 73 65 63 74 69 |arted!..|..\secti|
|000015e0| 6f 6e 7b 57 68 61 74 27 | 73 20 61 20 43 6f 6e 74 |on{What'|s a Cont|
|000015f0| 65 78 74 20 53 77 69 74 | 63 68 3f 7d 0d 0a 0d 0a |ext Swit|ch?}....|
|00001600| 49 6e 20 74 68 65 20 74 | 72 61 64 69 74 69 6f 6e |In the t|radition|
|00001610| 61 6c 20 7b 5c 62 66 20 | 56 61 6e 20 4e 6f 79 6d |al {\bf |Van Noym|
|00001620| 61 6e 20 63 6f 6d 70 75 | 74 65 72 20 61 72 63 68 |an compu|ter arch|
|00001630| 69 74 65 63 74 75 72 65 | 7d 2c 20 74 68 65 72 65 |itecture|}, there|
|00001640| 20 69 73 20 73 6f 6d 65 | 0d 0a 73 75 62 73 65 74 | is some|..subset|
|00001650| 20 6f 66 20 69 6e 66 6f | 72 6d 61 74 69 6f 6e 20 | of info|rmation |
|00001660| 77 68 69 63 68 20 64 65 | 66 69 6e 65 73 20 65 78 |which de|fines ex|
|00001670| 61 63 74 6c 79 20 77 68 | 61 74 20 73 74 61 74 65 |actly wh|at state|
|00001680| 20 61 20 70 72 6f 63 65 | 73 73 6f 72 20 69 73 20 | a proce|ssor is |
|00001690| 69 6e 2e 0d 0a 54 68 69 | 73 20 7b 5c 62 66 20 73 |in...Thi|s {\bf s|
|000016a0| 74 61 74 65 7d 2c 20 69 | 6e 20 63 6f 6d 62 69 6e |tate}, i|n combin|
|000016b0| 61 74 69 6f 6e 20 77 69 | 74 68 20 6f 74 68 65 72 |ation wi|th other|
|000016c0| 20 69 6e 70 75 74 73 20 | 66 72 6f 6d 20 6d 65 6d | inputs |from mem|
|000016d0| 6f 72 79 20 6f 72 20 49 | 2f 4f 20 64 65 76 69 63 |ory or I|/O devic|
|000016e0| 65 73 2c 0d 0a 64 65 66 | 69 6e 65 73 20 65 78 61 |es,..def|ines exa|
|000016f0| 63 74 6c 79 20 74 68 65 | 20 73 65 71 75 65 6e 63 |ctly the| sequenc|
|00001700| 65 20 6f 66 20 66 75 74 | 75 72 65 20 73 74 61 74 |e of fut|ure stat|
|00001710| 65 73 2e 20 20 46 6f 72 | 20 74 68 65 20 6d 6f 73 |es. For| the mos|
|00001720| 74 20 70 61 72 74 2c 20 | 74 68 65 0d 0a 73 74 61 |t part, |the..sta|
|00001730| 74 65 20 6f 66 20 6d 65 | 6d 6f 72 79 20 61 6e 64 |te of me|mory and|
|00001740| 20 49 2f 4f 20 64 65 76 | 69 63 65 73 20 69 73 20 | I/O dev|ices is |
|00001750| 61 73 73 75 6d 65 64 20 | 74 6f 20 62 65 20 7b 5c |assumed |to be {\|
|00001760| 62 66 20 73 74 61 74 69 | 63 7d 20 28 75 6e 63 68 |bf stati|c} (unch|
|00001770| 61 6e 67 69 6e 67 29 0d | 0a 75 6e 6c 65 73 73 20 |anging).|.unless |
|00001780| 74 68 65 20 70 72 6f 63 | 65 73 73 6f 72 20 65 78 |the proc|essor ex|
|00001790| 70 6c 69 63 69 74 6c 79 | 20 63 68 61 6e 67 65 73 |plicitly| changes|
|000017a0| 20 74 68 65 6d 20 28 69 | 66 20 74 68 65 79 20 61 | them (i|f they a|
|000017b0| 72 65 20 63 68 61 6e 67 | 65 64 20 62 79 20 73 6f |re chang|ed by so|
|000017c0| 6d 65 0d 0a 6f 74 68 65 | 72 20 65 6e 74 69 74 79 |me..othe|r entity|
|000017d0| 20 61 73 20 61 6e 20 49 | 2f 4f 20 64 65 76 69 63 | as an I|/O devic|
|000017e0| 65 2c 20 20 74 68 65 20 | 70 72 6f 63 65 73 73 6f |e, the |processo|
|000017f0| 72 20 69 73 20 75 73 75 | 61 6c 6c 79 0d 0a 6e 6f |r is usu|ally..no|
|00001800| 74 69 66 69 65 64 20 62 | 79 20 61 6e 20 7b 5c 62 |tified b|y an {\b|
|00001810| 66 20 69 6e 74 65 72 72 | 75 70 74 7d 20 66 6f 72 |f interr|upt} for|
|00001820| 20 65 66 66 69 63 69 65 | 6e 63 79 20 2d 2d 20 6d | efficie|ncy -- m|
|00001830| 6f 72 65 20 6f 6e 20 74 | 68 69 73 20 6c 61 74 65 |ore on t|his late|
|00001840| 72 29 2e 20 20 4f 6e 20 | 74 68 65 20 6f 74 68 65 |r). On |the othe|
|00001850| 72 20 68 61 6e 64 2c 0d | 0a 74 68 65 20 70 72 6f |r hand,.|.the pro|
|00001860| 63 65 73 73 6f 72 20 73 | 74 61 74 65 20 63 68 61 |cessor s|tate cha|
|00001870| 6e 67 65 73 20 63 6f 6e | 74 69 6e 75 61 6c 6c 79 |nges con|tinually|
|00001880| 20 61 6e 64 20 60 60 63 | 6f 6e 73 63 69 6f 75 73 | and ``c|onscious|
|00001890| 6c 79 27 27 20 77 69 74 | 68 20 65 76 65 72 79 0d |ly'' wit|h every.|
|000018a0| 0a 6f 70 65 72 61 74 69 | 6f 6e 2e 0d 0a 0d 0a 57 |.operati|on.....W|
|000018b0| 65 20 63 61 6c 6c 20 74 | 68 69 73 20 73 70 65 63 |e call t|his spec|
|000018c0| 69 61 6c 20 73 74 61 74 | 65 20 74 68 65 20 7b 5c |ial stat|e the {\|
|000018d0| 62 66 20 63 6f 6e 74 65 | 78 74 7d 20 6f 66 20 61 |bf conte|xt} of a|
|000018e0| 20 70 72 6f 63 65 73 73 | 6f 72 2e 20 20 42 79 20 | process|or. By |
|000018f0| 73 61 76 69 6e 67 0d 0a | 61 6e 64 20 72 65 73 74 |saving..|and rest|
|00001900| 6f 72 69 6e 67 20 28 63 | 68 61 6e 67 69 6e 67 29 |oring (c|hanging)|
|00001910| 20 74 68 65 20 63 6f 6e | 74 65 78 74 20 6f 66 20 | the con|text of |
|00001920| 74 68 65 20 70 72 6f 63 | 65 73 73 6f 72 2c 20 77 |the proc|essor, w|
|00001930| 65 20 63 61 6e 20 61 6c | 6c 6f 77 20 6f 6e 65 0d |e can al|low one.|
|00001940| 0a 70 72 6f 63 65 73 73 | 6f 72 20 74 6f 20 64 6f |.process|or to do|
|00001950| 20 74 68 65 20 77 6f 72 | 6b 20 6f 66 20 6d 61 6e | the wor|k of man|
|00001960| 79 2c 20 6d 61 6b 69 6e | 67 20 69 74 20 70 6f 73 |y, makin|g it pos|
|00001970| 73 69 62 6c 65 20 74 6f | 20 6d 61 6e 61 67 65 20 |sible to| manage |
|00001980| 74 68 65 0d 0a 63 6f 6d | 70 75 74 65 72 27 73 20 |the..com|puter's |
|00001990| 70 72 6f 63 65 73 73 6f | 72 73 20 6d 6f 72 65 20 |processo|rs more |
|000019a0| 65 66 66 69 63 69 65 6e | 74 6c 79 2e 20 20 57 65 |efficien|tly. We|
|000019b0| 20 63 61 6e 20 61 6c 73 | 6f 20 63 6f 6e 73 74 72 | can als|o constr|
|000019c0| 75 63 74 20 74 68 65 20 | 75 73 65 66 75 6c 0d 0a |uct the |useful..|
|000019d0| 70 72 6f 67 72 61 6d 6d | 69 6e 67 20 61 62 73 74 |programm|ing abst|
|000019e0| 72 61 63 74 69 6f 6e 20 | 6f 66 20 69 6e 64 65 70 |raction |of indep|
|000019f0| 65 6e 64 65 6e 74 20 7b | 5c 62 66 20 74 68 72 65 |endent {|\bf thre|
|00001a00| 61 64 73 20 6f 66 20 65 | 78 65 63 75 74 69 6f 6e |ads of e|xecution|
|00001a10| 7d 2e 20 20 41 0d 0a 70 | 72 6f 63 65 73 73 6f 72 |}. A..p|rocessor|
|00001a20| 20 63 61 6e 20 65 78 65 | 63 75 74 65 20 6f 6e 65 | can exe|cute one|
|00001a30| 20 74 68 72 65 61 64 20 | 66 6f 72 20 61 20 77 68 | thread |for a wh|
|00001a40| 69 6c 65 2c 20 74 68 65 | 6e 20 67 6f 20 6f 66 66 |ile, the|n go off|
|00001a50| 20 74 6f 20 65 78 65 63 | 75 74 65 0d 0a 6f 74 68 | to exec|ute..oth|
|00001a60| 65 72 20 74 68 72 65 61 | 64 73 2e 20 20 4c 61 74 |er threa|ds. Lat|
|00001a70| 65 72 2c 20 69 74 20 6f | 72 20 61 6e 6f 74 68 65 |er, it o|r anothe|
|00001a80| 72 20 70 72 6f 63 65 73 | 73 6f 72 20 63 61 6e 20 |r proces|sor can |
|00001a90| 63 6f 6e 74 69 6e 75 65 | 20 65 78 65 63 75 74 69 |continue| executi|
|00001aa0| 6e 67 20 74 68 65 0d 0a | 66 69 72 73 74 20 74 68 |ng the..|first th|
|00001ab0| 72 65 61 64 20 77 68 65 | 72 65 20 69 74 20 6c 65 |read whe|re it le|
|00001ac0| 66 74 20 6f 66 66 2e 0d | 0a 0d 0a 52 65 67 61 72 |ft off..|...Regar|
|00001ad0| 64 6c 65 73 73 20 6f 66 | 20 74 68 65 20 6e 75 6d |dless of| the num|
|00001ae0| 62 65 72 20 6f 66 20 70 | 72 6f 63 65 73 73 6f 72 |ber of p|rocessor|
|00001af0| 73 2c 20 66 75 6e 64 61 | 6d 65 6e 74 61 6c 20 74 |s, funda|mental t|
|00001b00| 6f 20 74 68 69 73 20 61 | 63 74 69 76 69 74 79 0d |o this a|ctivity.|
|00001b10| 0a 69 73 20 74 68 65 20 | 70 72 6f 63 65 73 73 6f |.is the |processo|
|00001b20| 72 27 73 20 61 62 69 6c | 69 74 79 20 74 6f 20 73 |r's abil|ity to s|
|00001b30| 61 76 65 20 61 6e 64 20 | 72 65 73 74 6f 72 65 20 |ave and |restore |
|00001b40| 28 65 78 63 68 61 6e 67 | 69 6e 67 29 20 74 68 72 |(exchang|ing) thr|
|00001b50| 65 61 64 20 63 6f 6e 74 | 65 78 74 73 2e 20 20 57 |ead cont|exts. W|
|00001b60| 65 0d 0a 6e 65 65 64 20 | 74 6f 20 70 72 6f 76 69 |e..need |to provi|
|00001b70| 64 65 20 61 20 70 72 6f | 76 69 64 65 20 61 20 70 |de a pro|vide a p|
|00001b80| 72 6f 67 72 61 6d 6d 69 | 6e 67 20 61 62 73 74 72 |rogrammi|ng abstr|
|00001b90| 61 63 74 69 6f 6e 20 74 | 6f 20 65 6e 63 61 70 73 |action t|o encaps|
|00001ba0| 75 6c 61 74 65 20 74 68 | 69 73 0d 0a 70 72 6f 63 |ulate th|is..proc|
|00001bb0| 65 73 73 2e 20 20 57 69 | 74 68 69 6e 20 74 68 65 |ess. Wi|thin the|
|00001bc0| 20 70 6f 77 65 72 20 6f | 66 20 63 6f 6d 6d 6f 6e | power o|f common|
|00001bd0| 20 68 69 67 68 2d 6c 65 | 76 65 6c 20 66 75 6e 63 | high-le|vel func|
|00001be0| 74 69 6f 6e 61 6c 20 6c | 61 6e 67 75 61 67 65 73 |tional l|anguages|
|00001bf0| 2c 0d 0a 74 68 65 72 65 | 20 74 77 6f 20 61 70 70 |,..there| two app|
|00001c00| 72 6f 61 63 68 65 73 20 | 77 65 20 63 6f 75 6c 64 |roaches |we could|
|00001c10| 20 62 75 69 6c 64 3a 0d | 0a 0d 0a 5c 73 75 62 73 | build:.|...\subs|
|00001c20| 65 63 74 69 6f 6e 7b 54 | 68 65 20 4e 6f 2d 52 65 |ection{T|he No-Re|
|00001c30| 74 75 72 6e 20 2f 20 4d | 75 6c 74 69 70 6c 65 2d |turn / M|ultiple-|
|00001c40| 52 65 74 75 72 6e 20 41 | 70 70 72 6f 61 63 68 7d |Return A|pproach}|
|00001c50| 0d 0a 0d 0a 49 6e 20 74 | 68 69 73 20 61 70 70 72 |....In t|his appr|
|00001c60| 6f 61 63 68 2c 20 77 65 | 20 63 72 65 61 74 65 20 |oach, we| create |
|00001c70| 74 77 6f 20 73 70 65 63 | 69 61 6c 20 66 75 6e 63 |two spec|ial func|
|00001c80| 74 69 6f 6e 73 20 66 6f | 72 20 63 6f 6e 74 65 78 |tions fo|r contex|
|00001c90| 74 20 73 77 69 74 63 68 | 69 6e 67 2e 0d 0a 4f 6e |t switch|ing...On|
|00001ca0| 65 20 66 75 6e 63 74 69 | 6f 6e 20 68 61 73 20 74 |e functi|on has t|
|00001cb0| 68 65 20 73 70 65 63 69 | 61 6c 20 70 72 6f 70 65 |he speci|al prope|
|00001cc0| 72 74 79 20 74 68 61 74 | 20 69 74 20 6e 65 76 65 |rty that| it neve|
|00001cd0| 72 20 72 65 74 75 72 6e | 73 2c 20 62 75 74 20 72 |r return|s, but r|
|00001ce0| 65 73 74 6f 72 65 73 0d | 0a 61 20 70 72 69 6f 72 |estores.|.a prior|
|00001cf0| 20 63 6f 6e 74 65 78 74 | 2e 20 20 41 73 20 61 20 | context|. As a |
|00001d00| 63 6f 6d 70 6c 69 6d 65 | 6e 74 2c 20 74 68 65 20 |complime|nt, the |
|00001d10| 6f 74 68 65 72 20 66 75 | 6e 63 74 69 6f 6e 20 68 |other fu|nction h|
|00001d20| 61 73 20 74 68 65 20 73 | 70 65 63 69 61 6c 0d 0a |as the s|pecial..|
|00001d30| 70 72 6f 70 65 72 74 79 | 20 74 68 61 74 20 69 74 |property| that it|
|00001d40| 20 63 61 6e 20 72 65 74 | 75 72 6e 20 6d 75 6c 74 | can ret|urn mult|
|00001d50| 69 70 6c 65 20 74 69 6d | 65 73 20 62 79 20 66 69 |iple tim|es by fi|
|00001d60| 72 73 74 20 73 61 76 69 | 6e 67 20 74 68 65 20 63 |rst savi|ng the c|
|00001d70| 75 72 72 65 6e 74 0d 0a | 63 6f 6e 74 65 78 74 20 |urrent..|context |
|00001d80| 28 73 65 65 20 66 69 67 | 75 72 65 20 62 65 6c 6f |(see fig|ure belo|
|00001d90| 77 29 20 28 43 20 61 6c | 72 65 61 64 79 20 70 72 |w) (C al|ready pr|
|00001da0| 6f 76 69 64 65 73 20 61 | 20 70 72 69 6d 69 74 69 |ovides a| primiti|
|00001db0| 76 65 20 66 6f 72 6d 20 | 6f 66 20 74 68 65 73 65 |ve form |of these|
|00001dc0| 20 66 75 6e 63 74 69 6f | 6e 73 20 77 69 74 68 0d | functio|ns with.|
|00001dd0| 0a 74 68 65 20 24 6c 6f | 6e 67 6a 6d 70 24 20 61 |.the $lo|ngjmp$ a|
|00001de0| 6e 64 20 24 73 65 74 6a | 6d 70 24 20 63 6f 6d 6d |nd $setj|mp$ comm|
|00001df0| 61 6e 64 73 20 72 65 73 | 70 65 63 74 69 76 65 6c |ands res|pectivel|
|00001e00| 79 2c 20 77 68 69 63 68 | 20 61 72 65 0d 0a 74 61 |y, which| are..ta|
|00001e10| 69 6c 6f 72 65 64 20 74 | 6f 20 74 68 65 20 63 6f |ilored t|o the co|
|00001e20| 6d 70 69 6c 65 72 20 6a | 75 73 74 20 74 6f 20 73 |mpiler j|ust to s|
|00001e30| 61 76 65 20 74 68 65 20 | 63 75 72 72 65 6e 74 20 |ave the |current |
|00001e40| 60 60 43 27 27 20 73 74 | 61 74 65 2e 29 20 20 54 |``C'' st|ate.) T|
|00001e50| 68 65 73 65 0d 0a 66 75 | 6e 63 74 69 6f 6e 73 20 |hese..fu|nctions |
|00001e60| 70 72 6f 76 69 64 65 20 | 61 20 67 72 65 61 74 20 |provide |a great |
|00001e70| 66 6c 65 78 69 62 69 6c | 69 74 79 20 61 74 20 74 |flexibil|ity at t|
|00001e80| 68 65 20 63 6f 73 74 20 | 6f 66 20 63 6f 6d 70 6c |he cost |of compl|
|00001e90| 65 78 69 74 79 2e 0d 0a | 54 68 65 20 6e 6f 74 69 |exity...|The noti|
|00001ea0| 6f 6e 20 6f 66 20 61 20 | 66 75 6e 63 74 69 6f 6e |on of a |function|
|00001eb0| 20 74 68 61 74 20 6e 65 | 76 65 72 20 72 65 74 75 | that ne|ver retu|
|00001ec0| 72 6e 73 20 6f 72 20 61 | 20 66 75 6e 63 74 69 6f |rns or a| functio|
|00001ed0| 6e 20 77 68 69 63 68 20 | 72 65 74 75 72 6e 73 0d |n which |returns.|
|00001ee0| 0a 6d 75 6c 74 69 70 6c | 65 20 74 69 6d 65 73 20 |.multipl|e times |
|00001ef0| 69 73 20 61 20 6c 69 74 | 74 6c 65 20 73 74 72 61 |is a lit|tle stra|
|00001f00| 6e 67 65 20 61 6e 64 20 | 75 6e 63 6f 6e 76 65 6e |nge and |unconven|
|00001f10| 74 69 6f 6e 61 6c 2e 20 | 20 41 6e 64 20 69 74 20 |tional. | And it |
|00001f20| 69 73 20 65 61 73 79 20 | 74 6f 0d 0a 60 60 73 63 |is easy |to..``sc|
|00001f30| 72 65 77 20 75 70 27 27 | 20 62 79 20 64 6f 69 6e |rew up''| by doin|
|00001f40| 67 20 73 75 63 68 20 74 | 68 69 6e 67 73 20 61 73 |g such t|hings as|
|00001f50| 20 24 6c 6f 6e 67 6a 6d | 70 24 69 6e 67 20 69 6e | $longjm|p$ing in|
|00001f60| 74 6f 20 61 6e 20 75 6e | 73 61 76 65 64 20 6f 72 |to an un|saved or|
|00001f70| 20 65 78 70 69 72 65 64 | 0d 0a 63 6f 6e 74 65 78 | expired|..contex|
|00001f80| 74 2c 20 6f 72 20 6d 69 | 78 69 6e 67 20 75 70 20 |t, or mi|xing up |
|00001f90| 74 68 65 20 64 69 66 66 | 65 72 65 6e 74 20 72 65 |the diff|erent re|
|00001fa0| 74 75 72 6e 73 20 66 72 | 6f 6d 20 24 73 65 74 6a |turns fr|om $setj|
|00001fb0| 6d 70 24 2e 20 20 53 6f | 6d 65 0d 0a 68 69 67 68 |mp$. So|me..high|
|00001fc0| 65 72 2d 6c 65 76 65 6c | 20 61 70 70 72 6f 61 63 |er-level| approac|
|00001fd0| 68 20 6d 61 79 20 62 65 | 20 6d 6f 72 65 20 61 70 |h may be| more ap|
|00001fe0| 70 72 6f 70 72 69 61 74 | 65 2e 2e 2e 0d 0a 0d 0a |propriat|e.......|
|00001ff0| 5c 62 65 67 69 6e 7b 66 | 69 67 75 72 65 7d 0d 0a |\begin{f|igure}..|
|00002000| 5c 63 61 70 74 69 6f 6e | 7b 43 6f 6e 74 72 6f 6c |\caption|{Control|
|00002010| 20 46 6c 6f 77 20 6f 66 | 20 74 68 65 20 54 77 6f | Flow of| the Two|
|00002020| 20 41 70 70 72 6f 61 63 | 68 65 73 20 74 6f 20 43 | Approac|hes to C|
|00002030| 6f 6e 74 65 78 74 20 53 | 77 69 74 63 68 69 6e 67 |ontext S|witching|
|00002040| 7d 0d 0a 5c 62 65 67 69 | 6e 7b 76 65 72 62 61 74 |}..\begi|n{verbat|
|00002050| 69 6d 7d 0d 0a 0d 0a 20 | 20 20 20 20 4e 6f 2d 52 |im}.... | No-R|
|00002060| 65 74 75 72 6e 20 2f 20 | 4d 75 6c 74 69 70 6c 65 |eturn / |Multiple|
|00002070| 2d 52 65 74 75 72 6e 20 | 20 20 20 20 20 20 20 20 |-Return | |
|00002080| 20 20 20 20 20 20 20 53 | 77 69 74 63 68 20 46 75 | S|witch Fu|
|00002090| 6e 63 74 69 6f 6e 0d 0a | 0d 0a 20 20 20 20 20 20 |nction..|.. |
|000020a0| 20 20 20 20 20 20 41 20 | 20 20 20 20 20 20 20 20 | A | |
|000020b0| 20 20 20 20 20 20 20 42 | 20 20 20 20 20 20 20 2a | B| *|
|000020c0| 20 20 20 20 20 20 20 20 | 20 20 41 20 20 20 20 20 | | A |
|000020d0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 42 0d 0a | | B..|
|000020e0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|000020f0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00002100| 20 20 20 20 20 2a 0d 0a | 20 20 20 31 20 20 20 20 | *..| 1 |
|00002110| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 78 20 20 | | x |
|00002120| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 2a 20 20 | | * |
|00002130| 20 31 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | 1 | |
|00002140| 20 20 20 20 78 0d 0a 20 | 20 20 7c 20 20 20 20 20 | x.. | | |
|00002150| 20 20 20 20 20 20 20 20 | 20 20 20 20 7c 20 20 20 | | | |
|00002160| 20 20 20 20 20 20 20 20 | 20 20 20 20 2a 20 20 20 | | * |
|00002170| 7c 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 || | |
|00002180| 20 20 20 7c 20 20 20 20 | 0d 0a 20 20 20 7c 20 20 | | |.. | |
|00002190| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 7c | | ||
|000021a0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 2a | | *|
|000021b0| 20 20 20 7c 20 20 20 20 | 20 20 20 20 20 20 20 20 | | | |
|000021c0| 20 20 20 20 20 20 7c 20 | 20 20 20 0d 0a 20 20 20 | | | .. |
|000021d0| 5c 2d 2d 3e 20 6e 72 65 | 74 75 72 6e 28 42 29 20 |\--> nre|turn(B) |
|000021e0| 20 20 5c 2d 2d 3e 20 6d | 72 65 74 75 72 6e 28 29 | \--> m|return()|
|000021f0| 20 20 2a 20 20 20 5c 2d | 2d 3e 20 73 77 69 74 63 | * \-|-> switc|
|00002200| 68 28 42 29 20 20 20 20 | 20 5c 2d 2d 3e 20 73 77 |h(B) | \--> sw|
|00002210| 69 74 63 68 28 2e 2e 2e | 29 20 20 0d 0a 20 20 20 |itch(...|) .. |
|00002220| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00002230| 20 20 2f 2d 2d 2d 20 20 | 20 20 20 20 20 20 20 20 | /--- | |
|00002240| 20 20 2a 20 20 20 2f 2d | 2d 2d 20 20 20 20 20 20 | * /-|-- |
|00002250| 20 20 20 20 20 20 20 20 | 20 2f 2d 2d 2d 20 20 0d | | /--- .|
|00002260| 0a 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |. | |
|00002270| 20 20 20 20 20 20 7c 20 | 2f 2d 20 20 20 20 20 20 | | |/- |
|00002280| 20 20 20 20 20 20 2a 20 | 20 20 7c 20 20 20 20 20 | * | | |
|00002290| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 7c 0d 0a | | |..|
|000022a0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|000022b0| 20 20 20 20 20 7c 20 7c | 20 20 20 20 20 20 20 20 | | || |
|000022c0| 20 20 20 20 20 2a 20 20 | 20 7c 20 20 20 20 20 20 | * | | |
|000022d0| 20 20 20 20 20 20 20 20 | 20 20 20 20 7c 0d 0a 20 | | |.. |
|000022e0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|000022f0| 20 20 20 20 56 20 56 20 | 20 20 20 20 20 20 20 20 | V V | |
|00002300| 20 20 20 20 2a 20 20 20 | 56 20 20 20 20 20 20 20 | * |V |
|00002310| 20 20 20 20 20 20 20 20 | 20 20 20 56 0d 0a 20 20 | | V.. |
|00002320| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00002330| 20 20 20 78 20 31 20 20 | 20 20 20 20 20 20 20 20 | x 1 | |
|00002340| 20 20 20 2a 20 20 20 79 | 20 20 20 20 20 20 20 20 | * y| |
|00002350| 20 20 20 20 20 20 20 20 | 20 20 31 0d 0a 20 20 20 | | 1.. |
|00002360| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00002370| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00002380| 20 20 2a 0d 0a 0d 0a 5c | 65 6e 64 7b 76 65 72 62 | *....\|end{verb|
|00002390| 61 74 69 6d 7d 0d 0a 5c | 65 6e 64 7b 66 69 67 75 |atim}..\|end{figu|
|000023a0| 72 65 7d 0d 0a 0d 0a 5c | 73 75 62 73 65 63 74 69 |re}....\|subsecti|
|000023b0| 6f 6e 7b 54 68 65 20 53 | 77 69 74 63 68 20 46 75 |on{The S|witch Fu|
|000023c0| 6e 63 74 69 6f 6e 20 41 | 70 70 72 6f 61 63 68 7d |nction A|pproach}|
|000023d0| 0d 0a 0d 0a 53 6f 20 74 | 68 65 20 6f 74 68 65 72 |....So t|he other|
|000023e0| 20 61 70 70 72 6f 61 63 | 68 20 69 73 20 74 6f 20 | approac|h is to |
|000023f0| 70 72 6f 76 69 64 65 20 | 61 20 73 77 69 74 63 68 |provide |a switch|
|00002400| 20 66 75 6e 63 74 69 6f | 6e 2e 20 20 57 68 65 6e | functio|n. When|
|00002410| 20 63 61 6c 6c 65 64 2c | 20 74 68 65 0d 0a 73 77 | called,| the..sw|
|00002420| 69 74 63 68 20 66 75 6e | 63 74 69 6f 6e 20 77 6f |itch fun|ction wo|
|00002430| 75 6c 64 20 73 77 69 74 | 63 68 20 69 6e 74 6f 20 |uld swit|ch into |
|00002440| 61 6e 6f 74 68 65 72 20 | 63 6f 6e 74 65 78 74 2e |another |context.|
|00002450| 20 20 4c 61 74 65 72 2c | 20 61 6e 6f 74 68 65 72 | Later,| another|
|00002460| 0d 0a 63 6f 6e 74 65 78 | 74 20 6d 61 79 20 73 77 |..contex|t may sw|
|00002470| 69 74 63 68 20 62 61 63 | 6b 20 74 6f 20 74 68 65 |itch bac|k to the|
|00002480| 20 66 69 72 73 74 2c 20 | 61 6e 64 20 74 68 65 20 | first, |and the |
|00002490| 66 75 6e 63 74 69 6f 6e | 20 77 6f 75 6c 64 20 72 |function| would r|
|000024a0| 65 74 75 72 6e 20 28 6f | 6e 65 0d 0a 63 61 6c 6c |eturn (o|ne..call|
|000024b0| 2c 20 6f 6e 65 20 72 65 | 74 75 72 6e 29 2e 20 20 |, one re|turn). |
|000024c0| 46 6f 72 20 73 79 6d 6d | 65 74 72 79 2c 20 74 68 |For symm|etry, th|
|000024d0| 65 20 66 75 6e 63 74 69 | 6f 6e 20 63 6f 75 6c 64 |e functi|on could|
|000024e0| 20 74 61 6b 65 20 61 73 | 20 61 6e 20 61 72 67 75 | take as| an argu|
|000024f0| 6d 65 6e 74 0d 0a 74 68 | 65 20 63 6f 6e 74 65 78 |ment..th|e contex|
|00002500| 74 20 74 6f 20 62 65 20 | 73 77 69 74 63 68 65 64 |t to be |switched|
|00002510| 20 74 6f 20 61 6e 64 20 | 72 65 74 75 72 6e 20 74 | to and |return t|
|00002520| 68 65 20 63 6f 6e 74 65 | 78 74 20 69 74 20 73 77 |he conte|xt it sw|
|00002530| 69 74 63 68 65 64 20 66 | 72 6f 6d 2e 0d 0a 55 6e |itched f|rom...Un|
|00002540| 6c 69 6b 65 20 6e 6f 2d | 72 65 74 75 72 6e 2f 6d |like no-|return/m|
|00002550| 75 6c 74 69 70 6c 65 2d | 72 65 74 75 72 6e 2c 20 |ultiple-|return, |
|00002560| 74 68 65 20 73 77 69 74 | 63 68 20 61 70 70 72 6f |the swit|ch appro|
|00002570| 61 63 68 20 69 73 20 71 | 75 69 74 65 20 61 6e 61 |ach is q|uite ana|
|00002580| 6c 6f 67 6f 75 73 0d 0a | 74 6f 20 74 68 65 20 66 |logous..|to the f|
|00002590| 61 6d 69 6c 69 61 72 20 | 66 75 6e 63 74 69 6f 6e |amiliar |function|
|000025a0| 20 63 61 6c 6c 2e 20 20 | 49 6e 64 65 65 64 2c 20 | call. |Indeed, |
|000025b0| 69 74 20 69 73 20 71 75 | 69 74 65 20 65 61 73 79 |it is qu|ite easy|
|000025c0| 20 74 6f 20 69 6d 70 6c | 65 6d 65 6e 74 20 74 68 | to impl|ement th|
|000025d0| 65 0d 0a 74 72 61 64 69 | 74 69 6f 6e 61 6c 20 66 |e..tradi|tional f|
|000025e0| 75 6e 63 74 69 6f 6e 3a | 20 6a 75 73 74 20 60 60 |unction:| just ``|
|000025f0| 63 61 6c 6c 27 27 20 74 | 68 65 20 66 75 6e 63 74 |call'' t|he funct|
|00002600| 69 6f 6e 20 62 79 20 73 | 77 69 74 63 68 69 6e 67 |ion by s|witching|
|00002610| 20 74 6f 20 69 74 2c 20 | 61 6e 64 0d 0a 74 68 65 | to it, |and..the|
|00002620| 20 66 75 6e 63 74 69 6f | 6e 2c 20 68 61 76 69 6e | functio|n, havin|
|00002630| 67 20 72 65 63 65 69 76 | 65 64 20 74 68 65 20 70 |g receiv|ed the p|
|00002640| 72 65 76 69 6f 75 73 20 | 63 6f 6e 74 65 78 74 20 |revious |context |
|00002650| 66 72 6f 6d 20 69 74 73 | 20 6f 77 6e 20 73 77 69 |from its| own swi|
|00002660| 74 63 68 0d 0a 69 6e 73 | 74 72 75 63 74 69 6f 6e |tch..ins|truction|
|00002670| 2c 20 77 6f 75 6c 64 20 | 73 77 69 74 63 68 20 62 |, would |switch b|
|00002680| 61 63 6b 20 75 70 6f 6e | 20 60 60 72 65 74 75 72 |ack upon| ``retur|
|00002690| 6e 2e 27 27 0d 0a 0d 0a | 42 75 74 20 64 6f 6e 27 |n.''....|But don'|
|000026a0| 74 20 74 72 79 20 74 68 | 69 73 20 66 6f 72 20 72 |t try th|is for r|
|000026b0| 65 63 75 72 73 69 76 65 | 20 63 61 6c 6c 73 21 20 |ecursive| calls! |
|000026c0| 20 54 68 65 20 61 6e 61 | 6c 6f 67 79 20 62 72 65 | The ana|logy bre|
|000026d0| 61 6b 73 20 64 6f 77 6e | 20 68 65 72 65 2e 0d 0a |aks down| here...|
|000026e0| 54 68 65 20 69 6d 70 6f | 72 74 61 6e 74 20 64 69 |The impo|rtant di|
|000026f0| 66 66 65 72 65 6e 63 65 | 20 69 73 20 74 68 65 20 |fference| is the |
|00002700| 60 60 63 61 6c 6c 65 72 | 27 27 20 61 6e 64 20 74 |``caller|'' and t|
|00002710| 68 65 20 60 60 63 61 6c | 6c 65 65 27 27 20 61 72 |he ``cal|lee'' ar|
|00002720| 65 20 72 65 61 6c 6c 79 | 0d 0a 74 68 65 20 73 61 |e really|..the sa|
|00002730| 6d 65 2e 20 20 42 6f 74 | 68 20 6d 61 69 6e 74 61 |me. Bot|h mainta|
|00002740| 69 6e 20 74 68 65 69 72 | 20 6f 77 6e 20 73 74 61 |in their| own sta|
|00002750| 74 65 20 61 6e 64 20 74 | 68 65 69 72 20 6f 77 6e |te and t|heir own|
|00002760| 20 73 74 61 63 6b 2e 20 | 20 42 6f 74 68 20 63 61 | stack. | Both ca|
|00002770| 6c 6c 65 64 0d 0a 74 68 | 65 20 73 77 69 74 63 68 |lled..th|e switch|
|00002780| 20 66 75 6e 63 74 69 6f | 6e 2e 20 20 54 68 65 20 | functio|n. The |
|00002790| 6f 6e 6c 79 20 64 69 66 | 66 65 72 65 6e 63 65 20 |only dif|ference |
|000027a0| 69 73 20 74 68 65 20 60 | 60 63 61 6c 6c 65 72 27 |is the `|`caller'|
|000027b0| 27 20 68 61 73 6e 27 74 | 0d 0a 72 65 74 75 72 6e |' hasn't|..return|
|000027c0| 65 64 20 79 65 74 2c 20 | 77 68 69 6c 65 20 74 68 |ed yet, |while th|
|000027d0| 65 20 60 60 63 61 6c 6c | 65 65 27 27 20 68 61 73 |e ``call|ee'' has|
|000027e0| 20 6a 75 73 74 20 72 65 | 74 75 72 6e 65 64 2e 20 | just re|turned. |
|000027f0| 20 42 75 74 20 75 6e 6c | 69 6b 65 20 69 6e 20 61 | But unl|ike in a|
|00002800| 0d 0a 66 75 6e 63 74 69 | 6f 6e 2c 20 74 68 69 73 |..functi|on, this|
|00002810| 20 60 60 63 61 6c 6c 65 | 65 27 27 20 64 6f 65 73 | ``calle|e'' does|
|00002820| 20 6e 6f 74 20 68 61 76 | 65 20 74 6f 20 72 65 74 | not hav|e to ret|
|00002830| 75 72 6e 20 74 6f 20 69 | 74 73 20 60 60 63 61 6c |urn to i|ts ``cal|
|00002840| 6c 65 72 27 27 2e 0d 0a | 49 6e 64 65 65 64 2c 20 |ler''...|Indeed, |
|00002850| 69 74 20 63 61 6e 20 73 | 77 69 74 63 68 20 74 6f |it can s|witch to|
|00002860| 20 61 6e 79 20 6f 74 68 | 65 72 20 63 6f 6e 74 65 | any oth|er conte|
|00002870| 78 74 20 69 74 20 6c 69 | 6b 65 2c 20 77 68 69 63 |xt it li|ke, whic|
|00002880| 68 2c 20 69 6e 20 74 75 | 72 6e 2c 20 6d 79 0d 0a |h, in tu|rn, my..|
|00002890| 73 77 69 74 63 68 20 62 | 61 63 6b 20 74 6f 20 74 |switch b|ack to t|
|000028a0| 68 65 20 60 60 63 61 6c | 6c 65 72 27 27 20 2d 2d |he ``cal|ler'' --|
|000028b0| 20 6f 72 20 74 68 65 20 | 60 60 63 61 6c 6c 65 72 | or the |``caller|
|000028c0| 27 27 20 27 73 20 73 77 | 69 74 63 68 20 6d 61 79 |'' 's sw|itch may|
|000028d0| 20 6e 65 76 65 72 0d 0a | 72 65 74 75 72 6e 2e 0d | never..|return..|
|000028e0| 0a 0d 0a 41 6e 6f 74 68 | 65 72 20 69 6d 70 6f 72 |...Anoth|er impor|
|000028f0| 74 61 6e 74 20 70 6f 69 | 6e 74 20 69 73 20 74 68 |tant poi|nt is th|
|00002900| 61 74 20 77 65 20 63 61 | 6e 20 74 72 65 61 74 20 |at we ca|n treat |
|00002910| 61 20 63 6f 6e 74 65 78 | 74 20 2d 2d 74 68 65 20 |a contex|t --the |
|00002920| 73 74 61 74 65 20 6f 66 | 0d 0a 65 78 65 63 75 74 |state of|..execut|
|00002930| 69 6f 6e 20 6f 66 20 73 | 6f 6d 65 20 74 68 72 65 |ion of s|ome thre|
|00002940| 61 64 2c 20 69 6e 63 6c | 75 64 69 6e 67 20 69 74 |ad, incl|uding it|
|00002950| 73 20 6c 6f 63 61 6c 20 | 76 61 72 69 61 62 6c 65 |s local |variable|
|00002960| 73 20 28 73 74 61 63 6b | 29 2d 2d 20 61 73 20 61 |s (stack|)-- as a|
|00002970| 6e 0d 0a 6f 62 6a 65 63 | 74 2e 20 20 4f 6e 65 20 |n..objec|t. One |
|00002980| 74 68 72 65 61 64 20 63 | 6f 75 6c 64 20 70 6f 74 |thread c|ould pot|
|00002990| 65 6e 74 69 61 6c 6c 79 | 20 66 72 65 65 7a 65 20 |entially| freeze |
|000029a0| 61 6e 6f 74 68 65 72 20 | 74 68 72 65 61 64 2c 20 |another |thread, |
|000029b0| 65 78 61 6d 69 6e 65 20 | 69 74 73 0d 0a 63 6f 6e |examine |its..con|
|000029c0| 74 65 78 74 2c 20 70 65 | 72 68 61 70 73 20 6d 6f |text, pe|rhaps mo|
|000029d0| 64 69 66 79 20 73 6f 6d | 65 20 6f 66 20 69 74 73 |dify som|e of its|
|000029e0| 20 76 61 72 69 61 62 6c | 65 73 2c 20 61 6e 64 20 | variabl|es, and |
|000029f0| 74 68 65 6e 20 73 74 61 | 72 74 20 69 74 20 72 75 |then sta|rt it ru|
|00002a00| 6e 6e 69 6e 67 0d 0a 61 | 67 61 69 6e 2e 20 20 54 |nning..a|gain. T|
|00002a10| 6f 20 62 65 20 61 62 6c | 65 20 74 6f 20 74 72 65 |o be abl|e to tre|
|00002a20| 61 74 20 74 68 65 20 63 | 6f 6e 74 72 6f 6c 20 66 |at the c|ontrol f|
|00002a30| 6c 6f 77 20 6f 66 20 61 | 20 70 72 6f 67 72 61 6d |low of a| program|
|00002a40| 20 6c 69 6b 65 20 69 74 | 73 20 64 61 74 61 20 69 | like it|s data i|
|00002a50| 73 0d 0a 61 20 76 65 72 | 79 20 70 6f 77 65 72 66 |s..a ver|y powerf|
|00002a60| 75 6c 20 63 61 70 61 62 | 69 6c 69 74 79 2e 0d 0a |ul capab|ility...|
|00002a70| 0d 0a 49 6e 20 73 68 6f | 72 74 2c 20 74 68 65 20 |..In sho|rt, the |
|00002a80| 73 77 69 74 63 68 20 66 | 75 6e 63 74 69 6f 6e 20 |switch f|unction |
|00002a90| 61 70 70 72 6f 61 63 68 | 2c 20 61 73 20 70 61 72 |approach|, as par|
|00002aa0| 74 69 63 75 6c 61 72 20 | 7b 5c 65 6d 20 61 70 70 |ticular |{\em app|
|00002ab0| 6c 69 63 61 74 69 6f 6e | 20 6f 66 7d 0d 0a 74 68 |lication| of}..th|
|00002ac0| 65 20 6d 75 6c 74 69 70 | 6c 65 2d 72 65 74 75 72 |e multip|le-retur|
|00002ad0| 6e 2f 6e 6f 2d 72 65 74 | 75 72 6e 20 61 70 70 72 |n/no-ret|urn appr|
|00002ae0| 6f 61 63 68 2c 20 69 73 | 20 6e 6f 74 20 61 73 20 |oach, is| not as |
|00002af0| 66 6c 65 78 69 62 6c 65 | 2c 20 62 75 74 20 70 72 |flexible|, but pr|
|00002b00| 6f 76 69 64 65 73 0d 0a | 74 68 65 20 66 75 6e 63 |ovides..|the func|
|00002b10| 74 69 6f 6e 61 6c 69 74 | 79 20 77 65 20 6e 65 65 |tionalit|y we nee|
|00002b20| 64 20 66 6f 72 20 6d 75 | 6c 74 69 74 61 73 6b 69 |d for mu|ltitaski|
|00002b30| 6e 67 20 69 6e 20 61 6e | 20 61 62 73 74 72 61 63 |ng in an| abstrac|
|00002b40| 74 69 6f 6e 20 77 65 20 | 63 61 6e 20 64 65 61 6c |tion we |can deal|
|00002b50| 0d 0a 77 69 74 68 2e 0d | 0a 0d 0a 5c 73 65 63 74 |..with..|...\sect|
|00002b60| 69 6f 6e 7b 42 75 69 6c | 64 69 6e 67 20 61 20 43 |ion{Buil|ding a C|
|00002b70| 6f 6e 74 65 78 74 20 53 | 77 69 74 63 68 20 69 6e |ontext S|witch in|
|00002b80| 20 60 60 43 27 27 7d 0d | 0a 0d 0a 49 6e 20 69 6d | ``C''}.|...In im|
|00002b90| 70 6c 65 6d 65 6e 74 69 | 6e 67 20 74 68 65 20 63 |plementi|ng the c|
|00002ba0| 6f 6e 74 65 78 74 20 73 | 77 69 74 63 68 20 28 73 |ontext s|witch (s|
|00002bb0| 65 65 20 63 6f 64 65 20 | 69 6e 20 6d 70 2a 2e 68 |ee code |in mp*.h|
|00002bc0| 20 61 6e 64 20 6d 70 2a | 2e 63 20 66 69 6c 65 73 | and mp*|.c files|
|00002bd0| 29 2c 20 77 65 0d 0a 75 | 73 65 20 74 68 65 20 70 |), we..u|se the p|
|00002be0| 6f 77 65 72 66 75 6c 20 | 70 72 6f 67 72 61 6d 6d |owerful |programm|
|00002bf0| 69 6e 67 20 6d 65 74 68 | 6f 64 6f 6c 6f 67 79 20 |ing meth|odology |
|00002c00| 6f 66 20 60 60 63 6c 75 | 73 74 65 72 69 6e 67 27 |of ``clu|stering'|
|00002c10| 27 20 73 69 6d 69 6c 61 | 72 0d 0a 63 6f 64 65 20 |' simila|r..code |
|00002c20| 61 6e 64 20 64 61 74 61 | 20 69 6e 74 6f 20 6f 6e |and data| into on|
|00002c30| 65 20 6d 6f 64 75 6c 61 | 72 20 75 6e 69 74 20 77 |e modula|r unit w|
|00002c40| 69 74 68 20 64 65 66 69 | 6e 65 64 0d 0a 69 6e 74 |ith defi|ned..int|
|00002c50| 65 72 66 61 63 65 73 2e | 5c 66 6f 6f 74 6e 6f 74 |erfaces.|\footnot|
|00002c60| 65 7b 42 61 73 65 64 20 | 6f 6e 20 4d 49 54 20 50 |e{Based |on MIT P|
|00002c70| 72 6f 66 65 73 73 6f 72 | 20 42 61 72 62 72 61 20 |rofessor| Barbra |
|00002c80| 4c 69 73 6b 6f 76 27 73 | 20 43 4c 55 0d 0a 70 72 |Liskov's| CLU..pr|
|00002c90| 6f 67 72 61 6d 6d 69 6e | 67 20 6c 61 6e 67 75 61 |ogrammin|g langua|
|00002ca0| 67 65 2e 20 20 53 65 65 | 20 42 2e 20 4c 69 73 6b |ge. See| B. Lisk|
|00002cb0| 6f 76 20 65 74 20 61 6c | 2c 20 5c 75 6e 64 65 72 |ov et al|, \under|
|00002cc0| 6c 69 6e 65 7b 43 4c 55 | 20 52 65 66 65 72 65 6e |line{CLU| Referen|
|00002cd0| 63 65 0d 0a 4d 61 6e 75 | 61 6c 7d 2c 20 53 70 72 |ce..Manu|al}, Spr|
|00002ce0| 69 6e 67 65 72 2d 56 65 | 72 6c 61 67 2c 20 4e 65 |inger-Ve|rlag, Ne|
|00002cf0| 77 20 59 6f 72 6b 2c 20 | 31 39 38 31 2e 7d 20 49 |w York, |1981.} I|
|00002d00| 6e 20 74 68 69 73 20 63 | 61 73 65 2c 20 77 65 20 |n this c|ase, we |
|00002d10| 63 72 65 61 74 65 20 61 | 0d 0a 74 68 72 65 61 64 |create a|..thread|
|00002d20| 20 63 6c 75 73 74 65 72 | 20 66 6f 72 20 69 6e 69 | cluster| for ini|
|00002d30| 74 69 61 6c 69 7a 69 6e | 67 2c 20 6d 61 6e 69 70 |tializin|g, manip|
|00002d40| 75 6c 61 74 69 6e 67 2c | 20 61 6e 64 20 64 65 69 |ulating,| and dei|
|00002d50| 6e 69 74 69 61 6c 69 7a | 69 6e 67 20 74 68 72 65 |nitializ|ing thre|
|00002d60| 61 64 73 0d 0a 6f 66 20 | 65 78 65 63 75 74 69 6f |ads..of |executio|
|00002d70| 6e 2e 20 20 54 68 65 20 | 7b 5c 62 66 20 69 6e 74 |n. The |{\bf int|
|00002d80| 65 72 66 61 63 65 7d 20 | 66 6f 72 20 74 68 65 20 |erface} |for the |
|00002d90| 74 68 72 65 61 64 20 63 | 6c 75 73 74 65 72 20 69 |thread c|luster i|
|00002da0| 73 20 69 6e 20 74 68 65 | 20 66 69 6c 65 0d 0a 7b |s in the| file..{|
|00002db0| 5c 65 6d 20 6d 70 74 68 | 64 2e 68 7d 3b 20 79 6f |\em mpth|d.h}; yo|
|00002dc0| 75 20 73 68 6f 75 6c 64 | 20 69 6e 63 6c 75 64 65 |u should| include|
|00002dd0| 20 7b 5c 65 6d 20 6d 70 | 74 68 64 2e 68 7d 20 69 | {\em mp|thd.h} i|
|00002de0| 6e 20 79 6f 75 72 20 70 | 72 6f 67 72 61 6d 73 20 |n your p|rograms |
|00002df0| 77 68 65 6e 20 79 6f 75 | 0d 0a 77 69 73 68 20 74 |when you|..wish t|
|00002e00| 6f 20 68 61 76 65 20 6d | 75 6c 74 69 70 6c 65 20 |o have m|ultiple |
|00002e10| 74 68 72 65 61 64 73 20 | 6f 66 20 65 78 65 63 75 |threads |of execu|
|00002e20| 74 69 6f 6e 2e 20 20 54 | 68 65 20 7b 5c 62 66 20 |tion. T|he {\bf |
|00002e30| 69 6d 70 6c 65 6d 65 6e | 74 61 74 69 6f 6e 20 61 |implemen|tation a|
|00002e40| 6e 64 20 72 65 70 72 65 | 73 65 6e 74 61 74 69 6f |nd repre|sentatio|
|00002e50| 6e 7d 20 66 6f 72 20 74 | 68 65 20 74 68 72 65 61 |n} for t|he threa|
|00002e60| 64 73 20 69 73 20 69 6e | 20 74 68 65 20 66 69 6c |ds is in| the fil|
|00002e70| 65 0d 0a 7b 5c 65 6d 20 | 6d 70 74 68 64 2e 63 7d |e..{\em |mpthd.c}|
|00002e80| 3b 20 79 6f 75 72 20 70 | 72 6f 67 72 61 6d 73 20 |; your p|rograms |
|00002e90| 73 68 6f 75 6c 64 20 6e | 6f 74 20 6b 6e 6f 77 20 |should n|ot know |
|00002ea0| 61 62 6f 75 74 20 74 68 | 69 73 20 66 69 6c 65 2c |about th|is file,|
|00002eb0| 20 61 6e 64 20 6e 6f 74 | 20 6d 61 6b 65 0d 0a 61 | and not| make..a|
|00002ec0| 6e 79 20 61 73 73 75 6d | 70 74 69 6f 6e 73 20 61 |ny assum|ptions a|
|00002ed0| 62 6f 75 74 20 68 6f 77 | 20 74 68 72 65 61 64 73 |bout how| threads|
|00002ee0| 20 61 72 65 20 77 6f 72 | 6b 20 62 65 79 6f 6e 64 | are wor|k beyond|
|00002ef0| 20 74 68 65 69 72 20 69 | 6e 74 65 72 66 61 63 65 | their i|nterface|
|00002f00| 2e 20 20 54 68 65 73 65 | 0d 0a 67 6f 6f 64 20 70 |. These|..good p|
|00002f10| 72 6f 67 72 61 6d 6d 69 | 6e 67 20 64 69 73 63 69 |rogrammi|ng disci|
|00002f20| 70 6c 69 6e 65 73 20 68 | 65 6c 70 20 62 75 69 6c |plines h|elp buil|
|00002f30| 64 20 63 6c 65 61 6e 2c | 20 77 65 6c 6c 2d 64 65 |d clean,| well-de|
|00002f40| 66 69 6e 65 64 20 69 6e | 74 65 72 66 61 63 65 73 |fined in|terfaces|
|00002f50| 0d 0a 62 65 74 77 65 65 | 6e 20 70 72 6f 67 72 61 |..betwee|n progra|
|00002f60| 6d 20 6d 6f 64 75 6c 65 | 73 20 2d 2d 20 61 20 62 |m module|s -- a b|
|00002f70| 69 67 20 68 65 6c 70 20 | 77 68 65 6e 20 79 6f 75 |ig help |when you|
|00002f80| 72 20 74 65 73 74 69 6e | 67 2c 20 64 65 62 75 67 |r testin|g, debug|
|00002f90| 67 69 6e 67 2c 20 6f 72 | 0d 0a 6d 6f 64 69 66 79 |ging, or|..modify|
|00002fa0| 69 6e 67 20 79 6f 75 72 | 20 63 6f 64 65 2e 0d 0a |ing your| code...|
|00002fb0| 0d 0a 4d 6f 72 65 20 67 | 65 6e 65 72 61 6c 6c 79 |..More g|enerally|
|00002fc0| 2c 20 6d 6f 73 74 20 7b | 5c 65 6d 20 2a 2e 68 7d |, most {|\em *.h}|
|00002fd0| 20 66 69 6c 65 73 20 61 | 72 65 20 63 6c 75 73 74 | files a|re clust|
|00002fe0| 65 72 20 69 6e 74 65 72 | 66 61 63 65 73 20 28 65 |er inter|faces (e|
|00002ff0| 78 63 65 70 74 0d 0a 74 | 68 65 20 73 65 63 74 69 |xcept..t|he secti|
|00003000| 6f 6e 73 20 6d 61 72 6b | 65 64 20 7b 5c 65 6d 20 |ons mark|ed {\em |
|00003010| 49 4e 54 45 52 4e 41 4c | 20 55 53 45 20 4f 4e 4c |INTERNAL| USE ONL|
|00003020| 59 7d 29 2c 20 61 6e 64 | 20 74 68 65 69 72 20 72 |Y}), and| their r|
|00003030| 65 73 70 65 63 74 69 76 | 65 0d 0a 7b 5c 65 6d 20 |espectiv|e..{\em |
|00003040| 2a 2e 63 7d 20 66 69 6c | 65 20 28 69 66 20 69 74 |*.c} fil|e (if it|
|00003050| 20 65 78 69 73 74 73 29 | 20 74 68 65 20 63 6c 75 | exists)| the clu|
|00003060| 73 74 65 72 20 69 6d 70 | 6c 65 6d 65 6e 74 61 74 |ster imp|lementat|
|00003070| 69 6f 6e 2e 20 20 57 69 | 74 68 69 6e 20 74 68 65 |ion. Wi|thin the|
|00003080| 0d 0a 63 6c 75 73 74 65 | 72 73 2c 20 61 6c 6c 20 |..cluste|rs, all |
|00003090| 64 61 74 61 74 79 70 65 | 73 20 61 72 65 20 69 6e |datatype|s are in|
|000030a0| 20 75 70 70 65 72 20 63 | 61 73 65 20 28 65 67 2c | upper c|ase (eg,|
|000030b0| 20 24 4d 50 54 48 44 24 | 29 20 61 6e 64 2c 20 69 | $MPTHD$|) and, i|
|000030c0| 66 0d 0a 61 73 73 6f 63 | 69 61 74 65 64 20 77 69 |f..assoc|iated wi|
|000030d0| 74 68 20 61 20 63 6c 75 | 73 74 65 72 2c 20 61 72 |th a clu|ster, ar|
|000030e0| 65 20 72 65 66 65 72 65 | 6e 63 65 64 20 65 78 74 |e refere|nced ext|
|000030f0| 65 72 6e 61 6c 6c 79 20 | 62 79 20 6e 61 6d 65 20 |ernally |by name |
|00003100| 28 70 6f 69 6e 74 65 72 | 29 0d 0a 61 6e 64 20 69 |(pointer|)..and i|
|00003110| 6e 74 65 72 6e 61 6c 6c | 79 20 62 79 20 76 61 6c |nternall|y by val|
|00003120| 75 65 20 28 63 6f 6e 74 | 65 6e 74 73 29 3b 20 6f |ue (cont|ents); o|
|00003130| 6e 20 74 68 65 20 6f 74 | 68 65 72 20 68 61 6e 64 |n the ot|her hand|
|00003140| 2c 20 61 6c 6c 20 70 72 | 6f 63 65 64 75 72 65 73 |, all pr|ocedures|
|00003150| 0d 0a 61 72 65 20 69 6e | 20 6c 6f 77 65 72 20 63 |..are in| lower c|
|00003160| 61 73 65 2c 20 61 6e 64 | 20 70 72 65 66 69 78 65 |ase, and| prefixe|
|00003170| 64 20 77 69 74 68 20 74 | 68 65 20 6d 61 6a 6f 72 |d with t|he major|
|00003180| 20 64 61 74 61 74 79 70 | 65 20 74 68 65 79 20 6f | datatyp|e they o|
|00003190| 70 65 72 61 74 65 0d 0a | 6f 6e 20 28 65 67 2c 20 |perate..|on (eg, |
|000031a0| 24 6d 70 74 68 64 5c 5f | 73 77 69 74 63 68 24 29 |$mpthd\_|switch$)|
|000031b0| 2e 20 20 54 6f 20 68 65 | 6c 70 20 70 72 65 76 65 |. To he|lp preve|
|000031c0| 6e 74 20 6e 61 6d 69 6e | 67 20 63 6f 6e 66 6c 69 |nt namin|g confli|
|000031d0| 63 74 73 2c 20 61 6c 6c | 0d 0a 67 6c 6f 62 61 6c |cts, all|..global|
|000031e0| 20 6e 61 6d 65 73 20 61 | 72 65 20 70 72 65 66 69 | names a|re prefi|
|000031f0| 78 65 64 20 77 69 74 68 | 20 74 68 65 20 69 6e 69 |xed with| the ini|
|00003200| 74 69 61 6c 73 20 6f 66 | 20 74 68 65 69 72 20 63 |tials of| their c|
|00003210| 72 65 61 74 6f 72 2e 20 | 20 49 6e 20 74 68 69 73 |reator. | In this|
|00003220| 0d 0a 63 61 73 65 2c 20 | 65 76 65 72 79 20 6e 61 |..case, |every na|
|00003230| 6d 65 20 62 65 67 69 6e | 73 20 77 69 74 68 20 7b |me begin|s with {|
|00003240| 5c 65 6d 20 6d 70 7d 20 | 66 6f 72 20 4d 69 6b 65 |\em mp} |for Mike|
|00003250| 20 50 61 72 6b 65 72 2e | 20 20 54 68 65 72 65 20 | Parker.| There |
|00003260| 61 72 65 0d 0a 71 75 69 | 74 65 20 61 20 6e 75 6d |are..qui|te a num|
|00003270| 62 65 72 20 6f 66 20 6f | 74 68 65 72 20 70 72 6f |ber of o|ther pro|
|00003280| 67 72 61 6d 6d 69 6e 67 | 20 64 69 73 63 69 70 6c |gramming| discipl|
|00003290| 69 6e 65 73 20 61 64 68 | 65 72 65 64 20 74 6f 20 |ines adh|ered to |
|000032a0| 77 69 74 68 69 6e 20 74 | 68 65 0d 0a 63 6f 64 65 |within t|he..code|
|000032b0| 20 6e 6f 74 20 72 65 6c | 65 76 61 6e 74 20 74 6f | not rel|evant to|
|000032c0| 20 63 6f 6e 74 65 78 74 | 20 73 77 69 74 63 68 69 | context| switchi|
|000032d0| 6e 67 20 62 75 74 20 77 | 6f 72 74 68 20 6c 6f 6f |ng but w|orth loo|
|000032e0| 6b 69 6e 67 20 61 74 2e | 0d 0a 0d 0a 54 68 6f 75 |king at.|....Thou|
|000032f0| 67 68 20 79 6f 75 72 20 | 70 72 6f 67 72 61 6d 73 |gh your |programs|
|00003300| 20 6d 61 79 20 6e 6f 74 | 20 6b 6e 6f 77 20 68 6f | may not| know ho|
|00003310| 77 20 74 68 72 65 61 64 | 73 20 61 72 65 20 69 6d |w thread|s are im|
|00003320| 70 6c 65 6d 65 6e 74 65 | 64 2c 20 49 20 65 78 70 |plemente|d, I exp|
|00003330| 65 63 74 20 61 0d 0a 66 | 65 77 20 68 75 6d 61 6e |ect a..f|ew human|
|00003340| 73 20 61 72 65 20 63 75 | 72 69 6f 75 73 2e 20 20 |s are cu|rious. |
|00003350| 53 6f 20 69 66 20 79 6f | 75 20 68 61 76 65 6e 27 |So if yo|u haven'|
|00003360| 74 20 61 6c 72 65 61 64 | 79 2c 20 74 61 6b 65 20 |t alread|y, take |
|00003370| 61 20 6d 6f 6d 65 6e 74 | 20 6e 6f 77 20 61 6e 64 |a moment| now and|
|00003380| 0d 0a 72 65 61 64 20 74 | 68 65 20 6f 76 65 72 76 |..read t|he overv|
|00003390| 69 65 77 20 61 6e 64 20 | 66 75 6e 63 74 69 6f 6e |iew and |function|
|000033a0| 61 6c 20 73 70 65 63 69 | 66 69 63 61 74 69 6f 6e |al speci|fication|
|000033b0| 20 6f 66 20 74 68 65 20 | 74 68 72 65 61 64 20 63 | of the |thread c|
|000033c0| 6c 75 73 74 65 72 20 69 | 6e 0d 0a 7b 5c 65 6d 20 |luster i|n..{\em |
|000033d0| 6d 70 74 68 64 2e 68 7d | 2c 20 61 6e 64 20 62 72 |mpthd.h}|, and br|
|000033e0| 69 65 66 6c 79 20 73 63 | 61 6e 20 69 74 73 20 69 |iefly sc|an its i|
|000033f0| 6d 70 6c 65 6d 65 6e 74 | 61 74 69 6f 6e 20 69 6e |mplement|ation in|
|00003400| 20 7b 5c 65 6d 20 6d 70 | 74 68 64 2e 63 7d 2e 20 | {\em mp|thd.c}. |
|00003410| 20 41 6c 73 6f 0d 0a 67 | 6c 61 6e 63 65 20 61 74 | Also..g|lance at|
|00003420| 20 74 68 65 20 74 77 6f | 20 6d 61 6a 6f 72 20 63 | the two| major c|
|00003430| 6c 75 73 74 65 72 73 20 | 69 74 20 64 65 70 65 6e |lusters |it depen|
|00003440| 64 73 20 6f 6e 2c 20 7b | 5c 65 6d 20 6d 70 73 69 |ds on, {|\em mpsi|
|00003450| 67 2e 68 7d 20 61 6e 64 | 20 7b 5c 65 6d 0d 0a 6d |g.h} and| {\em..m|
|00003460| 70 73 65 6d 2e 68 7d 2e | 20 20 48 65 72 65 27 73 |psem.h}.| Here's|
|00003470| 20 61 20 6d 61 70 20 74 | 6f 20 68 65 6c 70 20 67 | a map t|o help g|
|00003480| 75 69 64 65 20 79 6f 75 | 72 20 77 61 79 20 28 73 |uide you|r way (s|
|00003490| 65 65 20 66 69 67 75 72 | 65 29 2e 0d 0a 0d 0a 5c |ee figur|e).....\|
|000034a0| 62 65 67 69 6e 7b 66 69 | 67 75 72 65 7d 0d 0a 5c |begin{fi|gure}..\|
|000034b0| 63 61 70 74 69 6f 6e 7b | 4d 6f 64 75 6c 61 72 20 |caption{|Modular |
|000034c0| 44 65 70 65 6e 64 65 6e | 63 79 20 44 69 61 67 72 |Dependen|cy Diagr|
|000034d0| 61 6d 20 66 72 6f 6d 20 | 6d 70 74 68 64 20 54 68 |am from |mpthd Th|
|000034e0| 72 65 61 64 20 43 6c 75 | 73 74 65 72 7d 0d 0a 5c |read Clu|ster}..\|
|000034f0| 62 65 67 69 6e 7b 76 65 | 72 62 61 74 69 6d 7d 0d |begin{ve|rbatim}.|
|00003500| 0a 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |. | |
|00003510| 20 20 20 20 20 20 20 20 | 20 2f 2d 2d 2d 2d 2d 2d | | /------|
|00003520| 2d 2d 2d 2d 2d 2d 2d 2d | 2d 2d 2d 2d 2d 2d 2d 2d |--------|--------|
|00003530| 2d 2d 5c 0d 0a 20 20 20 | 20 20 20 20 20 20 20 20 |--\.. | |
|00003540| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 7c 20 20 | | | |
|00003550| 20 20 20 20 20 20 20 6d | 70 74 68 64 2e 2a 20 20 | m|pthd.* |
|00003560| 20 20 20 20 20 20 7c 0d | 0a 20 20 20 20 20 20 20 | |.|. |
|00003570| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00003580| 20 7c 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | | |
|00003590| 20 20 20 20 20 20 20 20 | 20 20 7c 0d 0a 20 20 20 | | |.. |
|000035a0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|000035b0| 20 20 20 20 20 7c 20 3c | 54 48 72 65 61 44 73 20 | | <|THreaDs |
|000035c0| 26 20 50 52 6f 43 65 73 | 73 6f 72 73 3e 20 7c 0d |& PRoCes|sors> |.|
|000035d0| 0a 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |. | |
|000035e0| 20 20 20 20 20 20 20 20 | 20 7c 20 20 20 20 20 20 | | | |
|000035f0| 20 20 20 20 20 66 6f 72 | 20 20 20 20 20 20 20 20 | for| |
|00003600| 20 20 7c 0d 0a 20 20 20 | 20 20 20 20 20 20 20 20 | |.. | |
|00003610| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 7c 20 20 | | | |
|00003620| 20 20 63 6f 6e 74 65 78 | 74 2d 63 6f 6e 74 65 78 | contex|t-contex|
|00003630| 74 20 20 20 20 20 7c 0d | 0a 20 20 20 20 20 20 20 |t |.|. |
|00003640| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00003650| 20 7c 20 20 20 20 20 20 | 20 73 77 69 74 63 68 69 | | | switchi|
|00003660| 6e 67 20 20 20 20 20 20 | 20 20 7c 0d 0a 20 20 20 |ng | |.. |
|00003670| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00003680| 20 20 20 20 20 5c 2d 2d | 2d 2d 2d 2d 2d 2d 2d 2d | \--|--------|
|00003690| 2d 2d 2d 2d 2d 2d 2d 2d | 2d 2d 2d 2d 2d 2d 2f 0d |--------|------/.|
|000036a0| 0a 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |. | |
|000036b0| 20 20 20 20 20 20 20 20 | 2f 20 20 20 20 20 20 20 | |/ |
|000036c0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|000036d0| 20 20 20 5c 0d 0a 20 20 | 20 20 20 20 20 20 20 20 | \.. | |
|000036e0| 20 20 20 20 20 20 20 20 | 20 20 20 20 2f 20 20 20 | | / |
|000036f0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00003700| 20 20 20 20 20 20 20 20 | 20 5c 0d 0a 20 20 20 20 | | \.. |
|00003710| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00003720| 20 2f 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | / | |
|00003730| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00003740| 5c 0d 0a 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |\.. | |
|00003750| 20 20 20 20 20 20 20 5c | 2f 20 20 20 20 20 20 20 | \|/ |
|00003760| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00003770| 20 20 20 20 20 20 20 5c | 2f 0d 0a 2f 2d 2d 2d 2d | \|/../----|
|00003780| 2d 2d 2d 2d 2d 2d 2d 2d | 2d 2d 2d 2d 2d 2d 2d 2d |--------|--------|
|00003790| 2d 2d 2d 2d 5c 20 20 20 | 20 20 20 20 20 20 20 20 |----\ | |
|000037a0| 20 20 20 20 20 20 20 20 | 20 20 20 2f 2d 2d 2d 2d | | /----|
|000037b0| 2d 2d 2d 2d 2d 2d 2d 2d | 2d 2d 2d 2d 2d 2d 2d 2d |--------|--------|
|000037c0| 2d 2d 2d 2d 5c 0d 0a 7c | 20 20 20 20 20 20 20 20 |----\..|| |
|000037d0| 6d 70 73 69 67 2e 2a 20 | 20 20 20 20 20 20 20 20 |mpsig.* | |
|000037e0| 7c 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 || | |
|000037f0| 20 20 20 20 20 20 20 7c | 20 20 20 20 20 20 20 20 | || |
|00003800| 6d 70 73 65 6d 2e 2a 20 | 20 20 20 20 20 20 20 20 |mpsem.* | |
|00003810| 7c 0d 0a 7c 20 20 20 20 | 20 20 20 20 20 20 20 20 ||..| | |
|00003820| 20 20 20 20 20 20 20 20 | 20 20 20 20 7c 20 20 20 | | | |
|00003830| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00003840| 20 20 20 7c 20 20 20 20 | 20 20 20 20 20 20 20 20 | | | |
|00003850| 20 20 20 20 20 20 20 20 | 20 20 20 20 7c 0d 0a 7c | | |..||
|00003860| 20 20 20 20 20 20 20 3c | 53 49 47 6e 61 6c 73 3e | <|SIGnals>|
|00003870| 20 20 20 20 20 20 20 20 | 7c 2f 5f 5f 5f 5f 5f 5f | ||/______|
|00003880| 5f 5f 5f 5f 5f 5f 5f 5f | 5f 5f 5f 5f 5f 5f 5f 7c |________|_______||
|00003890| 20 20 20 20 20 3c 53 45 | 4d 61 70 68 6f 72 65 73 | <SE|Maphores|
|000038a0| 3e 20 20 20 20 20 20 20 | 7c 0d 0a 7c 20 20 20 20 |> ||..| |
|000038b0| 20 20 20 20 20 20 66 6f | 72 20 20 20 20 20 20 20 | fo|r |
|000038c0| 20 20 20 20 7c 5c 20 20 | 20 20 20 20 20 20 20 20 | |\ | |
|000038d0| 20 20 20 20 20 20 20 20 | 20 20 20 7c 20 20 20 20 | | | |
|000038e0| 20 20 20 20 20 20 66 6f | 72 20 20 20 20 20 20 20 | fo|r |
|000038f0| 20 20 20 20 7c 0d 0a 7c | 20 20 20 68 61 72 64 77 | |..|| hardw|
|00003900| 61 72 65 2d 70 72 6f 63 | 65 73 73 6f 72 20 20 20 |are-proc|essor |
|00003910| 7c 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 || | |
|00003920| 20 20 20 20 20 20 20 7c | 20 20 70 72 6f 63 65 73 | || proces|
|00003930| 73 6f 72 2d 70 72 6f 63 | 65 73 73 6f 72 20 20 20 |sor-proc|essor |
|00003940| 7c 0d 0a 7c 20 20 20 20 | 73 79 6e 63 68 72 6f 6e ||..| |synchron|
|00003950| 69 7a 61 74 69 6f 6e 20 | 20 20 20 20 7c 20 20 20 |ization | | |
|00003960| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00003970| 20 20 20 7c 20 20 20 20 | 73 79 6e 63 68 72 6f 6e | | |synchron|
|00003980| 69 7a 61 74 69 6f 6e 20 | 20 20 20 20 7c 0d 0a 5c |ization | |..\|
|00003990| 2d 2d 2d 2d 2d 2d 2d 2d | 2d 2d 2d 2d 2d 2d 2d 2d |--------|--------|
|000039a0| 2d 2d 2d 2d 2d 2d 2d 2d | 2f 20 20 20 20 20 20 20 |--------|/ |
|000039b0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 5c | | \|
|000039c0| 2d 2d 2d 2d 2d 2d 2d 2d | 2d 2d 2d 2d 2d 2d 2d 2d |--------|--------|
|000039d0| 2d 2d 2d 2d 2d 2d 2d 2d | 2f 0d 0a 5c 65 6e 64 7b |--------|/..\end{|
|000039e0| 76 65 72 62 61 74 69 6d | 7d 0d 0a 5c 65 6e 64 7b |verbatim|}..\end{|
|000039f0| 66 69 67 75 72 65 7d 0d | 0a 0d 0a 0d 0a 7b 5c 63 |figure}.|.....{\c|
|00003a00| 65 6e 74 65 72 20 2a 20 | 2a 20 2a 7d 0d 0a 0d 0a |enter * |* *}....|
|00003a10| 53 69 6e 63 65 20 77 65 | 20 63 61 6e 20 6f 66 74 |Since we| can oft|
|00003a20| 65 6e 20 75 6e 64 65 72 | 73 74 61 6e 64 20 77 68 |en under|stand wh|
|00003a30| 61 74 20 61 20 70 72 6f | 67 72 61 6d 20 64 6f 65 |at a pro|gram doe|
|00003a40| 73 20 28 6f 72 20 63 61 | 6e 20 64 6f 29 20 62 79 |s (or ca|n do) by|
|00003a50| 20 75 6e 64 65 72 73 74 | 61 6e 64 69 6e 67 0d 0a | underst|anding..|
|00003a60| 68 6f 77 20 69 74 20 72 | 65 70 72 65 73 65 6e 74 |how it r|epresent|
|00003a70| 73 20 69 74 73 20 64 61 | 74 61 2c 20 49 20 77 69 |s its da|ta, I wi|
|00003a80| 6c 6c 20 63 6f 76 65 72 | 20 74 68 65 20 64 61 74 |ll cover| the dat|
|00003a90| 61 20 73 74 72 75 63 74 | 75 72 65 20 6f 66 20 74 |a struct|ure of t|
|00003aa0| 68 65 20 74 68 72 65 61 | 64 0d 0a 63 6c 75 73 74 |he threa|d..clust|
|00003ab0| 65 72 20 66 69 72 73 74 | 2e 20 20 48 6f 77 65 76 |er first|. Howev|
|00003ac0| 65 72 2c 20 49 20 77 69 | 6c 6c 20 61 6c 73 6f 20 |er, I wi|ll also |
|00003ad0| 63 6f 76 65 72 20 69 74 | 73 20 63 6f 64 65 20 73 |cover it|s code s|
|00003ae0| 74 72 75 63 74 75 72 65 | 2c 20 64 65 66 69 6e 69 |tructure|, defini|
|00003af0| 6e 67 0d 0a 74 68 65 20 | 66 75 6e 63 74 69 6f 6e |ng..the |function|
|00003b00| 73 20 61 76 61 69 6c 61 | 62 6c 65 20 74 6f 20 61 |s availa|ble to a|
|00003b10| 63 63 65 73 73 20 74 68 | 69 73 20 64 61 74 61 20 |ccess th|is data |
|00003b20| 61 6e 64 20 68 6f 77 20 | 74 68 65 79 20 77 6f 72 |and how |they wor|
|00003b30| 6b 2e 0d 0a 0d 0a 5c 73 | 75 62 73 65 63 74 69 6f |k.....\s|ubsectio|
|00003b40| 6e 7b 54 68 65 20 54 68 | 72 65 61 64 20 43 6c 75 |n{The Th|read Clu|
|00003b50| 73 74 65 72 20 2d 20 44 | 61 74 61 20 53 74 72 75 |ster - D|ata Stru|
|00003b60| 63 74 75 72 65 7d 0d 0a | 0d 0a 54 68 65 20 62 61 |cture}..|..The ba|
|00003b70| 73 69 63 20 74 68 72 65 | 61 64 20 63 6c 75 73 74 |sic thre|ad clust|
|00003b80| 65 72 20 64 61 74 61 20 | 73 74 72 75 63 74 75 72 |er data |structur|
|00003b90| 65 20 69 73 20 4d 50 54 | 48 44 2c 20 77 68 69 63 |e is MPT|HD, whic|
|00003ba0| 68 20 68 6f 6c 64 73 20 | 74 68 65 20 63 6f 6e 74 |h holds |the cont|
|00003bb0| 65 78 74 0d 0a 6f 66 20 | 74 68 65 20 74 68 72 65 |ext..of |the thre|
|00003bc0| 61 64 2e 20 20 4c 6f 6f | 6b 20 61 74 20 74 68 65 |ad. Loo|k at the|
|00003bd0| 20 4d 50 54 48 44 27 73 | 20 73 74 72 75 63 74 75 | MPTHD's| structu|
|00003be0| 72 65 20 64 65 63 6c 61 | 72 61 74 69 6f 6e 20 6e |re decla|ration n|
|00003bf0| 6f 77 20 61 74 20 74 68 | 65 0d 0a 62 6f 74 74 6f |ow at th|e..botto|
|00003c00| 6d 20 6f 66 20 7b 5c 65 | 6d 20 6d 70 74 68 64 2e |m of {\e|m mpthd.|
|00003c10| 68 7d 2e 20 59 6f 75 20 | 77 69 6c 6c 20 6e 6f 74 |h}. You |will not|
|00003c20| 69 63 65 20 4d 50 54 48 | 44 20 63 6f 6e 74 61 69 |ice MPTH|D contai|
|00003c30| 6e 73 20 73 65 76 65 72 | 61 6c 20 66 69 65 6c 64 |ns sever|al field|
|00003c40| 73 2e 0d 0a 4d 6f 73 74 | 20 69 6d 70 6f 72 74 61 |s...Most| importa|
|00003c50| 6e 74 20 61 72 65 20 74 | 68 65 20 24 73 74 61 74 |nt are t|he $stat|
|00003c60| 65 24 20 66 69 65 6c 64 | 2c 20 77 68 69 63 68 20 |e$ field|, which |
|00003c70| 73 74 6f 72 65 73 20 74 | 68 65 20 63 75 72 72 65 |stores t|he curre|
|00003c80| 6e 74 20 70 72 6f 63 65 | 73 73 6f 72 0d 0a 73 74 |nt proce|ssor..st|
|00003c90| 61 74 65 20 77 68 65 6e | 20 74 68 65 20 74 68 72 |ate when| the thr|
|00003ca0| 65 61 64 20 69 73 20 6e | 6f 74 20 65 78 65 63 75 |ead is n|ot execu|
|00003cb0| 74 69 6e 67 2c 20 61 6e | 64 20 74 68 65 20 24 68 |ting, an|d the $h|
|00003cc0| 65 61 70 2f 73 74 61 63 | 6b 24 20 60 60 66 69 65 |eap/stac|k$ ``fie|
|00003cd0| 6c 64 27 27 0d 0a 28 69 | 74 27 73 20 72 65 61 6c |ld''..(i|t's real|
|00003ce0| 6c 79 20 61 66 74 65 72 | 20 74 68 65 20 73 74 72 |ly after| the str|
|00003cf0| 75 63 74 75 72 65 29 2c | 20 77 68 69 63 68 20 68 |ucture),| which h|
|00003d00| 6f 6c 64 73 20 74 68 65 | 20 74 68 72 65 61 64 27 |olds the| thread'|
|00003d10| 73 20 73 74 61 63 6b 20 | 28 6c 6f 63 61 6c 0d 0a |s stack |(local..|
|00003d20| 76 61 72 69 61 62 6c 65 | 73 20 61 6e 64 20 66 75 |variable|s and fu|
|00003d30| 6e 63 74 69 6f 6e 20 63 | 61 6c 6c 73 29 20 61 6e |nction c|alls) an|
|00003d40| 64 20 70 6f 73 73 69 62 | 6c 79 20 61 20 68 65 61 |d possib|ly a hea|
|00003d50| 70 20 6f 66 20 76 61 72 | 69 61 62 6c 65 73 20 74 |p of var|iables t|
|00003d60| 68 65 20 74 68 72 65 61 | 64 0d 0a 61 6c 6c 6f 63 |he threa|d..alloc|
|00003d70| 61 74 65 73 20 64 79 6e | 61 6d 69 63 61 6c 6c 79 |ates dyn|amically|
|00003d80| 5c 66 6f 6f 74 6e 6f 74 | 65 7b 52 6f 75 74 69 6e |\footnot|e{Routin|
|00003d90| 65 73 20 74 6f 20 6d 61 | 6e 61 67 65 20 74 68 69 |es to ma|nage thi|
|00003da0| 73 20 68 65 61 70 20 61 | 72 65 20 62 65 79 6f 6e |s heap a|re beyon|
|00003db0| 64 20 74 68 65 0d 0a 73 | 63 6f 70 65 20 6f 66 20 |d the..s|cope of |
|00003dc0| 74 68 69 73 20 61 72 74 | 69 63 6c 65 7d 2e 20 20 |this art|icle}. |
|00003dd0| 46 6f 72 20 62 65 67 69 | 6e 6e 69 6e 67 20 74 68 |For begi|nning th|
|00003de0| 65 20 74 68 72 65 61 64 | 2c 20 24 6d 70 73 69 67 |e thread|, $mpsig|
|00003df0| 73 74 61 74 65 24 20 68 | 6f 6c 64 73 20 74 68 65 |state$ h|olds the|
|00003e00| 0d 0a 73 69 67 6e 61 6c | 20 62 6c 6f 63 6b 65 64 |..signal| blocked|
|00003e10| 2f 75 6e 62 6c 6f 63 6b | 65 64 20 73 74 61 74 65 |/unblock|ed state|
|00003e20| 20 77 68 65 6e 20 74 68 | 65 20 74 68 72 65 61 64 | when th|e thread|
|00003e30| 20 77 61 73 20 69 6e 69 | 74 69 61 6c 69 7a 65 64 | was ini|tialized|
|00003e40| 20 73 6f 20 74 68 61 74 | 20 69 74 0d 0a 6d 61 79 | so that| it..may|
|00003e50| 20 62 65 20 72 65 73 74 | 6f 72 65 64 20 77 68 65 | be rest|ored whe|
|00003e60| 6e 20 74 68 65 20 74 68 | 72 65 61 64 20 69 73 20 |n the th|read is |
|00003e70| 66 69 72 73 74 20 73 74 | 61 72 74 65 64 2e 20 20 |first st|arted. |
|00003e80| 46 6f 72 20 65 78 65 63 | 75 74 69 6e 67 20 74 68 |For exec|uting th|
|00003e90| 65 0d 0a 74 68 72 65 61 | 64 2c 20 24 6d 70 74 68 |e..threa|d, $mpth|
|00003ea0| 64 66 6e 24 20 68 6f 6c | 64 73 20 74 68 65 20 74 |dfn$ hol|ds the t|
|00003eb0| 68 72 65 61 64 20 66 75 | 6e 63 74 69 6f 6e 20 74 |hread fu|nction t|
|00003ec0| 6f 20 62 65 20 65 78 65 | 63 75 74 65 64 20 72 65 |o be exe|cuted re|
|00003ed0| 70 65 61 74 65 64 6c 79 | 2e 0d 0a 46 6f 72 20 73 |peatedly|...For s|
|00003ee0| 77 69 74 63 68 69 6e 67 | 20 62 65 74 77 65 65 6e |witching| between|
|00003ef0| 20 74 68 72 65 61 64 73 | 2c 20 24 6f 6c 64 6d 70 | threads|, $oldmp|
|00003f00| 74 68 64 24 20 68 6f 6c | 64 73 20 74 68 65 20 74 |thd$ hol|ds the t|
|00003f10| 68 72 65 61 64 20 65 78 | 65 63 75 74 65 64 20 70 |hread ex|ecuted p|
|00003f20| 72 69 6f 72 0d 0a 74 6f | 20 74 68 69 73 20 6f 6e |rior..to| this on|
|00003f30| 65 2e 20 20 46 69 6e 61 | 6c 6c 79 2c 20 66 6f 72 |e. Fina|lly, for|
|00003f40| 20 73 61 66 65 74 79 2c | 20 24 73 74 61 63 6b 6f | safety,| $stacko|
|00003f50| 76 65 72 66 6c 6f 77 24 | 20 64 65 74 65 63 74 73 |verflow$| detects|
|00003f60| 20 28 62 65 63 6f 6d 65 | 73 0d 0a 6e 6f 6e 2d 7a | (become|s..non-z|
|00003f70| 65 72 6f 29 20 77 68 65 | 6e 20 74 68 65 20 63 6f |ero) whe|n the co|
|00003f80| 6e 74 65 78 74 20 68 61 | 73 20 62 65 63 6f 6d 65 |ntext ha|s become|
|00003f90| 20 69 6e 76 61 6c 69 64 | 20 64 75 65 20 74 6f 20 | invalid| due to |
|00003fa0| 73 74 61 63 6b 20 6f 76 | 65 72 66 6c 6f 77 20 28 |stack ov|erflow (|
|00003fb0| 77 68 65 6e 0d 0a 74 68 | 65 20 73 74 61 63 6b 20 |when..th|e stack |
|00003fc0| 68 61 73 20 67 72 6f 77 | 6e 20 6f 76 65 72 20 74 |has grow|n over t|
|00003fd0| 68 65 20 68 65 61 70 20 | 61 6e 64 20 70 72 6f 62 |he heap |and prob|
|00003fe0| 61 62 6c 79 20 6f 76 65 | 72 77 72 69 74 74 65 6e |ably ove|rwritten|
|00003ff0| 20 74 68 65 20 74 68 72 | 65 61 64 0d 0a 66 69 65 | the thr|ead..fie|
|00004000| 6c 64 73 29 2e 0d 0a 0d | 0a 49 74 20 69 73 20 61 |lds)....|.It is a|
|00004010| 73 73 75 6d 65 64 20 74 | 68 61 74 20 74 68 65 20 |ssumed t|hat the |
|00004020| 6d 61 63 72 6f 20 24 6d | 70 74 68 64 5f 6d 65 28 |macro $m|pthd_me(|
|00004030| 29 24 20 69 73 20 64 65 | 66 69 6e 65 64 20 74 6f |)$ is de|fined to|
|00004040| 20 62 65 20 61 20 64 65 | 64 69 63 61 74 65 64 0d | be a de|dicated.|
|00004050| 0a 72 65 67 69 73 74 65 | 72 20 6f 66 20 61 20 70 |.registe|r of a p|
|00004060| 72 6f 63 65 73 73 6f 72 | 20 73 6f 20 74 68 65 20 |rocessor| so the |
|00004070| 70 72 6f 63 65 73 73 6f | 72 20 63 61 6e 20 74 65 |processo|r can te|
|00004080| 6c 6c 20 77 68 69 63 68 | 20 74 68 72 65 61 64 20 |ll which| thread |
|00004090| 69 74 27 73 0d 0a 72 75 | 6e 6e 69 6e 67 20 28 49 |it's..ru|nning (I|
|000040a0| 6e 20 73 69 6e 67 6c 65 | 2d 70 72 6f 63 65 73 73 |n single|-process|
|000040b0| 6f 72 20 6d 61 63 68 69 | 6e 65 73 2c 20 74 68 69 |or machi|nes, thi|
|000040c0| 73 20 63 61 6e 20 62 65 | 20 61 20 67 6c 6f 62 61 |s can be| a globa|
|000040d0| 6c 20 76 61 72 69 61 62 | 6c 65 2e 29 2e 0d 0a 0d |l variab|le.)....|
|000040e0| 0a 0d 0a 5c 73 75 62 73 | 65 63 74 69 6f 6e 7b 54 |...\subs|ection{T|
|000040f0| 68 65 20 54 68 72 65 61 | 64 20 43 6c 75 73 74 65 |he Threa|d Cluste|
|00004100| 72 20 2d 20 43 6f 64 65 | 20 53 74 72 75 63 74 75 |r - Code| Structu|
|00004110| 72 65 7d 0d 0a 0d 0a 49 | 6e 20 74 68 69 73 20 73 |re}....I|n this s|
|00004120| 75 62 73 65 63 74 69 6f | 6e 2c 20 49 20 77 69 6c |ubsectio|n, I wil|
|00004130| 6c 20 63 6f 76 65 72 20 | 74 68 65 20 66 75 6e 63 |l cover |the func|
|00004140| 74 69 6f 6e 73 20 6e 65 | 65 64 65 64 20 74 6f 20 |tions ne|eded to |
|00004150| 66 75 6c 6c 79 20 69 6d | 70 6c 65 6d 65 6e 74 0d |fully im|plement.|
|00004160| 0a 63 6f 6e 74 65 78 74 | 20 73 77 69 74 63 68 69 |.context| switchi|
|00004170| 6e 67 2e 0d 0a 0d 0a 5c | 73 75 62 73 75 62 73 65 |ng.....\|subsubse|
|00004180| 63 74 69 6f 6e 7b 41 63 | 68 69 65 76 69 6e 67 20 |ction{Ac|hieving |
|00004190| 41 74 6f 6d 69 63 69 74 | 79 20 77 69 74 68 20 53 |Atomicit|y with S|
|000041a0| 69 67 6e 61 6c 73 20 61 | 6e 64 20 53 65 6d 61 70 |ignals a|nd Semap|
|000041b0| 68 6f 72 65 73 7d 0d 0a | 0d 0a 50 65 72 68 61 70 |hores}..|..Perhap|
|000041c0| 73 20 6d 6f 73 74 20 66 | 75 6e 64 61 6d 65 6e 74 |s most f|undament|
|000041d0| 61 6c 20 74 6f 20 6d 75 | 6c 74 69 74 61 73 6b 69 |al to mu|ltitaski|
|000041e0| 6e 67 2c 20 65 76 65 6e | 20 6d 6f 72 65 20 74 68 |ng, even| more th|
|000041f0| 61 6e 20 74 68 65 20 63 | 6f 6e 74 65 78 74 0d 0a |an the c|ontext..|
|00004200| 73 77 69 74 63 68 2c 20 | 69 73 20 74 68 65 20 6e |switch, |is the n|
|00004210| 65 65 64 20 66 6f 72 20 | 7b 5c 62 66 20 61 75 74 |eed for |{\bf aut|
|00004220| 6f 6e 6f 6d 6f 75 73 20 | 61 63 74 69 6f 6e 73 7d |onomous |actions}|
|00004230| 2e 20 20 49 74 20 69 73 | 20 6e 6f 74 20 73 75 72 |. It is| not sur|
|00004240| 70 72 69 73 69 6e 67 0d | 0a 74 68 61 74 20 74 68 |prising.|.that th|
|00004250| 65 79 20 61 72 65 20 61 | 6d 6f 6e 67 20 74 68 65 |ey are a|mong the|
|00004260| 20 66 69 72 73 74 20 74 | 6f 70 69 63 73 20 63 6f | first t|opics co|
|00004270| 76 65 72 65 64 20 69 6e | 20 74 65 78 74 73 20 6f |vered in| texts o|
|00004280| 6e 20 63 6f 6e 63 75 72 | 72 65 6e 74 0d 0a 6f 70 |n concur|rent..op|
|00004290| 65 72 61 74 69 6e 67 20 | 73 79 73 74 65 6d 73 20 |erating |systems |
|000042a0| 61 6e 64 20 6d 75 6c 74 | 69 74 61 73 6b 69 6e 67 |and mult|itasking|
|000042b0| 2e 5c 66 6f 6f 74 6e 6f | 74 65 7b 73 75 63 68 20 |.\footno|te{such |
|000042c0| 61 73 20 69 6e 20 4c 75 | 62 6f 6d 69 72 20 42 69 |as in Lu|bomir Bi|
|000042d0| 63 2c 0d 0a 5c 75 6e 64 | 65 72 6c 69 6e 65 7b 54 |c,..\und|erline{T|
|000042e0| 68 65 20 4c 6f 67 69 63 | 61 6c 20 44 65 73 69 67 |he Logic|al Desig|
|000042f0| 6e 20 6f 66 20 4f 70 65 | 72 61 74 69 6e 67 20 53 |n of Ope|rating S|
|00004300| 79 73 74 65 6d 73 7d 2c | 20 2e 2e 2e 7d 20 41 6e |ystems},| ...} An|
|00004310| 20 61 74 6f 6e 6f 6d 6f | 75 73 0d 0a 61 63 74 69 | atonomo|us..acti|
|00004320| 6f 6e 20 69 73 20 61 6e | 20 6f 70 65 72 61 74 69 |on is an| operati|
|00004330| 6f 6e 20 77 68 69 63 68 | 20 6d 75 73 74 20 62 65 |on which| must be|
|00004340| 20 63 6f 6d 70 6c 65 74 | 65 64 20 69 6e 20 6f 6e | complet|ed in on|
|00004350| 65 20 69 6e 64 69 76 69 | 73 69 62 6c 65 20 73 74 |e indivi|sible st|
|00004360| 65 70 2e 0d 0a 54 68 65 | 20 61 63 74 69 6f 6e 20 |ep...The| action |
|00004370| 63 61 6e 6e 6f 74 20 62 | 65 20 69 6e 74 65 72 72 |cannot b|e interr|
|00004380| 75 70 74 65 64 20 6f 72 | 20 70 61 72 74 69 61 6c |upted or| partial|
|00004390| 6c 79 20 63 6f 6d 70 6c | 65 74 65 64 2e 20 20 53 |ly compl|eted. S|
|000043a0| 6f 6d 65 20 74 68 69 6e | 67 73 20 69 6e 0d 0a 6c |ome thin|gs in..l|
|000043b0| 69 66 65 20 72 65 71 75 | 69 72 65 20 61 74 6f 6d |ife requ|ire atom|
|000043c0| 69 63 69 74 79 2e 20 20 | 57 68 65 6e 20 63 68 61 |icity. |When cha|
|000043d0| 6e 67 69 6e 67 20 63 6c | 6f 74 68 65 73 2c 20 79 |nging cl|othes, y|
|000043e0| 6f 75 20 64 6f 6e 27 74 | 20 77 61 6e 74 20 74 6f |ou don't| want to|
|000043f0| 20 62 65 0d 0a 63 61 75 | 67 68 74 20 77 69 74 68 | be..cau|ght with|
|00004400| 20 79 6f 75 72 20 70 61 | 6e 74 73 20 64 6f 77 6e | your pa|nts down|
|00004410| 2c 20 61 6e 64 20 74 68 | 65 20 73 61 6d 65 20 69 |, and th|e same i|
|00004420| 73 20 74 72 75 65 20 66 | 6f 72 20 63 6f 6d 70 75 |s true f|or compu|
|00004430| 74 65 72 73 21 0d 0a 0d | 0a 54 68 65 72 65 20 61 |ters!...|.There a|
|00004440| 72 65 20 74 77 6f 20 62 | 61 73 69 63 20 61 70 70 |re two b|asic app|
|00004450| 72 6f 61 63 68 65 73 20 | 77 68 69 63 68 20 77 65 |roaches |which we|
|00004460| 20 63 61 6e 20 74 61 6b | 65 20 74 6f 20 61 63 68 | can tak|e to ach|
|00004470| 69 65 76 65 20 61 74 6f | 6d 69 63 69 74 79 2e 0d |ieve ato|micity..|
|00004480| 0a 4f 6e 65 20 61 70 70 | 72 6f 61 63 68 20 69 73 |.One app|roach is|
|00004490| 20 74 6f 20 77 61 69 74 | 20 75 6e 74 69 6c 20 79 | to wait| until y|
|000044a0| 6f 75 20 63 61 6e 20 7b | 5c 65 6d 20 67 75 61 72 |ou can {|\em guar|
|000044b0| 61 6e 74 65 65 7d 20 79 | 6f 75 20 63 61 6e 20 64 |antee} y|ou can d|
|000044c0| 6f 20 77 68 61 74 0d 0a | 79 6f 75 27 76 65 20 67 |o what..|you've g|
|000044d0| 6f 74 20 74 6f 20 64 6f | 20 69 6e 20 60 60 6f 6e |ot to do| in ``on|
|000044e0| 65 20 61 63 74 69 6f 6e | 27 27 2e 20 20 54 6f 20 |e action|''. To |
|000044f0| 61 76 6f 69 64 20 74 68 | 65 20 77 61 69 74 2c 20 |avoid th|e wait, |
|00004500| 61 20 6d 6f 72 65 0d 0a | 73 6f 70 68 69 73 74 69 |a more..|sophisti|
|00004510| 63 61 74 65 64 20 61 70 | 70 72 6f 61 63 68 20 69 |cated ap|proach i|
|00004520| 73 20 74 6f 20 67 6f 20 | 61 68 65 61 64 20 61 6e |s to go |ahead an|
|00004530| 64 20 64 6f 20 77 68 61 | 74 20 79 6f 75 27 76 65 |d do wha|t you've|
|00004540| 20 67 6f 74 20 74 6f 20 | 64 6f 0d 0a 61 74 6f 6d | got to |do..atom|
|00004550| 69 63 6c 79 2c 20 61 6e | 64 20 69 66 20 79 6f 75 |icly, an|d if you|
|00004560| 27 72 65 20 69 6e 74 65 | 72 72 75 70 74 65 64 2c |'re inte|rrupted,|
|00004570| 20 74 6f 20 75 6e 64 6f | 20 77 68 61 74 20 79 6f | to undo| what yo|
|00004580| 75 27 76 65 20 64 6f 6e | 65 2e 0d 0a 48 6f 77 65 |u've don|e...Howe|
|00004590| 76 65 72 2c 20 74 68 69 | 73 20 61 70 70 72 6f 61 |ver, thi|s approa|
|000045a0| 63 68 20 69 73 20 6d 6f | 72 65 20 63 6f 6d 70 6c |ch is mo|re compl|
|000045b0| 69 63 61 74 65 64 2c 20 | 72 65 71 75 69 72 69 6e |icated, |requirin|
|000045c0| 67 20 74 68 61 74 20 79 | 6f 75 0d 0a 72 65 6d 65 |g that y|ou..reme|
|000045d0| 6d 62 65 72 20 65 76 65 | 72 79 74 68 69 6e 67 20 |mber eve|rything |
|000045e0| 79 6f 75 27 76 65 20 64 | 6f 6e 65 20 73 6f 20 66 |you've d|one so f|
|000045f0| 61 72 2e 20 20 4d 6f 72 | 65 20 69 6d 70 6f 72 74 |ar. Mor|e import|
|00004600| 61 6e 74 6c 79 2c 20 61 | 6c 6c 20 77 65 27 76 65 |antly, a|ll we've|
|00004610| 0d 0a 72 65 61 6c 6c 79 | 20 64 6f 6e 65 20 69 73 |..really| done is|
|00004620| 20 62 72 65 61 6b 20 75 | 70 20 6f 6e 65 20 62 69 | break u|p one bi|
|00004630| 67 20 60 60 61 74 6f 6d | 69 63 27 27 20 61 63 74 |g ``atom|ic'' act|
|00004640| 69 6f 6e 20 69 6e 74 6f | 20 73 6d 61 6c 6c 65 72 |ion into| smaller|
|00004650| 0d 0a 61 63 74 69 6f 6e | 73 2c 20 77 68 69 63 68 |..action|s, which|
|00004660| 2c 20 69 66 20 79 6f 75 | 20 74 68 69 6e 6b 20 61 |, if you| think a|
|00004670| 62 6f 75 74 20 69 74 2c | 20 6d 75 73 74 20 61 6c |bout it,| must al|
|00004680| 73 6f 20 62 65 20 61 74 | 6f 6d 69 63 2e 20 20 49 |so be at|omic. I|
|00004690| 6e 20 6f 74 68 65 72 0d | 0a 77 6f 72 64 73 2c 20 |n other.|.words, |
|000046a0| 77 65 27 72 65 20 67 6f | 69 6e 67 20 69 6e 20 63 |we're go|ing in c|
|000046b0| 69 72 63 6c 65 73 2e 20 | 20 54 68 75 73 20 61 74 |ircles. | Thus at|
|000046c0| 20 73 6f 6d 65 20 70 6f | 69 6e 74 2c 20 77 65 20 | some po|int, we |
|000046d0| 77 69 6c 6c 20 68 61 76 | 65 20 74 6f 0d 0a 68 61 |will hav|e to..ha|
|000046e0| 76 65 20 61 6e 20 61 63 | 74 69 6f 6e 20 77 68 69 |ve an ac|tion whi|
|000046f0| 63 68 20 6d 75 73 74 20 | 77 61 69 74 20 75 6e 74 |ch must |wait unt|
|00004700| 69 6c 20 69 74 20 63 61 | 6e 20 67 75 61 72 61 6e |il it ca|n guaran|
|00004710| 74 65 65 20 74 6f 0d 0a | 63 6f 6d 70 6c 65 74 65 |tee to..|complete|
|00004720| 2e 20 20 54 6f 20 69 6e | 73 75 72 65 20 77 65 20 |. To in|sure we |
|00004730| 61 72 65 20 6e 6f 74 20 | 69 6e 74 65 72 72 75 70 |are not |interrup|
|00004740| 74 65 64 2c 20 77 65 20 | 6e 65 65 64 20 74 6f 20 |ted, we |need to |
|00004750| 7b 5c 62 66 0d 0a 73 79 | 6e 63 68 72 6f 6e 69 7a |{\bf..sy|nchroniz|
|00004760| 65 7d 20 6f 75 72 73 65 | 6c 76 65 73 20 77 69 74 |e} ourse|lves wit|
|00004770| 68 20 61 6c 6c 20 70 61 | 72 74 73 20 6f 66 20 6f |h all pa|rts of o|
|00004780| 75 72 20 65 6e 76 69 72 | 6f 6e 6d 65 6e 74 20 77 |ur envir|onment w|
|00004790| 68 69 63 68 20 6d 61 79 | 0d 0a 69 6e 74 65 72 72 |hich may|..interr|
|000047a0| 75 70 74 20 75 73 2c 20 | 6e 61 6d 65 6c 79 20 6f |upt us, |namely o|
|000047b0| 74 68 65 72 20 70 72 6f | 63 65 73 73 6f 72 73 20 |ther pro|cessors |
|000047c0| 61 6e 64 20 69 6e 64 65 | 70 65 6e 64 65 6e 74 20 |and inde|pendent |
|000047d0| 68 61 72 64 77 61 72 65 | 20 74 68 65 0d 0a 63 6f |hardware| the..co|
|000047e0| 6d 70 75 74 65 72 20 6d | 61 79 20 68 61 76 65 2e |mputer m|ay have.|
|000047f0| 20 20 57 65 20 6e 65 65 | 64 20 74 68 65 20 73 79 | We nee|d the sy|
|00004800| 6e 63 68 72 6f 6e 69 63 | 69 74 79 20 6f 66 20 7b |nchronic|ity of {|
|00004810| 5c 62 66 20 73 69 67 6e | 61 6c 73 7d 20 61 6e 64 |\bf sign|als} and|
|00004820| 0d 0a 7b 5c 62 66 20 73 | 65 6d 61 70 68 6f 72 65 |..{\bf s|emaphore|
|00004830| 73 7d 2e 2e 2e 0d 0a 0d | 0a 5c 62 65 67 69 6e 7b |s}......|.\begin{|
|00004840| 64 65 73 63 72 69 70 74 | 69 6f 6e 7d 0d 0a 0d 0a |descript|ion}....|
|00004850| 5c 69 74 65 6d 5b 41 20 | 53 69 67 6e 61 6c 5d 2c |\item[A |Signal],|
|00004860| 20 6f 72 2c 20 69 6e 20 | 6c 6f 77 65 72 2d 6c 65 | or, in |lower-le|
|00004870| 76 65 6c 20 63 6f 6e 73 | 74 72 75 63 74 73 2c 20 |vel cons|tructs, |
|00004880| 61 6e 20 7b 5c 62 66 20 | 69 6e 74 65 72 72 75 70 |an {\bf |interrup|
|00004890| 74 7d 2c 20 69 73 20 61 | 0d 0a 63 6f 6d 6d 6f 6e |t}, is a|..common|
|000048a0| 20 6d 65 61 6e 73 20 75 | 73 65 64 20 74 6f 20 73 | means u|sed to s|
|000048b0| 79 6e 63 68 72 6f 6e 69 | 7a 65 20 61 20 70 72 6f |ynchroni|ze a pro|
|000048c0| 63 65 73 73 6f 72 20 77 | 69 74 68 20 65 78 74 65 |cessor w|ith exte|
|000048d0| 72 6e 61 6c 20 28 61 6e | 64 0d 0a 61 73 79 6e 63 |rnal (an|d..async|
|000048e0| 68 72 6f 6e 6f 75 73 29 | 20 68 61 72 64 77 61 72 |hronous)| hardwar|
|000048f0| 65 20 28 73 75 63 68 20 | 61 73 20 49 2f 4f 20 64 |e (such |as I/O d|
|00004900| 72 69 76 65 72 73 29 2e | 20 20 41 20 70 72 6f 63 |rivers).| A proc|
|00004910| 65 73 73 6f 72 20 73 75 | 70 70 6f 72 74 69 6e 67 |essor su|pporting|
|00004920| 0d 0a 69 6e 74 65 72 72 | 75 70 74 73 20 61 75 74 |..interr|upts aut|
|00004930| 6f 6d 61 74 69 63 61 6c | 6c 79 20 63 68 65 63 6b |omatical|ly check|
|00004940| 73 20 66 6f 72 20 61 6e | 79 20 70 65 6e 64 69 6e |s for an|y pendin|
|00004950| 67 20 69 6e 74 65 72 72 | 75 70 74 20 73 69 67 6e |g interr|upt sign|
|00004960| 61 6c 73 20 62 65 74 77 | 65 65 6e 0d 0a 65 61 63 |als betw|een..eac|
|00004970| 68 20 69 6e 73 74 72 75 | 63 74 69 6f 6e 2e 20 20 |h instru|ction. |
|00004980| 49 66 20 61 6e 20 69 6e | 74 65 72 72 75 70 74 20 |If an in|terrupt |
|00004990| 69 73 20 70 65 6e 64 69 | 6e 67 2c 20 74 68 65 20 |is pendi|ng, the |
|000049a0| 70 72 6f 63 65 73 73 6f | 72 20 73 75 73 70 65 6e |processo|r suspen|
|000049b0| 64 73 20 77 68 61 74 0d | 0a 69 74 20 69 73 20 64 |ds what.|.it is d|
|000049c0| 6f 69 6e 67 20 61 6e 64 | 20 63 61 6c 6c 73 20 61 |oing and| calls a|
|000049d0| 6e 20 7b 5c 62 66 20 69 | 6e 74 65 72 72 75 70 74 |n {\bf i|nterrupt|
|000049e0| 20 68 61 6e 64 6c 65 72 | 7d 20 72 6f 75 74 69 6e | handler|} routin|
|000049f0| 65 2c 20 73 75 63 68 20 | 61 73 20 61 20 70 72 6f |e, such |as a pro|
|00004a00| 63 65 64 75 72 65 0d 0a | 74 6f 20 72 65 61 64 20 |cedure..|to read |
|00004a10| 69 6e 20 74 68 65 20 63 | 68 61 72 61 63 74 65 72 |in the c|haracter|
|00004a20| 20 77 68 65 6e 20 6f 6e | 65 20 68 61 73 20 61 72 | when on|e has ar|
|00004a30| 72 69 76 65 64 20 61 74 | 20 74 68 65 20 6b 65 79 |rived at| the key|
|00004a40| 62 6f 61 72 64 2e 20 20 | 57 68 65 6e 20 74 68 65 |board. |When the|
|00004a50| 0d 0a 68 61 6e 64 6c 65 | 72 20 72 65 74 75 72 6e |..handle|r return|
|00004a60| 73 2c 20 74 68 65 20 70 | 72 6f 63 65 73 73 6f 72 |s, the p|rocessor|
|00004a70| 20 28 75 73 75 61 6c 6c | 79 29 20 63 6f 6e 74 69 | (usuall|y) conti|
|00004a80| 6e 75 65 73 20 61 73 20 | 69 66 20 6e 6f 74 68 69 |nues as |if nothi|
|00004a90| 6e 67 20 68 61 64 0d 0a | 68 61 70 70 65 6e 65 64 |ng had..|happened|
|00004aa0| 2e 0d 0a 0d 0a 49 6e 74 | 65 72 72 75 70 74 73 20 |.....Int|errupts |
|00004ab0| 68 65 6c 70 20 74 68 65 | 20 70 72 6f 67 72 61 6d |help the| program|
|00004ac0| 6d 65 72 20 62 79 20 61 | 75 74 6f 6d 61 74 69 63 |mer by a|utomatic|
|00004ad0| 61 6c 6c 79 20 7b 5c 62 | 66 20 70 6f 6c 6c 69 6e |ally {\b|f pollin|
|00004ae0| 67 7d 20 7b 5c 65 6d 20 | 69 6e 0d 0a 68 61 72 64 |g} {\em |in..hard|
|00004af0| 77 61 72 65 7d 20 69 66 | 20 73 70 65 63 69 66 69 |ware} if| specifi|
|00004b00| 63 20 65 76 65 6e 74 73 | 20 68 61 76 65 20 6f 63 |c events| have oc|
|00004b10| 63 75 72 72 65 64 2c 20 | 61 6c 6c 6f 77 69 6e 67 |curred, |allowing|
|00004b20| 20 6d 6f 72 65 20 74 69 | 6d 65 6c 79 20 28 66 61 | more ti|mely (fa|
|00004b30| 73 74 65 72 29 0d 0a 69 | 6e 74 65 72 61 63 74 69 |ster)..i|nteracti|
|00004b40| 6f 6e 20 77 69 74 68 20 | 74 68 65 20 6f 75 74 73 |on with |the outs|
|00004b50| 69 64 65 20 77 6f 72 6c | 64 2e 20 20 48 6f 77 65 |ide worl|d. Howe|
|00004b60| 76 65 72 2c 20 74 68 65 | 69 72 20 68 61 6e 64 6c |ver, the|ir handl|
|00004b70| 65 72 73 20 6d 75 73 74 | 20 6d 61 6b 65 0d 0a 73 |ers must| make..s|
|00004b80| 6f 6d 65 20 63 68 61 6e | 67 65 20 74 6f 20 74 68 |ome chan|ge to th|
|00004b90| 65 20 63 6f 6d 70 75 74 | 65 72 27 73 20 65 6e 76 |e comput|er's env|
|00004ba0| 69 72 6f 6e 6d 65 6e 74 | 20 74 6f 20 68 61 76 65 |ironment| to have|
|00004bb0| 20 61 6e 20 65 66 66 65 | 63 74 2e 20 20 41 74 20 | an effe|ct. At |
|00004bc0| 63 65 72 74 61 69 6e 0d | 0a 70 6f 69 6e 74 73 2c |certain.|.points,|
|00004bd0| 20 74 68 6f 75 67 68 2c | 20 77 65 20 64 6f 6e 27 | though,| we don'|
|00004be0| 74 20 77 61 6e 74 20 74 | 68 65 6d 20 74 6f 20 6d |t want t|hem to m|
|00004bf0| 61 6b 65 20 61 6e 79 20 | 63 68 61 6e 67 65 73 3b |ake any |changes;|
|00004c00| 20 66 6f 72 20 65 78 61 | 6d 70 6c 65 2c 20 77 65 | for exa|mple, we|
|00004c10| 0d 0a 6d 61 79 20 6e 6f | 74 20 77 61 6e 74 20 74 |..may no|t want t|
|00004c20| 6f 20 62 65 20 69 6e 74 | 65 72 72 75 70 74 65 64 |o be int|errupted|
|00004c30| 20 77 68 69 6c 65 20 73 | 65 72 76 69 63 69 6e 67 | while s|ervicing|
|00004c40| 20 61 6e 20 69 6e 74 65 | 72 72 75 70 74 2c 20 6e | an inte|rrupt, n|
|00004c50| 6f 72 20 77 6f 75 6c 64 | 20 77 65 0d 0a 6c 69 6b |or would| we..lik|
|00004c60| 65 20 74 6f 20 62 65 20 | 69 6e 74 65 72 72 75 70 |e to be |interrup|
|00004c70| 74 65 64 20 77 68 65 6e | 20 77 6f 72 6b 69 6e 67 |ted when| working|
|00004c80| 20 6f 6e 20 64 61 74 61 | 20 61 6e 20 69 6e 74 65 | on data| an inte|
|00004c90| 72 72 75 70 74 20 6d 61 | 79 20 6d 6f 64 69 66 79 |rrupt ma|y modify|
|00004ca0| 2e 20 20 49 6e 0d 0a 6f | 74 68 65 72 20 77 6f 72 |. In..o|ther wor|
|00004cb0| 64 73 2c 20 77 68 65 6e | 20 77 6f 72 6b 69 6e 67 |ds, when| working|
|00004cc0| 20 77 69 74 68 20 61 20 | 70 69 65 63 65 20 6f 66 | with a |piece of|
|00004cd0| 20 64 61 74 61 2c 20 77 | 65 20 6e 65 65 64 20 74 | data, w|e need t|
|00004ce0| 6f 20 7b 5c 62 66 20 6d | 61 73 6b 7d 20 6f 72 0d |o {\bf m|ask} or.|
|00004cf0| 0a 7b 5c 62 66 20 62 6c | 6f 63 6b 7d 20 61 6c 6c |.{\bf bl|ock} all|
|00004d00| 20 69 6e 74 65 72 72 75 | 70 74 73 20 77 68 69 63 | interru|pts whic|
|00004d10| 68 20 6d 61 79 20 64 69 | 72 65 63 74 6c 79 20 6f |h may di|rectly o|
|00004d20| 72 20 69 6e 64 69 72 65 | 63 74 6c 79 20 6d 6f 64 |r indire|ctly mod|
|00004d30| 69 66 79 20 74 68 61 74 | 0d 0a 64 61 74 61 2e 20 |ify that|..data. |
|00004d40| 20 49 66 20 77 65 20 61 | 72 65 20 6f 6e 6c 79 20 | If we a|re only |
|00004d50| 67 6f 69 6e 67 20 74 6f | 20 62 65 20 77 6f 72 6b |going to| be work|
|00004d60| 69 6e 67 20 77 69 74 68 | 20 74 68 65 20 64 61 74 |ing with| the dat|
|00004d70| 61 20 61 20 73 68 6f 72 | 74 20 74 69 6d 65 2c 0d |a a shor|t time,.|
|00004d80| 0a 70 65 72 68 61 70 73 | 20 74 68 65 20 73 61 66 |.perhaps| the saf|
|00004d90| 65 73 74 20 61 6e 64 20 | 65 61 73 69 65 73 74 20 |est and |easiest |
|00004da0| 73 74 72 61 74 65 67 79 | 20 69 73 20 6a 75 73 74 |strategy| is just|
|00004db0| 20 74 6f 20 62 6c 6f 63 | 6b 20 61 6c 6c 20 69 6e | to bloc|k all in|
|00004dc0| 74 65 72 72 75 70 74 73 | 2e 0d 0a 54 68 69 73 20 |terrupts|...This |
|00004dd0| 69 73 20 77 68 61 74 20 | 74 68 65 20 7b 5c 65 6d |is what |the {\em|
|00004de0| 20 6d 70 73 69 67 2e 68 | 7d 20 63 6f 64 65 20 64 | mpsig.h|} code d|
|00004df0| 6f 65 73 20 62 79 20 6d | 6f 64 69 66 79 69 6e 67 |oes by m|odifying|
|00004e00| 20 74 68 65 20 24 49 46 | 20 66 6c 61 67 24 0d 0a | the $IF| flag$..|
|00004e10| 28 4d 53 2d 44 4f 53 20 | 76 65 72 73 69 6f 6e 29 |(MS-DOS |version)|
|00004e20| 20 6f 72 20 63 61 6c 6c | 69 6e 67 20 74 68 65 20 | or call|ing the |
|00004e30| 24 73 65 74 73 69 67 6d | 61 73 6b 24 20 28 55 4e |$setsigm|ask$ (UN|
|00004e40| 49 58 20 76 65 72 73 69 | 6f 6e 29 2e 20 20 46 6f |IX versi|on). Fo|
|00004e50| 72 0d 0a 63 6f 6e 76 65 | 6e 69 65 6e 63 65 20 28 |r..conve|nience (|
|00004e60| 61 6e 64 20 74 6f 20 65 | 6d 70 68 61 73 69 7a 65 |and to e|mphasize|
|00004e70| 20 74 68 65 20 74 65 6d | 70 6f 72 61 72 79 20 6e | the tem|porary n|
|00004e80| 61 74 75 72 65 20 6f 66 | 20 73 69 67 6e 61 6c 20 |ature of| signal |
|00004e90| 62 6c 6f 63 6b 69 6e 67 | 29 2c 20 49 0d 0a 68 61 |blocking|), I..ha|
|00004ea0| 76 65 20 69 6e 63 6c 75 | 64 65 64 20 61 20 24 6d |ve inclu|ded a $m|
|00004eb0| 70 73 69 67 5c 5f 63 72 | 69 74 73 65 63 74 24 20 |psig\_cr|itsect$ |
|00004ec0| 6d 61 63 72 6f 20 74 6f | 20 64 69 73 61 62 6c 65 |macro to| disable|
|00004ed0| 20 69 6e 74 65 72 72 75 | 70 74 73 20 61 72 6f 75 | interru|pts arou|
|00004ee0| 6e 64 20 61 0d 0a 73 70 | 65 63 69 66 69 65 64 20 |nd a..sp|ecified |
|00004ef0| 62 6c 6f 63 6b 20 6f 66 | 20 63 6f 64 65 2e 0d 0a |block of| code...|
|00004f00| 0d 0a 5c 69 74 65 6d 5b | 41 20 53 65 6d 61 70 68 |..\item[|A Semaph|
|00004f10| 6f 72 65 5d 20 41 73 20 | 73 69 67 6e 61 6c 73 20 |ore] As |signals |
|00004f20| 73 79 6e 63 68 72 6f 6e | 69 7a 65 20 74 68 65 20 |synchron|ize the |
|00004f30| 70 72 6f 63 65 73 73 6f | 72 20 77 69 74 68 20 6f |processo|r with o|
|00004f40| 74 68 65 72 0d 0a 68 61 | 72 64 77 61 72 65 2c 20 |ther..ha|rdware, |
|00004f50| 73 65 6d 61 70 68 6f 72 | 65 73 20 73 79 6e 63 68 |semaphor|es synch|
|00004f60| 72 6f 6e 69 7a 65 20 69 | 74 20 77 69 74 68 20 6f |ronize i|t with o|
|00004f70| 74 68 65 72 20 70 72 6f | 63 65 73 73 6f 72 73 2e |ther pro|cessors.|
|00004f80| 20 20 41 20 73 65 6d 61 | 70 68 6f 72 65 20 69 73 | A sema|phore is|
|00004f90| 0d 0a 6c 69 6b 65 20 61 | 20 67 61 74 65 20 77 68 |..like a| gate wh|
|00004fa0| 69 63 68 20 77 69 6c 6c | 20 6f 6e 6c 79 20 6c 65 |ich will| only le|
|00004fb0| 74 20 61 20 63 65 72 74 | 61 69 6e 20 6e 75 6d 62 |t a cert|ain numb|
|00004fc0| 65 72 20 6f 66 20 70 65 | 6f 70 6c 65 20 28 70 72 |er of pe|ople (pr|
|00004fd0| 6f 63 65 73 73 6f 72 73 | 29 0d 0a 74 68 72 6f 75 |ocessors|)..throu|
|00004fe0| 67 68 2c 20 75 73 75 61 | 6c 6c 79 20 6a 75 73 74 |gh, usua|lly just|
|00004ff0| 20 6f 6e 65 2e 20 20 57 | 65 20 63 61 6e 20 6c 69 | one. W|e can li|
|00005000| 6d 69 74 20 74 68 65 20 | 73 69 6d 75 6c 74 61 6e |mit the |simultan|
|00005010| 65 6f 75 73 20 61 63 63 | 65 73 73 20 6f 66 0d 0a |eous acc|ess of..|
|00005020| 63 65 72 74 61 69 6e 20 | 64 61 74 61 20 6f 72 20 |certain |data or |
|00005030| 74 68 65 20 73 69 6d 75 | 6c 74 61 6e 65 6f 75 73 |the simu|ltaneous|
|00005040| 20 65 76 61 6c 75 61 74 | 69 6f 6e 20 6f 66 20 63 | evaluat|ion of c|
|00005050| 65 72 74 61 69 6e 20 63 | 6f 64 65 20 62 79 0d 0a |ertain c|ode by..|
|00005060| 61 73 73 6f 63 69 61 74 | 69 6e 67 20 61 20 73 65 |associat|ing a se|
|00005070| 6d 61 70 68 6f 72 65 20 | 77 69 74 68 20 69 74 20 |maphore |with it |
|00005080| 61 6e 64 20 72 65 71 75 | 69 72 69 6e 67 20 65 61 |and requ|iring ea|
|00005090| 63 68 20 70 72 6f 63 65 | 73 73 20 63 6c 61 69 6d |ch proce|ss claim|
|000050a0| 20 74 68 65 0d 0a 73 65 | 6d 61 70 68 6f 72 65 20 | the..se|maphore |
|000050b0| 62 65 66 6f 72 65 20 69 | 74 20 70 72 6f 63 65 65 |before i|t procee|
|000050c0| 64 73 2c 20 61 6e 64 20 | 72 65 6c 65 61 73 65 20 |ds, and |release |
|000050d0| 69 74 20 77 68 65 6e 20 | 69 74 27 73 20 74 68 72 |it when |it's thr|
|000050e0| 6f 75 67 68 2e 0d 0a 0d | 0a 54 68 6f 75 67 68 20 |ough....|.Though |
|000050f0| 61 20 73 69 6d 70 6c 65 | 20 69 64 65 61 2c 20 69 |a simple| idea, i|
|00005100| 6d 70 6c 65 6d 65 6e 74 | 69 6e 67 20 73 65 6d 61 |mplement|ing sema|
|00005110| 70 68 6f 72 65 73 20 69 | 73 20 73 6f 6d 65 77 68 |phores i|s somewh|
|00005120| 61 74 20 74 72 69 63 6b | 79 2e 20 20 41 74 20 73 |at trick|y. At s|
|00005130| 6f 6d 65 0d 0a 70 6f 69 | 6e 74 20 77 65 20 6d 75 |ome..poi|nt we mu|
|00005140| 73 74 20 68 61 76 65 20 | 73 6f 6d 65 20 7b 5c 65 |st have |some {\e|
|00005150| 6d 20 69 6e 64 69 76 69 | 73 69 62 6c 65 7d 20 6f |m indivi|sible} o|
|00005160| 70 65 72 61 74 69 6f 6e | 20 77 68 69 63 68 20 65 |peration| which e|
|00005170| 69 74 68 65 72 20 65 78 | 63 68 61 6e 67 65 73 0d |ither ex|changes.|
|00005180| 0a 64 61 74 61 20 62 65 | 74 77 65 65 6e 20 61 20 |.data be|tween a |
|00005190| 70 72 6f 63 65 73 73 6f | 72 27 73 20 6c 6f 63 61 |processo|r's loca|
|000051a0| 6c 20 28 72 65 67 69 73 | 74 65 72 29 20 6d 65 6d |l (regis|ter) mem|
|000051b0| 6f 72 79 20 61 6e 64 20 | 67 6c 6f 62 61 6c 20 73 |ory and |global s|
|000051c0| 68 61 72 65 64 0d 0a 6d | 65 6d 6f 72 79 2c 20 6f |hared..m|emory, o|
|000051d0| 72 20 73 6f 6d 65 74 68 | 69 6e 67 20 77 68 69 63 |r someth|ing whic|
|000051e0| 68 20 65 73 73 65 6e 74 | 69 61 6c 6c 79 20 69 6e |h essent|ially in|
|000051f0| 63 72 65 6d 65 6e 74 73 | 20 6f 72 20 64 65 63 72 |crements| or decr|
|00005200| 65 6d 65 6e 74 73 20 74 | 68 65 20 67 6c 6f 62 61 |ements t|he globa|
|00005210| 6c 0d 0a 6d 65 6d 6f 72 | 79 20 6c 6f 63 61 74 69 |l..memor|y locati|
|00005220| 6f 6e 2e 20 20 54 68 6f | 75 67 68 20 74 68 65 20 |on. Tho|ugh the |
|00005230| 66 6f 72 6d 65 72 20 69 | 73 20 6d 6f 73 74 20 65 |former i|s most e|
|00005240| 66 66 69 63 69 65 6e 74 | 2c 20 74 68 65 20 6c 61 |fficient|, the la|
|00005250| 74 74 65 72 20 68 61 70 | 70 65 6e 73 0d 0a 74 6f |tter hap|pens..to|
|00005260| 20 62 65 20 61 76 61 69 | 6c 61 62 6c 65 20 69 6e | be avai|lable in|
|00005270| 20 60 60 43 27 27 20 61 | 6e 64 20 74 61 6b 65 73 | ``C'' a|nd takes|
|00005280| 20 6e 6f 20 6c 6f 77 2d | 6c 65 76 65 6c 20 61 73 | no low-|level as|
|00005290| 73 65 6d 62 6c 79 20 70 | 72 6f 67 72 61 6d 6d 69 |sembly p|rogrammi|
|000052a0| 6e 67 2e 20 20 54 68 65 | 0d 0a 7b 5c 65 6d 20 6d |ng. The|..{\em m|
|000052b0| 70 73 65 6d 2e 68 7d 20 | 63 6f 64 65 20 61 74 74 |psem.h} |code att|
|000052c0| 65 6d 70 74 73 20 74 6f | 20 61 63 63 6f 6d 70 6c |empts to| accompl|
|000052d0| 69 73 68 20 74 68 69 73 | 20 69 6e 20 60 60 43 27 |ish this| in ``C'|
|000052e0| 27 20 76 69 61 20 74 68 | 65 20 24 2b 2b 76 61 72 |' via th|e $++var|
|000052f0| 24 0d 0a 70 72 65 2d 69 | 6e 63 72 65 6d 65 6e 74 |$..pre-i|ncrement|
|00005300| 20 6f 70 65 72 61 74 6f | 72 20 6f 6e 20 61 20 24 | operato|r on a $|
|00005310| 76 6f 6c 61 74 69 6c 65 | 24 20 69 6e 74 65 67 65 |volatile|$ intege|
|00005320| 72 2e 20 20 4f 6e 20 6d | 6f 73 74 20 6d 61 63 68 |r. On m|ost mach|
|00005330| 69 6e 65 73 2c 20 74 68 | 69 73 0d 0a 73 68 6f 75 |ines, th|is..shou|
|00005340| 6c 64 20 63 6f 6d 70 69 | 6c 65 20 74 6f 20 61 20 |ld compi|le to a |
|00005350| 73 69 6e 67 6c 65 20 6d | 61 63 68 69 6e 65 20 69 |single m|achine i|
|00005360| 6e 73 74 72 75 63 74 69 | 6f 6e 2e 20 20 28 54 68 |nstructi|on. (Th|
|00005370| 65 20 70 72 6f 62 6c 65 | 6d 20 6f 66 20 74 68 69 |e proble|m of thi|
|00005380| 73 0d 0a 73 63 68 65 6d | 65 20 69 73 20 74 68 61 |s..schem|e is tha|
|00005390| 74 20 69 74 20 72 65 71 | 75 69 72 65 73 20 74 6f |t it req|uires to|
|000053a0| 20 62 65 20 65 66 66 69 | 63 69 65 6e 74 20 61 20 | be effi|cient a |
|000053b0| 62 61 63 6b 2d 6f 66 66 | 20 73 74 72 61 74 65 67 |back-off| strateg|
|000053c0| 79 20 28 61 20 64 65 6c | 61 79 29 0d 0a 77 68 65 |y (a del|ay)..whe|
|000053d0| 6e 20 73 65 76 65 72 61 | 6c 20 70 72 6f 63 65 73 |n severa|l proces|
|000053e0| 73 65 73 20 61 72 65 20 | 67 72 61 62 62 69 6e 67 |ses are |grabbing|
|000053f0| 20 66 6f 72 20 74 68 65 | 20 73 61 6d 65 20 73 65 | for the| same se|
|00005400| 6d 61 70 68 6f 72 65 20 | 28 4e 4f 54 20 49 4e 20 |maphore |(NOT IN |
|00005410| 43 55 52 52 45 4e 54 0d | 0a 49 4d 50 4c 45 4d 45 |CURRENT.|.IMPLEME|
|00005420| 4e 54 41 54 49 4f 4e 29 | 29 2e 20 20 46 6f 72 20 |NTATION)|). For |
|00005430| 63 6f 6e 76 65 6e 69 65 | 6e 63 65 20 28 61 6e 64 |convenie|nce (and|
|00005440| 20 74 6f 20 65 6d 70 68 | 61 73 69 7a 65 20 74 68 | to emph|asize th|
|00005450| 65 20 74 65 6d 70 6f 72 | 61 72 79 20 6e 61 74 75 |e tempor|ary natu|
|00005460| 72 65 0d 0a 6f 66 20 73 | 69 67 6e 61 6c 20 62 6c |re..of s|ignal bl|
|00005470| 6f 63 6b 69 6e 67 29 2c | 20 49 20 68 61 76 65 20 |ocking),| I have |
|00005480| 69 6e 63 6c 75 64 65 64 | 20 61 20 24 6d 70 73 69 |included| a $mpsi|
|00005490| 67 5c 5f 63 72 69 74 73 | 65 63 74 24 20 6d 61 63 |g\_crits|ect$ mac|
|000054a0| 72 6f 20 74 6f 20 63 6c | 61 69 6d 20 61 0d 0a 73 |ro to cl|aim a..s|
|000054b0| 65 6d 61 70 68 6f 72 65 | 20 61 6e 64 20 28 74 68 |emaphore| and (th|
|000054c0| 65 6e 29 20 64 69 73 61 | 62 6c 65 20 69 6e 74 65 |en) disa|ble inte|
|000054d0| 72 72 75 70 74 73 20 61 | 72 6f 75 6e 64 20 61 20 |rrupts a|round a |
|000054e0| 73 70 65 63 69 66 69 65 | 64 20 62 6c 6f 63 6b 20 |specifie|d block |
|000054f0| 6f 66 20 63 6f 64 65 2e | 0d 0a 0d 0a 5c 65 6e 64 |of code.|....\end|
|00005500| 7b 64 65 73 63 72 69 70 | 74 69 6f 6e 7d 0d 0a 0d |{descrip|tion}...|
|00005510| 0a 54 68 65 73 65 20 73 | 79 6e 63 68 72 6f 6e 69 |.These s|ynchroni|
|00005520| 7a 61 74 69 6f 6e 20 74 | 6f 6f 6c 73 2c 20 73 69 |zation t|ools, si|
|00005530| 67 6e 61 6c 73 20 61 6e | 64 20 73 65 6d 61 70 68 |gnals an|d semaph|
|00005540| 6f 72 65 73 2c 20 61 72 | 65 20 6e 65 63 65 73 73 |ores, ar|e necess|
|00005550| 61 72 79 20 74 6f 0d 0a | 62 75 69 6c 64 20 61 6e |ary to..|build an|
|00005560| 20 65 66 66 69 63 69 65 | 6e 74 20 61 6e 64 20 63 | efficie|nt and c|
|00005570| 6f 72 72 65 63 74 20 6d | 75 6c 74 69 70 72 6f 63 |orrect m|ultiproc|
|00005580| 65 73 73 69 6e 67 20 73 | 63 68 65 64 75 6c 65 72 |essing s|cheduler|
|00005590| 2e 20 20 48 6f 77 65 76 | 65 72 2c 20 74 68 65 79 |. Howev|er, they|
|000055a0| 0d 0a 61 72 65 20 69 6e | 74 65 6e 64 65 64 20 7b |..are in|tended {|
|000055b0| 5c 65 6d 20 6f 6e 6c 79 | 7d 20 66 6f 72 20 74 68 |\em only|} for th|
|000055c0| 65 73 65 20 6c 6f 77 2d | 6c 65 76 65 6c 20 73 79 |ese low-|level sy|
|000055d0| 73 74 65 6d 20 75 74 69 | 6c 69 74 69 65 73 2e 20 |stem uti|lities. |
|000055e0| 20 46 6f 72 0d 0a 73 69 | 6d 70 6c 69 63 69 74 79 | For..si|mplicity|
|000055f0| 2c 20 69 6e 73 74 65 61 | 64 20 6f 66 20 73 69 67 |, instea|d of sig|
|00005600| 6e 61 6c 73 2c 20 68 69 | 67 68 65 72 2d 6c 65 76 |nals, hi|gher-lev|
|00005610| 65 6c 20 7b 5c 65 6d 20 | 74 61 73 6b 7d 20 49 2f |el {\em |task} I/|
|00005620| 4f 20 73 68 6f 75 6c 64 | 20 6d 6f 72 65 0d 0a 73 |O should| more..s|
|00005630| 74 72 61 69 67 68 74 2d | 66 6f 72 77 61 72 64 20 |traight-|forward |
|00005640| 63 6f 6e 73 74 72 75 63 | 74 73 20 61 73 20 7b 5c |construc|ts as {\|
|00005650| 65 6d 20 73 74 72 65 61 | 6d 73 7d 2e 20 20 41 6e |em strea|ms}. An|
|00005660| 64 20 66 6f 72 20 65 66 | 66 69 63 69 65 6e 63 79 |d for ef|ficiency|
|00005670| 2c 20 69 6e 73 74 65 61 | 64 0d 0a 6f 66 20 73 65 |, instea|d..of se|
|00005680| 6d 61 70 68 6f 72 65 73 | 2c 20 68 69 67 68 65 72 |maphores|, higher|
|00005690| 2d 6c 65 76 65 6c 20 7b | 5c 65 6d 20 74 61 73 6b |-level {|\em task|
|000056a0| 7d 20 73 79 6e 63 68 72 | 6f 6e 69 7a 61 74 69 6f |} synchr|onizatio|
|000056b0| 6e 20 73 68 6f 75 6c 64 | 20 75 73 65 20 6d 6f 72 |n should| use mor|
|000056c0| 65 0d 0a 63 6f 6e 74 72 | 6f 6c 6c 61 62 6c 65 20 |e..contr|ollable |
|000056d0| 63 6f 6e 73 74 72 75 63 | 74 73 20 61 73 20 7b 5c |construc|ts as {\|
|000056e0| 65 6d 20 63 6f 6e 64 69 | 74 69 6f 6e 61 6c 20 77 |em condi|tional w|
|000056f0| 61 69 74 73 7d 2e 20 20 | 28 4d 6f 72 65 20 6f 6e |aits}. |(More on|
|00005700| 20 61 0d 0a 6e 69 66 74 | 79 20 61 62 73 74 72 61 | a..nift|y abstra|
|00005710| 63 74 69 6f 6e 20 74 6f | 20 69 6d 70 6c 65 6d 65 |ction to| impleme|
|00005720| 6e 74 20 74 68 65 73 65 | 20 69 64 65 61 73 20 69 |nt these| ideas i|
|00005730| 6e 20 61 20 73 75 62 73 | 65 71 75 65 6e 74 20 61 |n a subs|equent a|
|00005740| 72 74 69 63 6c 65 2e 29 | 0d 0a 0d 0a 5c 73 75 62 |rticle.)|....\sub|
|00005750| 73 75 62 73 65 63 74 69 | 6f 6e 7b 53 77 69 74 63 |subsecti|on{Switc|
|00005760| 68 69 6e 67 20 42 65 74 | 77 65 65 6e 20 43 6f 6e |hing Bet|ween Con|
|00005770| 74 65 78 74 73 3a 20 20 | 24 6d 70 74 68 64 5c 5f |texts: |$mpthd\_|
|00005780| 73 77 69 74 63 68 24 7d | 0d 0a 0d 0a 4e 6f 77 20 |switch$}|....Now |
|00005790| 74 68 61 74 20 77 65 27 | 76 65 20 64 65 66 69 6e |that we'|ve defin|
|000057a0| 65 64 20 77 68 61 74 20 | 61 20 74 68 72 65 61 64 |ed what |a thread|
|000057b0| 20 63 6f 6e 74 65 78 74 | 20 69 73 20 61 6e 64 20 | context| is and |
|000057c0| 68 6f 77 20 77 65 20 63 | 61 6e 20 73 79 6e 63 68 |how we c|an synch|
|000057d0| 72 6f 6e 69 7a 65 0d 0a | 69 74 73 20 65 6e 76 69 |ronize..|its envi|
|000057e0| 72 6f 6e 6d 65 6e 74 2c | 20 77 65 20 6e 65 65 64 |ronment,| we need|
|000057f0| 20 61 20 77 61 79 20 74 | 6f 20 73 77 69 74 63 68 | a way t|o switch|
|00005800| 20 62 65 74 77 65 65 6e | 20 63 6f 6e 74 65 78 74 | between| context|
|00005810| 73 2e 20 20 46 6f 72 74 | 75 6e 61 74 65 6c 79 2c |s. Fort|unately,|
|00005820| 0d 0a 60 60 43 27 27 20 | 70 72 6f 76 69 64 65 73 |..``C'' |provides|
|00005830| 20 61 20 77 61 79 20 74 | 6f 20 73 61 76 65 20 61 | a way t|o save a|
|00005840| 6e 64 20 72 65 70 6c 61 | 63 65 20 74 68 65 20 63 |nd repla|ce the c|
|00005850| 75 72 72 65 6e 74 20 63 | 6f 6e 74 65 78 74 20 75 |urrent c|ontext u|
|00005860| 73 69 6e 67 20 74 68 65 | 0d 0a 24 73 65 74 6a 6d |sing the|..$setjm|
|00005870| 70 24 20 61 6e 64 20 24 | 6c 6f 6e 67 6a 6d 70 24 |p$ and $|longjmp$|
|00005880| 20 63 6f 6d 6d 61 6e 64 | 73 2e 20 20 42 79 20 75 | command|s. By u|
|00005890| 73 69 6e 67 20 74 68 65 | 20 74 77 6f 20 69 6e 20 |sing the| two in |
|000058a0| 63 6f 6e 6a 75 6e 63 74 | 69 6f 6e 20 28 73 65 65 |conjunct|ion (see|
|000058b0| 0d 0a 7b 5c 65 6d 20 6d | 70 74 68 64 2e 63 7d 29 |..{\em m|pthd.c})|
|000058c0| 2c 20 77 65 20 63 61 6e | 20 73 77 69 74 63 68 20 |, we can| switch |
|000058d0| 62 65 74 77 65 65 6e 20 | 74 68 72 65 61 64 73 20 |between |threads |
|000058e0| 71 75 69 63 6b 6c 79 20 | 61 6e 64 20 70 6f 72 74 |quickly |and port|
|000058f0| 61 62 6c 79 2e 0d 0a 48 | 6f 77 65 76 65 72 2c 20 |ably...H|owever, |
|00005900| 61 20 63 6f 6e 74 65 78 | 74 20 73 77 69 74 63 68 |a contex|t switch|
|00005910| 20 6d 75 73 74 20 62 65 | 20 6f 6e 65 20 6f 66 20 | must be| one of |
|00005920| 74 68 6f 73 65 20 61 75 | 74 6f 6e 6f 6d 6f 75 73 |those au|tonomous|
|00005930| 20 6f 70 65 72 61 74 69 | 6f 6e 73 3a 20 77 65 0d | operati|ons: we.|
|00005940| 0a 63 61 6e 6e 6f 74 20 | 62 65 20 69 6e 74 65 72 |.cannot |be inter|
|00005950| 72 75 70 74 65 64 20 72 | 69 67 68 74 20 61 66 74 |rupted r|ight aft|
|00005960| 65 72 20 77 65 20 68 61 | 76 65 20 73 61 76 65 64 |er we ha|ve saved|
|00005970| 20 74 68 65 20 6f 6c 64 | 20 63 6f 6e 74 65 78 74 | the old| context|
|00005980| 20 79 65 74 20 68 61 76 | 65 0d 0a 6e 6f 74 20 79 | yet hav|e..not y|
|00005990| 65 74 20 6a 75 6d 70 65 | 64 20 69 6e 74 6f 20 74 |et jumpe|d into t|
|000059a0| 68 65 20 6e 65 77 2e 20 | 20 54 68 65 72 65 66 6f |he new. | Therefo|
|000059b0| 72 65 2c 20 49 20 70 72 | 6f 74 65 63 74 20 74 68 |re, I pr|otect th|
|000059c0| 65 20 6f 70 65 72 61 74 | 69 6f 6e 20 69 6e 20 61 |e operat|ion in a|
|000059d0| 0d 0a 24 6d 70 73 65 6d | 5c 5f 63 72 69 74 73 65 |..$mpsem|\_critse|
|000059e0| 63 74 24 20 28 73 65 65 | 20 63 6f 64 65 29 20 74 |ct$ (see| code) t|
|000059f0| 6f 20 69 6e 73 75 72 65 | 20 74 68 61 74 20 69 6e |o insure| that in|
|00005a00| 74 65 72 72 75 70 74 73 | 20 61 72 65 20 64 69 73 |terrupts| are dis|
|00005a10| 61 62 6c 65 64 20 61 6e | 64 0d 0a 74 68 65 20 70 |abled an|d..the p|
|00005a20| 72 6f 63 65 73 73 6f 72 | 20 68 61 73 20 63 6f 6e |rocessor| has con|
|00005a30| 74 72 6f 6c 20 6f 66 20 | 61 20 6e 65 77 20 63 6f |trol of |a new co|
|00005a40| 6e 74 65 78 74 20 7b 5c | 65 6d 20 62 65 66 6f 72 |ntext {\|em befor|
|00005a50| 65 7d 20 72 65 6c 65 61 | 73 69 6e 67 20 69 74 73 |e} relea|sing its|
|00005a60| 20 6f 6c 64 0d 0a 6f 6e | 65 2e 20 20 59 6f 75 20 | old..on|e. You |
|00005a70| 77 69 6c 6c 20 6e 6f 74 | 69 63 65 20 74 68 65 20 |will not|ice the |
|00005a80| 63 61 6c 6c 20 63 68 65 | 63 6b 73 20 69 66 20 6e |call che|cks if n|
|00005a90| 65 77 20 63 6f 6e 74 65 | 78 74 20 74 6f 20 62 65 |ew conte|xt to be|
|00005aa0| 20 73 77 69 74 63 68 65 | 64 20 74 6f 20 69 73 0d | switche|d to is.|
|00005ab0| 0a 76 61 6c 69 64 20 28 | 64 65 74 65 63 74 69 6e |.valid (|detectin|
|00005ac0| 67 20 73 74 61 63 6b 20 | 6f 76 65 72 66 6c 6f 77 |g stack |overflow|
|00005ad0| 29 20 61 6e 64 20 69 66 | 20 69 74 20 69 73 20 61 |) and if| it is a|
|00005ae0| 6c 72 65 61 64 79 20 74 | 68 65 20 63 75 72 72 65 |lready t|he curre|
|00005af0| 6e 74 20 63 6f 6e 74 65 | 78 74 0d 0a 28 64 65 74 |nt conte|xt..(det|
|00005b00| 6f 75 72 69 6e 67 20 73 | 65 6c 66 20 7b 5c 62 66 |ouring s|elf {\bf|
|00005b10| 20 64 65 61 64 6c 6f 63 | 6b 7d 29 2e 20 20 42 75 | deadloc|k}). Bu|
|00005b20| 74 20 70 65 72 68 61 70 | 73 20 6c 65 61 73 74 20 |t perhap|s least |
|00005b30| 63 6f 6e 73 70 69 63 75 | 6f 75 73 20 69 73 0d 0a |conspicu|ous is..|
|00005b40| 24 6d 70 74 68 64 5c 5f | 73 77 69 74 63 68 24 20 |$mpthd\_|switch$ |
|00005b50| 72 65 74 75 72 6e 20 6f | 66 20 74 68 65 20 69 64 |return o|f the id|
|00005b60| 20 6f 66 20 74 68 65 20 | 70 72 65 76 69 6f 75 73 | of the |previous|
|00005b70| 20 74 68 72 65 61 64 3b | 20 69 74 20 7b 5c 65 6d | thread;| it {\em|
|00005b80| 20 6d 75 73 74 7d 20 62 | 65 0d 0a 72 65 74 75 72 | must} b|e..retur|
|00005b90| 6e 65 64 20 66 72 6f 6d | 20 61 20 73 77 69 74 63 |ned from| a switc|
|00005ba0| 68 20 69 66 20 69 74 20 | 69 73 20 65 76 65 72 20 |h if it |is ever |
|00005bb0| 74 6f 20 62 65 20 6f 62 | 74 61 69 6e 65 64 20 72 |to be ob|tained r|
|00005bc0| 65 6c 69 61 62 6c 79 2c | 20 73 6f 20 69 74 20 69 |eliably,| so it i|
|00005bd0| 73 0d 0a 63 6f 70 69 65 | 64 20 69 6e 74 6f 20 24 |s..copie|d into $|
|00005be0| 6f 6c 64 6d 70 74 68 64 | 24 20 73 6f 20 74 68 65 |oldmpthd|$ so the|
|00005bf0| 20 72 65 63 65 69 76 69 | 6e 67 20 74 68 72 65 61 | receivi|ng threa|
|00005c00| 64 20 63 61 6e 20 72 65 | 61 64 20 69 74 20 62 65 |d can re|ad it be|
|00005c10| 66 6f 72 65 20 69 74 0d | 0a 72 65 73 74 6f 72 65 |fore it.|.restore|
|00005c20| 73 20 69 74 73 20 69 6e | 74 65 72 72 75 70 74 20 |s its in|terrupt |
|00005c30| 73 74 61 74 75 73 2e 0d | 0a 0d 0a 54 68 75 73 20 |status..|...Thus |
|00005c40| 74 68 65 20 24 6d 70 74 | 68 64 5c 5f 73 77 69 74 |the $mpt|hd\_swit|
|00005c50| 63 68 24 20 69 73 6f 6c | 61 74 65 73 20 61 6e 64 |ch$ isol|ates and|
|00005c60| 20 65 6e 63 61 70 73 75 | 6c 61 74 65 73 20 74 68 | encapsu|lates th|
|00005c70| 65 20 63 6f 6d 70 6c 69 | 63 61 74 69 6f 6e 73 0d |e compli|cations.|
|00005c80| 0a 6f 66 20 63 6f 6e 74 | 65 78 74 20 73 77 69 74 |.of cont|ext swit|
|00005c90| 63 68 69 6e 67 20 69 6e | 74 6f 20 6f 6e 65 20 73 |ching in|to one s|
|00005ca0| 61 66 65 20 61 6e 64 20 | 63 6f 6e 76 65 6e 69 65 |afe and |convenie|
|00005cb0| 6e 74 20 70 72 6f 63 65 | 64 75 72 65 20 63 61 6c |nt proce|dure cal|
|00005cc0| 6c 2e 0d 0a 0d 0a 5c 73 | 75 62 73 65 63 74 69 6f |l.....\s|ubsectio|
|00005cd0| 6e 7b 43 72 65 61 74 69 | 6e 67 20 61 6e 64 20 44 |n{Creati|ng and D|
|00005ce0| 65 73 74 72 6f 79 69 6e | 67 20 43 6f 6e 74 65 78 |estroyin|g Contex|
|00005cf0| 74 73 3a 20 24 6d 70 74 | 68 64 5c 5f 69 6e 69 74 |ts: $mpt|hd\_init|
|00005d00| 24 20 61 6e 64 20 24 6d | 70 74 68 64 5c 5f 64 69 |$ and $m|pthd\_di|
|00005d10| 6e 69 74 24 7d 0d 0a 0d | 0a 41 6c 6c 20 6d 61 79 |nit$}...|.All may|
|00005d20| 20 73 6f 75 6e 64 20 66 | 69 6e 65 20 61 6e 64 20 | sound f|ine and |
|00005d30| 64 61 6e 64 79 2c 20 75 | 6e 74 69 6c 20 77 65 20 |dandy, u|ntil we |
|00005d40| 77 72 69 74 65 20 61 20 | 70 72 6f 67 72 61 6d 20 |write a |program |
|00005d50| 61 6e 64 20 77 61 6e 74 | 20 74 6f 20 73 77 69 74 |and want| to swit|
|00005d60| 63 68 0d 0a 74 6f 20 61 | 6e 6f 74 68 65 72 20 74 |ch..to a|nother t|
|00005d70| 68 72 65 61 64 2e 20 20 | 57 68 61 74 20 74 68 72 |hread. |What thr|
|00005d80| 65 61 64 3f 20 20 48 6f | 77 20 64 6f 20 74 68 72 |ead? Ho|w do thr|
|00005d90| 65 61 64 73 20 67 65 74 | 20 73 74 61 72 74 65 64 |eads get| started|
|00005da0| 3f 20 20 48 6f 77 20 64 | 6f 0d 0a 74 68 72 65 61 |? How d|o..threa|
|00005db0| 64 73 20 67 65 74 20 73 | 74 6f 70 70 65 64 3f 20 |ds get s|topped? |
|00005dc0| 20 57 65 20 6e 65 65 64 | 20 61 20 6d 65 61 6e 73 | We need| a means|
|00005dd0| 20 6f 66 20 67 65 74 74 | 69 6e 67 20 74 68 72 65 | of gett|ing thre|
|00005de0| 61 64 73 20 67 6f 69 6e | 67 2e 0d 0a 0d 0a 4c 75 |ads goin|g.....Lu|
|00005df0| 62 6f 6d 69 72 20 42 69 | 63 5c 66 6f 6f 74 6e 6f |bomir Bi|c\footno|
|00005e00| 74 65 7b 4c 2e 20 42 69 | 63 20 65 74 20 61 6c 2c |te{L. Bi|c et al,|
|00005e10| 20 5c 75 6e 64 65 72 6c | 69 6e 65 7b 54 68 65 20 | \underl|ine{The |
|00005e20| 4c 6f 67 69 63 61 6c 20 | 44 65 73 69 67 6e 20 6f |Logical |Design o|
|00005e30| 66 0d 0a 4f 70 65 72 61 | 74 69 6e 67 20 53 79 73 |f..Opera|ting Sys|
|00005e40| 74 65 6d 73 7d 2c 20 70 | 2e 20 32 31 2e 7d 20 64 |tems}, p|. 21.} d|
|00005e50| 65 73 63 72 69 62 65 73 | 20 6f 6e 65 20 66 75 6e |escribes| one fun|
|00005e60| 63 74 69 6f 6e 61 6c 6c | 79 20 6e 69 63 65 20 77 |ctionall|y nice w|
|00005e70| 61 79 20 74 6f 0d 0a 69 | 6e 69 74 69 61 74 65 20 |ay to..i|nitiate |
|00005e80| 7b 5c 65 6d 20 70 61 72 | 61 6c 6c 65 6c 20 70 72 |{\em par|allel pr|
|00005e90| 6f 63 65 73 73 65 73 7d | 20 75 73 69 6e 67 20 61 |ocesses}| using a|
|00005ea0| 20 24 63 6f 62 65 67 69 | 6e 20 53 5f 7b 30 7d 20 | $cobegi|n S_{0} |
|00005eb0| 7c 20 53 5f 7b 31 7d 20 | 7c 0d 0a 2e 2e 2e 20 7c || S_{1} ||..... ||
|00005ec0| 20 53 5f 7b 6e 7d 20 63 | 6f 65 6e 64 24 20 63 6f | S_{n} c|oend$ co|
|00005ed0| 6e 73 74 72 75 63 74 20 | 77 68 69 63 68 20 65 78 |nstruct |which ex|
|00005ee0| 65 63 75 74 65 73 20 24 | 6e 24 20 73 65 67 6d 65 |ecutes $|n$ segme|
|00005ef0| 6e 74 73 20 24 53 5f 7b | 69 7d 24 20 6f 66 0d 0a |nts $S_{|i}$ of..|
|00005f00| 73 65 71 75 65 6e 74 69 | 61 6c 20 63 6f 64 65 20 |sequenti|al code |
|00005f10| 69 6e 20 70 61 72 61 6c | 6c 65 6c 2e 20 20 48 6f |in paral|lel. Ho|
|00005f20| 77 65 76 65 72 2c 20 74 | 68 69 73 20 61 70 70 72 |wever, t|his appr|
|00005f30| 6f 61 63 68 20 6d 61 79 | 20 72 65 71 75 69 72 65 |oach may| require|
|00005f40| 20 61 0d 0a 6d 61 6a 6f | 72 20 6d 6f 64 69 66 69 | a..majo|r modifi|
|00005f50| 63 61 74 69 6f 6e 20 69 | 6e 20 74 68 65 20 73 79 |cation i|n the sy|
|00005f60| 6e 74 61 78 20 6f 66 20 | 60 60 43 27 27 20 6f 72 |ntax of |``C'' or|
|00005f70| 20 61 20 63 6f 6d 70 6c | 65 78 20 66 75 6e 63 74 | a compl|ex funct|
|00005f80| 69 6f 6e 20 63 61 6c 6c | 0d 0a 77 69 74 68 20 61 |ion call|..with a|
|00005f90| 20 76 61 72 69 61 62 6c | 65 20 6e 75 6d 62 65 72 | variabl|e number|
|00005fa0| 20 6f 66 20 61 72 67 75 | 6d 65 6e 74 73 2e 20 20 | of argu|ments. |
|00005fb0| 4d 6f 72 65 20 69 6d 70 | 6f 72 74 61 6e 74 6c 79 |More imp|ortantly|
|00005fc0| 2c 20 74 68 65 20 66 6f | 72 63 65 64 0d 0a 6e 65 |, the fo|rced..ne|
|00005fd0| 73 74 69 6e 67 20 6f 66 | 20 74 68 65 20 63 6f 6e |sting of| the con|
|00005fe0| 73 74 72 75 63 74 20 6c | 61 63 6b 73 20 74 68 65 |struct l|acks the|
|00005ff0| 20 65 78 70 72 65 73 73 | 69 76 65 20 70 6f 77 65 | express|ive powe|
|00006000| 72 20 6f 66 20 6f 74 68 | 65 72 20 61 70 70 72 6f |r of oth|er appro|
|00006010| 61 63 68 65 73 2e 2e 2e | 0d 0a 0d 0a 41 20 6d 6f |aches...|....A mo|
|00006020| 72 65 20 67 65 6e 65 72 | 61 6c 20 28 62 75 74 20 |re gener|al (but |
|00006030| 6d 6f 72 65 20 63 6f 6d | 70 6c 65 78 29 20 61 70 |more com|plex) ap|
|00006040| 70 72 6f 61 63 68 20 77 | 6f 75 6c 64 20 62 65 20 |proach w|ould be |
|00006050| 74 6f 20 75 73 65 20 24 | 66 6f 72 6b 24 2c 20 24 |to use $|fork$, $|
|00006060| 6a 6f 69 6e 24 2c 0d 0a | 61 6e 64 20 24 71 75 69 |join$,..|and $qui|
|00006070| 74 24 20 70 72 69 6d 69 | 74 69 76 65 73 2e 20 20 |t$ primi|tives. |
|00006080| 49 6e 20 74 68 69 73 20 | 69 6e 74 65 72 66 61 63 |In this |interfac|
|00006090| 65 2c 20 61 6e 20 65 78 | 65 63 75 74 69 6f 6e 20 |e, an ex|ecution |
|000060a0| 66 6c 6f 77 20 6d 61 79 | 20 62 65 0d 0a 24 66 6f |flow may| be..$fo|
|000060b0| 72 6b 24 65 64 20 61 74 | 20 61 20 70 6f 69 6e 74 |rk$ed at| a point|
|000060c0| 20 69 6e 20 74 68 65 20 | 63 6f 64 65 2c 20 6f 72 | in the |code, or|
|000060d0| 20 74 77 6f 20 6f 72 20 | 6d 6f 72 65 20 66 6c 6f | two or |more flo|
|000060e0| 77 73 20 6c 61 74 65 72 | 20 24 6a 6f 69 6e 24 65 |ws later| $join$e|
|000060f0| 64 3b 0d 0a 73 75 70 65 | 72 66 6c 75 6f 75 73 20 |d;..supe|rfluous |
|00006100| 66 6c 6f 77 73 20 63 61 | 6e 20 61 6c 73 6f 20 62 |flows ca|n also b|
|00006110| 65 20 24 71 75 69 74 24 | 20 28 74 65 72 6d 69 6e |e $quit$| (termin|
|00006120| 61 74 65 64 29 2e 20 20 | 55 4e 49 58 20 70 72 6f |ated). |UNIX pro|
|00006130| 76 69 64 65 73 20 61 0d | 0a 76 61 72 69 61 6e 74 |vides a.|.variant|
|00006140| 20 6f 6e 20 74 68 69 73 | 20 69 6e 74 65 72 66 61 | on this| interfa|
|00006150| 63 65 20 77 69 74 68 20 | 69 74 73 20 24 66 6f 72 |ce with |its $for|
|00006160| 6b 24 2c 20 24 77 61 69 | 74 24 2c 20 61 6e 64 20 |k$, $wai|t$, and |
|00006170| 24 65 78 69 74 24 20 70 | 72 69 6d 69 74 69 76 65 |$exit$ p|rimitive|
|00006180| 73 2e 0d 0a 54 68 65 20 | 55 4e 49 58 20 24 66 6f |s...The |UNIX $fo|
|00006190| 72 6b 24 20 63 61 6e 20 | 61 70 70 65 61 72 20 61 |rk$ can |appear a|
|000061a0| 6e 79 77 68 65 72 65 20 | 69 6e 20 61 20 62 6c 6f |nywhere |in a blo|
|000061b0| 63 6b 20 6f 66 20 63 6f | 64 65 2c 20 61 6e 64 20 |ck of co|de, and |
|000061c0| 61 66 74 65 72 20 74 68 | 65 0d 0a 24 66 6f 72 6b |after th|e..$fork|
|000061d0| 24 2c 20 62 6f 74 68 20 | 70 72 6f 63 65 73 73 65 |$, both |processe|
|000061e0| 73 20 68 61 76 65 20 61 | 63 63 65 73 73 20 74 6f |s have a|ccess to|
|000061f0| 20 61 6c 6c 20 6c 6f 63 | 61 6c 20 76 61 72 69 61 | all loc|al varia|
|00006200| 62 6c 65 73 20 63 75 72 | 72 65 6e 74 6c 79 0d 0a |bles cur|rently..|
|00006210| 64 65 66 69 6e 65 64 2e | 20 20 54 68 69 73 20 69 |defined.| This i|
|00006220| 73 20 64 6f 6e 65 20 62 | 79 20 6d 61 6b 69 6e 67 |s done b|y making|
|00006230| 20 61 6e 20 61 6c 6d 6f | 73 74 20 65 78 61 63 74 | an almo|st exact|
|00006240| 20 63 6f 70 79 20 6f 66 | 20 74 68 65 20 70 72 6f | copy of| the pro|
|00006250| 63 65 73 73 0d 0a 64 61 | 74 61 73 70 61 63 65 20 |cess..da|taspace |
|00006260| 28 62 6f 74 68 20 73 74 | 61 63 6b 20 61 6e 64 20 |(both st|ack and |
|00006270| 68 65 61 70 29 20 69 6e | 74 6f 20 61 6e 20 69 64 |heap) in|to an id|
|00006280| 65 6e 74 69 63 61 6c 20 | 62 75 74 20 73 65 70 61 |entical |but sepa|
|00006290| 72 61 74 65 20 61 64 64 | 72 65 73 73 0d 0a 73 70 |rate add|ress..sp|
|000062a0| 61 63 65 2e 20 20 54 68 | 69 73 20 74 65 63 68 6e |ace. Th|is techn|
|000062b0| 69 71 75 65 20 64 6f 65 | 73 20 61 6e 20 65 78 63 |ique doe|s an exc|
|000062c0| 65 6c 6c 65 6e 74 20 6a | 6f 62 20 61 74 20 70 72 |ellent j|ob at pr|
|000062d0| 6f 74 65 63 74 69 6e 67 | 20 70 72 6f 63 65 73 73 |otecting| process|
|000062e0| 65 73 0d 0a 66 72 6f 6d | 20 65 61 63 68 20 6f 74 |es..from| each ot|
|000062f0| 68 65 72 2c 20 62 75 74 | 20 69 74 20 6d 61 6b 65 |her, but| it make|
|00006300| 73 20 73 68 61 72 65 64 | 20 6d 65 6d 6f 72 79 20 |s shared| memory |
|00006310| 65 78 74 72 65 6d 65 6c | 79 20 64 69 66 66 69 63 |extremel|y diffic|
|00006320| 75 6c 74 2c 20 69 73 20 | 73 6c 6f 77 2c 0d 0a 61 |ult, is |slow,..a|
|00006330| 6e 64 20 72 65 71 75 69 | 72 65 73 20 61 20 6c 6f |nd requi|res a lo|
|00006340| 74 20 6f 66 20 73 70 65 | 63 69 61 6c 20 68 61 72 |t of spe|cial har|
|00006350| 64 77 61 72 65 20 28 7b | 5c 62 66 20 76 69 72 74 |dware ({|\bf virt|
|00006360| 75 61 6c 20 6d 65 6d 6f | 72 79 7d 20 73 75 70 70 |ual memo|ry} supp|
|00006370| 6f 72 74 29 20 6e 6f 74 | 0d 0a 75 73 75 61 6c 6c |ort) not|..usuall|
|00006380| 79 20 66 6f 75 6e 64 20 | 6f 6e 20 70 65 72 73 6f |y found |on perso|
|00006390| 6e 61 6c 20 63 6f 6d 70 | 75 74 65 72 73 2e 0d 0a |nal comp|uters...|
|000063a0| 0d 0a 46 6f 72 20 74 68 | 65 73 65 20 72 65 61 73 |..For th|ese reas|
|000063b0| 6f 6e 73 2c 20 49 20 70 | 72 6f 76 69 64 65 20 74 |ons, I p|rovide t|
|000063c0| 68 65 20 70 72 69 6d 69 | 74 69 76 65 20 24 6d 70 |he primi|tive $mp|
|000063d0| 74 68 64 5c 5f 69 6e 69 | 74 24 20 77 68 69 63 68 |thd\_ini|t$ which|
|000063e0| 20 73 69 6d 70 6c 79 0d | 0a 63 72 65 61 74 65 73 | simply.|.creates|
|000063f0| 20 61 20 74 68 72 65 61 | 64 20 64 61 74 61 20 6f | a threa|d data o|
+--------+-------------------------+-------------------------+--------+--------+
Only 25.0 KB of data is shown above.