Home > Articles > Programming > General Programming/Other Languages

  • Print
  • + Share This
This chapter is from the book

In the last chapter we saw that we can declare and instantiate processes by prefixing a proctype declaration with the keyword active.

There are several ways to instantiate processes in PROMELA.

We can create multiple instantiations of a given proctype by adding the desired number in square brackets to the active prefix, for instance as follows:

active [2] proctype you_run()
        printf("my pid is: %d\n", _pid)

Each running process has a unique process instantiation number. These instantiation numbers are always non-negative, and are assigned in order of creation, starting at zero for the first created process. Each process can refer to its own instantiation number via the predefined local variable _pid. Simulating the example above, for instance, produces the following output:

$ spin you_run.pml
my pid is: 0
        my pid is: 1
2 processes created

The two processes that are instantiated here each print the value of their process instantiation number and then terminate. The two lines of output happen to come out in numeric order here, but since process execution is asynchronous, it could just as well have been the opposite. By default, during simulation runs, SPIN arranges for the output of each active process to appear in a different column: the pid number is used to set the number of tab stops used to indent each new line of output that is produced by a process. 1

There is also another way to instantiate new PROMELA processes. Any running process can start other processes by using a predefined operator called run. For instance, we could rewrite the last example as follows:

proctype you_run(byte x)
        printf("x = %d, pid = %d\n", x, _pid)

init {
        run you_run(0);
        run you_run(1)

A disadvantage of this solution is that it often creates one process more than strictly necessary (i.e., the init process). For simulation or implementation, the extra process would not matter too much, but in system verification we usually take every possible precaution to keep the system descriptions at a minimum: avoiding all unnecessary elements.

A simulation run of the last model produces the following result:

$ spin you_run2.pml
                x = 1, pid = 2
        x = 0, pid = 1
3 processes created

In this version of the proctype you_run, we added a parameter of type byte. This formal parameter is initialized in the run statement, which appears here in the init process. This means that when the “execution” of a run statement results in the creation of a new process, all formal parameters from the target proctype declaration are initialized to the values of the corresponding actual parameters that are provided in the run statement (i.e., parameter passing is by value).

Parameter values, of course, cannot be passed to the init process, or to processes that are instantiated as active proctypes. If processes created through the use of an active prefix have formal parameters, they are treated as if they were local variables, and they are initialized to zero. This initialization rule matches the rule for all data objects in PROMELA: if no explicit initialization is present, an object is always initialized to zero.

A newly created process may, but need not, start executing immediately after it is instantiated. Similarly, the new process may, but need not and generally will not, terminate before the process that created it moves on to its next statement. That is: processes do not behave like functions. Each process, no matter how it is created, defines an asynchronous thread of execution that can interleave its statement executions in arbitrary ways with other processes.

We mentioned in passing that run is really an operator, and therefore technically what so far we have casually referred to as a run “statement” is really an expression. Technically again, the expression is not “executed” but evaluated. The run expression is the only type of expression that can have a side effect when it evaluates to non-zero, but not when it evaluates to zero (i.e., when it fails to instantiate a new process). A run expression is also special in the sense that it can contain only one run operator and cannot be combined with any other conditionals.

The value of a run expression evaluates to zero if no process can be instantiated, otherwise it evaluates to a non-zero value which equals the process instantiation number of the newly created process. Note that the pid returned upon successful process instantiation can never itself be zero, because there must be at least one process running to evaluate the expression. Evaluating a run expression, then, produces a value of type pid (cf. p. 16, 36).

Because run is an operator, we can also change the definition of init in the last example into the following version, where the process instantiation numbers are stored in local variables.

init { pid p0, p1;

       p0 = run you_run(0);
       p1 = run you_run(1);
       printf("pids: %d and %d\n", p0, p1)

Simulating the execution of this model produces:

$ spin you_run2.pml
        x = 1, pid = 2
pids: 1 and 2
                x = 0, pid = 1
3 processes created

Note that the output from the three processes can again appear in any order because of the concurrent nature of the executions.

Finiteness: Why would evaluating a run expression ever fail to instantiate a new process, and return zero? The reason lies in the fact that a PROMELA model can only define finite systems. Enforcing that restriction helps to guarantee that any correctness property that can be stated in PROMELA is decidable. It is impossible to define a PROMELA model for which the total number of reachable system states can grow to infinity. Data objects can only have a finite range of possible values; there can be only finitely many active processes, finitely many message channels, and every such channel can have only finite capacity. The language does not prescribe a precise bound for all these quantities, other than that there is such a bound and that it is finite. For all currently existing versions of SPIN, the bound on the number of active processes and the bound on the number of message channels is put at 255.

An attempt to ignore these bounds will necessarily fail. For instance, we could try to define the following model:

active proctype splurge(int n)
{       pid p;
        printf("%d\n", n);
        p = run splurge(n+1)

Simulating the execution of this model with SPIN, using the -T option to disable the default indentation of printf output, produces the following result:

$ spin -T splurge.pml

spin: too many processes (255 max)
255 processes created

The creation of the 256th process fails (note that the process numbering start at zero) and ends the simulation run. But there are more interesting things to discover here, not just about how processes are instantiated, but also about how they can terminate and die. Process termination and process death are two distinct events in PROMELA.

  • A process “terminates” when it reaches the end of its code, that is, the closing curly brace at the end of the proctype body from which it was instantiated.

  • A process can only “die” and be removed as an active process if all processes that were instantiated later than this process have died first.

Processes can terminate in any order, but they can only die in the reverse order of their creation. When a process reaches the end of its code this only signifies process termination, but not process death. When a process has terminated, this means that it can no longer execute statements, but will still be counted as an active process in the system. Specifically, the process pid number remains associated with this process and cannot be reused for a new process. When a process dies, it is removed from the system and its pid can be reused for another process.

This means that each instantiation of the proctype splurge in the last example terminates immediately after it creates the next process, but none of these processes can die until the process creation fails for the first time on the 255th attempt. That last process is the first process that can die and be removed from the system, since it is the most recently created process in the system. Once this happens, its immediate predecessor can die, followed by its predecessor, and all the way back to the first created process in stack order, until the number of active processes drops to zero, and the simulation ends.

  • + Share This
  • 🔖 Save To Your Account