Nut Operating System Demo

Two concurrent processes demo

Run the demo here

The demonstration shows Nos running two processes (2000 and 2011). The nos1.txt  is the operating system code.  The processor simulator plus the support for Nos is  ems.js . You need the latest Rz compiler and Assembler (rz36-2.zip) to compile and assemble the source code.

One process prints 10, 12, 14...300. Another process prints 3, 5, 7...300. The timeout is set at 300 instructions for interrupt to occur.  The application program is this:

count1()
  i = 10
  while(i < 300)
    print(i," ")
    i = i + 2

There are a lot of jumble output on the screen. The output from count1( ) is underlined. Here is how to understand them!

load 468 instructions
10 12 14 16 18 20 22 24 26 28 30 32 #2 34 36 38 40 42 44 46 48 50 52 54 56 58 60 #1 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 #0 * s a v e 2000 r e s 2011 3 5 #2 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 #1 37 39 41 43 45 47 49 51 53 55 57 59 61 63 #0 * s a v e 2011 r e s 2000 92 94 96 #2 98 100 102 104 106 108 110 112 114 116 118 120 122 124 #1 126 128 130 132 134 136 138 140 142 144 146 148 150 152 #0 * s a v e 2000 r e s 2011 65 67 69 #2 71 73 75 77 79 81 83 85 87 89 91 93 95 97 #1 99 101 103 105 107 109 111 113 115 117 119 121 123 125 127 #0 * s a v e 2011 r e s 2000 154 156 158 #2 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 #1 190 192 194 196 198 200 202 204 206 208 210 212 214 216 #0 * s a v e 2000 r e s 2011 129 131 133 #2 135 137 139 141 143 145 147 149 151 153 155 157 159 161 #1 163 165 167 169 171 173 175 177 179 181 183 185 187 189 #0 * s a v e 2011 r e s 2000 218 220 222 #2 224 226 228 230 232 234 236 238 240 242 244 246 248 250 #1 252 254 256 258 260 262 264 266 268 270 272 274 276 278 280 #0 * s a v e 2000 r e s 2011 191 193 195 #2 197 199 201 203 205 207 209 211 213 215 217 219 221 223 225 #1 227 229 231 233 235 237 239 241 243 245 247 249 251 253 #0 * s a v e 2011 r e s 2000 282 284 286 #2 288 290 292 294 296 298 stop * s a v e 2000 d e l #2 r e s 2011 #1 255 257 259 261 263 265 267 269 271 273 275 277 279 281 #0 * s a v e 2011 r e s 2011 283 285 287 #2 289 291 293 295 297 299 stop * s a v e 2011 d e l exit
execute 3552 instructions 18857 cycles
interrupt 11


* indicate the interrupt occurred
#n indicate the counter down timer (set at 3, each count 100 instructions)
save xxx  indicate the save computation state of the process xxx.
res yyy indicate the restore computation state of the process yyy.
del  indicate the deletetion of the process from the queue because it is stopped.
exit  indicate the simulation stops because there is no task in the queue.

Semaphore Demo

Run the demo2 here

The second demonstration ( nos2.txt ) shows two processes share a common variable.  These processes are synchronised, one is the "writer" incrementing the variable, the other is the "reader" reading and printing the value of the variable. They are in "lock", taking turn to access the common variable.  Here is the code:
empty, full, mutex   // semaphores
shareVar

writer()
  i = 0
  while( i < 3 )
    wait(empty)    ***1  
    wait(mutex)
    shareVar = shareVar + 1
    signal(mutex)
    signal(full)
    i = i + 1

reader()
  i = 0
  while( i < 3 )
    wait(full)
    wait(mutex)
    print("+",shareVar)
    signal(mutex)
    signal(empty)   ***2
    i = i + 1

The output on the screen:

load 837 instructions
#9 #8 #7 #6 block * s a v e 2106 #9 r e s 2117 #8 #7 + 1 #6 #5 #4 wake * s a v e 2117 r e s 2106 #9 #8 #7 #6 block * s a v e 2106 #9 r e s 2117 #8 #7 + 2 #6 #5 #4 wake * s a v e 2117 r e s 2106 #9 #8 #7 stop * s a v e 2106 #9 r e s 2117 #8 #7 + 3 #6 #5 stop * s a v e 2117 exit
execute 3899 instructions 21519 cycles
interrupt 6


The writer starts the first round and increments shareVar. It goes through "empty" and enters "mutex", access the variable then signals "full".   In the second round, it waits for "empty" (from reader) at the code line marked *** 1. That is where the first "block" occurs (the writer is in the waiting list of the semaphore "empty"). The scheduler switches to the next process (reader).  The reader accesses the variable, prints its value (+ 1) and signals the semaphore "empty" at the code line marked *** 2.  This action wake-ups the writer who is in the waiting list of the semaphore "empty".  And the process is switched back to the writer and continue.

last update 27 Jan 2013