The Department of Computer Science & Engineering
cse@buffalo

CSE 305
Programming Languages
Lecture Notes
Stuart C. Shapiro
Fall, 2003


Concurrency

Example: Eager-beaver and
Goal: Evaluate a collection of logical expressions. If they all evaluate to true, return true. If any of the expressions evaluates to false, return false.

Idea: Evaluate all the expressions concurrently, each in its own process. Give each of them access to a global boolean flag, initialized to true. When a process terminates, if its result was false, it sets flag to false. As soon as a process notices that flag is false, it terminates.

A main process is needed to spawn the subprocesses, give each of them an argument expression to work on, and to return the final answer to the caller. The main process should return false as soon as it notices that flag is false.

Should the main process terminate the subprocesses when it is ready to return false? Not if each subprocess terminates itself when flag is false.

How could the main process ever return true? It must know that all the subprocesses have terminated, even though flag is still true. It can do this with a global childProcessCount, initialized to the number of subprocesses. When a subprocess terminates, it should decrement the childProcessCount. Then, when the main process notices that childProcessCount is zero, it can terminate and return true. There are then no subprocesses left to terminate.

The global variable childProcessCount is shared among the processes, and must be protected, so we don't get a sequence such as: access1 decrement1 access2 decrement2 store2 store1.

The global variable flag is acted on by all the processes, but needn't be protected because what's stored into flag does not depend on a previous value.

ACL provides mp:process-run-function:

(mp:process-run-function
    (list :name <a name for this process>
          :resume-hook <a 0-argument function that's run whenever the process resumes>)
    <a function the process runs>
    <a sequence of arguments for the function>)
mp:make-process-lock
(mp:make-process-lock &key name)
mp:with-process-lock:
(mp:with-process-lock (<lock>)
  <body>)
mp:process-kill
(mp:process-kill <process>)
mp:*current-process*

and mp:process-wait

(mp:process-wait <string> <function> &rest <arguments>)
"This function suspends the current process (the value of *current-process*) until applying function to arguments yields true." [Allegro CL version 6.2 documentation]

See eager-beaver and program.

First Previous Next

Copyright © 2003 by Stuart C. Shapiro. All rights reserved.

Stuart C. Shapiro <shapiro@cse.buffalo.edu>