我见过一些popen()/pclose() 的实现。他们都使用了一个静态的pid列表,并且没有锁定:
static int *pids;
static int fds;
if (!pids) {
if ((fds = getdtablesize()) <= 0)
return (NULL);
if ((pids = malloc(fds * sizeof(int))) == NULL)
return (NULL);
memset(pids, 0, fds * sizeof(int));
}
或者这个,据说是 NetBSD:
static struct pid {
struct pid *next;
FILE *fp;
pid_t pid;
} *pidlist;
/* Link into list of file descriptors. */
cur->fp = iop;
cur->pid = pid;
cur->next = pidlist;
pidlist = cur;
它看起来像 - 不是线程安全的实现吗?还是我错过了一些明显的东西?