[Tutorial] Processes -- Introduction and setpriority
Author |
Message |
Tony
|
Posted: Thu Mar 03, 2005 1:38 pm Post subject: [Tutorial] Processes -- Introduction and setpriority |
|
|
Processes Revised
HoltSoft wrote:
A process declaration is much like a procedure declaration, but is activated by a fork statement rather than by a call. The fork statement starts concurrent (parallel) execution of the process while the statements following the fork continue to execute.
Such allows us to execute multiple lines of code in parallel rather then sequentially one after one. An important thing to realize is that commands take turns to execute, and two processes will not run nicely next to each other. Note that it is individual commands that take turns, not the entire line statement as one might assume at first.
Turing: |
process foo
loop
put "foo", "foo", "foo"
end loop
end foo
process bar
loop
put "bar", "bar", "bar"
end loop
end bar
fork foo
fork bar
|
What kind of output would you expect?
Not quite what we wanted, is it?
Reducing the number of commands per line decreases the chance of corruption, but a) that tells us to stop using chain functions, and b) glitches will still occur.
Is two statement. First outputs string "foo", second outputs the newline character. Try it out.
Turing: |
process foo
loop
put "foo"
end loop
end foo
process bar
loop
put "bar"
end loop
end bar
fork foo
fork bar
|
delay (1) after each put fixes the late \n glitch, but a) it slows down the execution time as delays accumulate, and b) this simply stops working as we go back to our put "foo", "foo", "foo" example. Glitches reemerge even at delay(100) as processes simply have a lot of picks to make. Delay becomes just another command in the mix.
Although processes come across looking quite badly at this point, it is only because they are unmanaged.
setpriority ( p : nat )
HoltSoft wrote:
The setpriority procedure is used to set the priority of a process in a concurrent program. This priority cannot be counted on to guarantee critical access to shared variables. A smaller value of p means increased speed. The argument to setpriority may be limited to the range 0 to 2**15 - 1.
That is not an entirely accurate description. Smaller value is not an increase in speed, it is an order the process takes in the query.
By default everything is set to a priority of 1000. You can find out the priority of any given function using getpriority (returning function's nat priority value). When processes start piling up in a query, by default one to chosen at random. You would need to tell the processes the order you want them to take. setpriority could (and should) be used as triggers for executing processes. When a block of code needs to be executed together (such as our "foo", "foo", "foo" output without breaking up), it should be wrapped around in a priority shift.
Turing: |
process foo
loop
setpriority (999) %increase priority
put "foo", "foo", "foo"
setpriority (1000) %return to normal
end loop
end foo
process bar
loop
put "bar"
end loop
end bar
fork bar
fork foo
|
Since put "foo", "foo", "foo" was given a higher priority (smaller number == higher priority) than put "bar"'s default 1000, the former was executed in full before it's priority was set back to 1000 (equivalent to that of bar) and other processes were given a chance to execute. Such technique is similar to Visual Basic's DoEvents functionality.
Description: |
|
Filesize: |
21.96 KB |
Viewed: |
4365 Time(s) |
|
Description: |
|
Filesize: |
9.99 KB |
Viewed: |
4359 Time(s) |
|
Description: |
|
Filesize: |
15.51 KB |
Viewed: |
4358 Time(s) |
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
jamonathin
|
Posted: Thu Mar 03, 2005 2:37 pm Post subject: (No subject) |
|
|
ver noice, very noice
|
|
|
|
|
|
Cervantes
|
Posted: Thu Mar 03, 2005 5:55 pm Post subject: (No subject) |
|
|
Uh oh, I'm not getting the nice output.
I am still getting things like barfoofoofoo and black lines.
Description: |
|
Filesize: |
14.78 KB |
Viewed: |
4368 Time(s) |
|
|
|
|
|
|
|
Martin
|
Posted: Fri Mar 04, 2005 8:49 am Post subject: (No subject) |
|
|
So it's uhh...just as random with different odds?
Processes still suck.
|
|
|
|
|
|
|
|