2

我见过一些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;

它看起来像 - 不是线程安全的实现吗?还是我错过了一些明显的东西?

4

2 回答 2

3

如果 libc 配置为可重入(很可能是),则 GNU libc 实现是线程安全的但是,对于 libc 的其他实现,情况可能并非如此。

于 2009-11-09T17:43:29.380 回答
3

我不认为你遗漏了一些明显的东西。我没有看到任何对 popen() 线程安全的引用,无论是否要求它是线程安全的。因此,您可能应该将其视为非线程安全的。

于 2009-11-09T17:35:43.203 回答