next up previous contents
Next: 10.3 Destroying Processes Up: 10 Multi-Tasking Previous: 10.1 Overview

10.2 Creating New Processes

The function to create a new process is start_process.

start_process takes one mandatory argument---the function call to be started as a process. There are two optional arguments: the process's number of ticks and stack size. (If only one optional argument is given, it is assumed to be the ticks number, and the default stack size is used.)

start_process has the following syntax:

int start_process( function-call( ... ) , [ TICKS ] , [ STACK-SIZE ] )

start_process returns an integer, which is the process ID assigned to the new process.

The function call may be any valid call of the function used. The following code shows the function main creating a process:

    void check_sensor(int n)
      while (1)
        printf("Sensor %d is %d\n", n, digital(n));

    void main()

Normally when a C functions ends, it exits with a return value or the ``void'' value. If a function invoked as a process ends, it ``dies,'' letting its return value (if there was one) disappear. (This is okay, because processes communicate results by storing them in globals, not by returning them as return values.) Hence in the above example, the

check_sensor function is defined as an infinite loop, so as to run forever (until the board is reset or a kill_process is executed).

Creating a process with a non-default number of ticks or a non-default stack size is simply a matter of using start_process with optional arguments; e.g.

    start_process(check_sensor(2), 1, 50);

will create a check_sensor process that runs for 1 milliseconds per invocation and has a stack size of 50 bytes (for the given definition of check_sensor, a small stack space would be sufficient).

Fred G. Martin
Fri Mar 29 17:44:15 EST 1996