Newsgroups: comp.sys.transputer
From: aba@exe.dcs.exeter.ac.uk (Adam Back)
Subject: Re: Process id's with the INMOS C Toolset ?
Organization: Department of Computer Science, University of Exeter, U.K.
Date: Fri, 2 Dec 1994 12:30:47 GMT
Message-ID: <D06o9n.ts@exeter.ac.uk>

Adam Back <aba@exe.dcs.exeter.ac.uk> wrote:
> >My solution relies on allocating aligned process workspaces.  Each
> >process can look at the value of its workspace (stack) pointer and as
> >the workspaces (stack space) now start on an aligned region of memory,
> >you can work out the start of the workspace region with:
> >
> >	start = Wptr - Wptr % WORKSPACE_ALIGNMENT
> >
> 
> Interesting. The solution got me a little further in understanding the
> problem - is the idea to have a unique process id for each sub-process ?
> 
> In that case the Wptr is a good candidate.
> 
> Or is the idea to be able to positively identify which of a number of
> calling processes invoked you ? Then you might be able to use the Wptr
> of the calling process. Somewhat harder to determine.
> 
> Is your % WORKSPACE_ALIGNMENT just an easier way to get the latter ?
> 
> If not, what is WORKSPACE_ALIGNMENT for ?
> 
> I think I just do not fully understand the problem.

The problem with using Wptr is that its value changes with function
invocation depth (it doubles as a stack pointer).  So if you do:

  void func1( Process* p )
  {
    int wptr;
    __asm { ldlp 0; st wptr; }
    printf( "wptr in func1 = 0x%08x\n", wptr );
    func2();
  }

  void func2()
  {
    int wptr;
    __asm { ldlp 0; st wptr; }
    printf( "wptr in main = 0x%08x\n", wptr );
    func1();
  }

  int main()
  {
    Process* p1 = ProcAlloc( func1, 1024, 0 );
    ProcRun( p1 );
    ...
  }

you get different values in func1 and func2.

It would be possible to keep a record of the workspace start and the
workspace size of processes created and search through them to find
the one the wptr falls in.  However this is inefficient as you need a
search to do your lookup.  (This may be ok for some applications, but
if you want to frequently access information other than the process id
for each process then it is too slow.)

So the solution I used was to allocate workspace regions so that a
workspace always starts at a particular alignment.  Say we align
workspaces at 1k boundaries.  Then we can get the start of the
workspace from the workspace ptr by:

  {
    int wptr;
    __asm { ldlp0; st wptr; }
    wptr &= 0xfffffc00;
  }

Process workspaces grow down from the workspace top, so we can
allocate useful per process information at the workspace base.  So if
we store a process id at the workspace base, we can access it quickly
with the above code.

The process workspace looks like:

      1024
 byte aligned  stack       WPtr                     next 1024 byte
     region    base      (stack ptr)  stack top     aligned region
        V       V            V           V                V

        |                                |                |
      --|-------+------------+-----------+-             --|--
    ... | info  |         <- | stack     |  ...           |   ...
      --|-------+------------+-----------+-             --|--


The process id is stored in the info field before the stack base.

It would be easy to add the the parent process id to the info field in
the ProcAlloc and ProcInit functions although I have not done this.
This would allow you to implement a ParentId() function.

I have an implementation of replacement functions for ProcAlloc,
ProcInit, ProcAllocClean and ProcInitClean using this method which can
be accessed by ftp:

ftp://dcs.exeter.ac.uk/pub/parallel/software/procids.tar.gz

Adam
--
_______________________________________________________________
email:aba@dcs.exeter.ac.uk        http://dcs.exeter.ac.uk/~aba/


