Douglas W. Jones
Background A user-level thread package for UNIX might begin with a call to the following routine:
(void) thsetup(int n; (void) *p(); int s)
{
int i;
thheapinit();
thschedinit();
thcreate(p, s);
for (i = 1; i < n; i++) {
if (fork() != 0) { /* child */
thdispatch();
}
}
thdispatch();
}
This converts the parent process into n parallel processes all running
thdispatch9(), a routine that takes ready threads off the thread ready list
and runs them. Prior to this, this routine initializes the shared heap
(the only memory area shared by the n parallel processes) and initializes
the thread ready list before creating a single thread running the function p,
with a stack of size s. New threads are created by the following routine:
(void) thcreate((void) *p(); int s);This creates and schedules one thread in the thread ready list. This thread will execute the code in function p, using a stack of size s bytes to execute that code.
The following additional thread scheduling and synchronization operations are in our elementary UNIX-based user-level thread package:
(thsem *) thseminit(); -- create and return a pointer to a thread-semaphore -- in the shared heap (void) thwait(thesem s); -- wait on a thread-semaphore (void) thsignal(thesem s); -- signal on a thread-semaphore (void) thexit(thesem s); -- exit on a thread-semaphore (void *) thmalloc(n); -- allocate n bytes of shared heap (void *) thfree(void * p); -- deallocate a block of shared heap
... thsetup(4); -- 4-processes will execute my threads thseminit(s); thcreate(x); thcreate(y); thcreate(z); ... thwait(s) ...
Hint: There is an extremely serious problem with this proposed thread package that makes it entirely useless, as proposed here!
Hint: This is hard!