Paul McGlynn <pmcglynn@broadcom.com> wrote:
We’re using QNX 4.24. We have a single code set from which we want to
create several processes (or threads?). Currently, we use tfork() to
create new processes.
My current understanding is as follows: The QNX scheduler deals with
processes. If a process forks off a child process, the child uses
different code, data, and stack spaces than the parent. They could
share an explicitly-constructed shared memory, though.
Not quite. QNX does code-sharing as well, so a forked child, or even
another copy of a program started with spawn, will share code space
with the all other copies.
The sin utility reports code space this way – try doing a “sin” and
looking at the code size for the shell, then run a couple more shells,
and run “sin” again – the code size will have shrunk, as sin attributes
the code size to all the processes that share it evenly.
If a process starts a new thread, the child thread uses different code
and stack spaces than the parent, but they both use the same data
space. So they can both directly access whatever static variables.
Essentially correct – but note that while they do use different
stacks, the stack space of the “child” thread is still in the
data space of the parent thread (it is, essentially, malloced),
and subject to any corruption issues this might engender.
The Process Manager does not distinguish between a thread and a
process; each thread/process has an ID and each can be active,
blocked, etc.
The Process Manager only knows about processes – threads under
QNX4 are, effectively, processes that share the same data segment.
I think my questions boil down to the following:
- What is the difference between a thread & a process?
A thread is a process that shares its DS (data segment) with
one or more other processes.
- What is the difference between tfork() and begin_thread()?
begin_thread() calls tfork(), but it also sets up some extra stuff
that makes more pieces of the Watcom portion of the library safe to
use in a threaded environment. (eg, if you use begin_thread(),
malloc() is thread safe, but not if you use tfork().)
- If a library function (such as malloc()) is not “thread-safe”, that
only applies to threads, not processes, right?
Either this question is obvious, or I’m missing something. Not thread-safe
means not safe to use in a multi-thread program – that is, in a program
where more than one process(thread) are sharing the same DS.
There is, also, a pthreads library available from /usr/free that will
allow fd sharing between threads, gives (I think) more synchronisation
options, and a more complete thread environment for QNX4 – if you must
do thread work, I would strongly recommend getting and using that library.
ftp.qnx.com:/usr/free/qnx4/os/libs/pthreads-1.0b4.tgz
But, in general, if you can design without using threads under QNX4,
you are generally better with a cooperating processes model, possibly
using explicit shared memory, rather than a thread model.
-David