I've seen a few implementations of popen()/pclose(). They all used a static list of pids, and no locking:
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));
}
Or this, supposedly 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;
Is it what it looks like - a not thread safe implementation? Or am I missing something obvious?
解决方案
The GNU libc implementation is threadsafe if libc is configured to be reentrant (which it is likely to be). However, this may not be the case for other implementations of libc.