我见过一些 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;
这就是它看起来的样子 - 非线程安全的实现吗?或者我错过了一些明显的东西?
最佳答案
GNU libc implementation如果 libc 配置为可重入(很可能是),则它是线程安全的。但是,对于 libc 的其他实现来说,情况可能并非如此。
关于locking - popen - 锁还是不是线程安全的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1702442/