[Date Prev] [Date Next] [Thread Prev] [Thread Next] Date Index Thread Index Search archive:
Date:Wed, 24 Mar 2004 07:01:35 +0000 (UTC) 
Subject:Re: threads and shared memory space in pop11 
From:A . Sloman 
Volume-ID: 

Dave

> I'm (still) trying to get a threaded program working under pop11, so far
> I have managed to get the threads working and it all seemed to be fine,
> until I ran into the issue of shared memory with lists.

If you really want to share memory the simplest thing is to use
'lightweight' processes, created using consproc, etc.

You'll then need to write your own scheduler, as explained previously.

> The simple program below illustrates my problem:
>
> global vars list = [a b];
>
> define some();
>      if sys_vfork(false) then
>          "some" -> list(1);
>      endif;
> enddefine;

The documentation on sys_vfork (in REF sysutil) says:
        sys_vfork is  therefore used  ONLY in  the situation  where  the
        purpose of forking is to immediately sysexecute another image in
        the child, possibly after redirecting the standard input  and/or
        output etc (and  is quicker than  sys_fork because Unix  doesn't
        have to copy the  whole Poplog process). Using  it in any  other
        way will crash the system  -- in particular, the procedure  that
        calls sys_vfork must not exit when running as the child (this is
        the same limitation placed on the  use of the vfork system  call
        in C).

So you cannot use it to do what you want. If you use sys_fork and
create a new process which is a copy of the old one you can not, as
far as I know make them share a memory location (unless perhaps
you can use something like mmap). I don't know if you can use syspipe
to create a pipe that can be read and written to at both ends.


sys_fork
>
> some();
> syssleep(50);
>
> list =>
>
> when run you get:
>
> [telly|~]$ pop11 test.p
> ** [some b]
> ** [a b]
> [telly|~]$
>
> My problem is that the main thread should then have access to the list
> [some b], but its only able to access [a b], which shouldn't exist at
> all (in that form) by now.
>
> (Notice also how the dead thread printed stuff it wasn't meant to, and
> the main thread didn't print anything until afterwards).
>
> I tried sys_vfork() too, the result was the same and there were grim
> warnings about the program crashing iun the help file, so I stuck with
> sys_fork().
>
> The rogram consists of a service loop and a read loop (read loop gets
> data from a socket and adds it to a queue, service loop processes data
> asynchronously) and it is very important that the two threads have
> access to the same queue.

you could probably use two pipes, one for sending information in one
direction (e,g, requests) and the other in the other direction.

Aaron