本文介绍了popen-锁定还是线程安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了popen()/pclose()的一些实现.他们都使用了静态的pid列表,并且没有锁定:

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));
    }

或者说是NetBSD:

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?

推荐答案

GNU libc实现是线程安全的.但是,对于libc的其他实现,情况可能并非如此.

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.

这篇关于popen-锁定还是线程安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 02:03