next up previous
Next: Concurrent Execution of Up: No Title Previous: Machine and Run-Time

XINU Processes and Concurrency

Consider the following program consisting of three procedures:

#include <conf.h>

main ()
{
    int prA(), prB();

    resume( create(prA, 200, 20, "proc 1", 0) );  
    resume( create(prB, 200, 20, "proc 2", 0) );
}

prA()
{
    while (1)
        putc(CONSOLE, 'A');
}

prB()
{
    while (1)
        putc(CONSOLE, `B');
}

The statement `#include <conf.h>' inserts a file of XINU declarations in the source program. These declarations include, among other things, a definition of the constant CONSOLE, and the procedures `resume', `create', and `putc' used by the program.

Consider what happens when this program is executed. A process is created, which starts executing the main procedure. This procedure in turn executes the two resume-create statements. Each of these spawns a new process.

The statement `create(prA....)' asks XINU to create a new process that executes the procedure `prA' (just as the initial process executes the procedure `main'). The other arguments to create specify such things as the stack size needed ( recall that each process is associated with a separate stack), a scheduling priority (the OS uses this to allocate processor time to the process), process name (`proc1'), the count of arguments to the process (which are the arguments to the procedure `prA' and are zero in number). Create sets up the process, leaving it ready to run, but temporarily suspended. It returns the process id of the new process, which is an integer that identifies the created process so that it may be referenced later. In the example, the main procedure passes this process id to `resume', which starts (unsuspends) the process so it begins executing. The process executes the procedure `prA', which uses the XINU facility `putc' to write the character `A' on the screen.

Similarly, the statement `resume(create (prB...))' creates a process that executes the procedure `prB', which prints `Bs' on the screen.

It is important to distinguish between a procedure call and calls to `create' and `resume:

A procedure call does not return until the called procedure completes. Create and resume return to the caller after starting the process, allowing execution of both the calling procedure and the named procedure to proceed concurrently, as discussed in the following section.

Thus in the example, the main program terminates its computation after executing the create and resume procedure (when it reaches the end of the main procedure), while the two processes remain churning out `As' and `Bs' forever.




next up previous
Next: Concurrent Execution of Up: No Title Previous: Machine and Run-Time



Prasun Dewan
Tue Jan 13 12:23:19 EST 2004