On Solaris, the child of fork does not have access to the stacks
of other threads that existed at the time of the fork, as demonstrated
by the C program below (fails on Solaris, works everywhere else).
Work around this by making a copy of everything the child needs
in malloc'ed memory, which is inherited properly.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
int *p;
pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t ready = PTHREAD_COND_INITIALIZER;
pthread_cond_t never = PTHREAD_COND_INITIALIZER;
void*
thread1(void *v)
{
int local = 42;
pthread_mutex_lock(&mu);
p = &local;
pthread_cond_signal(&ready);
pthread_cond_wait(&never, &mu);
return 0;
}
void*
thread2(void *v)
{
pthread_mutex_lock(&mu);
while(p == 0)
pthread_cond_wait(&ready, &mu);
pthread_mutex_unlock(&mu);
printf("parent: *p = %d\n", *p);
int pid = fork();
if (pid == 0) {
printf("child: *p = %d\n", *p);
exit(0);
}
printf("parent: pid = %d\n", pid);
int status;
waitpid(pid, &status, 0);
if(WIFEXITED(status))
printf("child exit %d\n", WEXITSTATUS(status));
else if(WIFSIGNALED(status))
printf("child signal %d\n", WTERMSIG(status));
else
printf("unexpected child status %d\n", status);
return 0;
}
int
main(void)
{
pthread_t t1, t2;
pthread_create(&t1, 0, thread1, 0);
pthread_create(&t2, 0, thread2, 0);
void *v;
pthread_join(t2, &v);
return 0;
}
This is a port of many Plan 9 libraries and programs to Unix.
Installation
To install, run ./INSTALL. It builds mk and then uses mk to run the rest of the installation.
For more details, see install(1), at install.txt in this directory and at https://9fans.github.io/plan9port/man/man1/install.html.
Documentation
See https://9fans.github.io/plan9port/man/ for more documentation. (Documentation is also in this tree, but you need to run a successful install first. After that, "9 man 1 intro".)
Intro(1) contains a list of man pages that describe new features or differences from Plan 9.
Helping out
If you'd like to help out, great!
If you port this code to other architectures, please share your changes so others can benefit.
Git
You can use Git to keep your local copy up-to-date as we make changes and fix bugs. See the git(1) man page here ("9 man git") for details on using Git.
Status
Contact
-
Mailing list: https://groups.google.com/group/plan9port-dev
-
Issue tracker: https://github.com/9fans/plan9port/issues
-
Submitting changes: https://github.com/9fans/plan9port/pulls
-
Russ Cox rsc@swtch.com