locking - popen - 锁还是不是线程安全的?

标签 locking thread-safety popen

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

相关文章:

数组列表上的 java volatile /同步

c++ - bash 脚本在使用 popen() 时不将返回码返回给调用 C++ 程序

ios - 自定义 iOS 锁定应用程序

postgresql - 使用 Postgres 和 GO 强制执行 "lock"

C# Interlocked 用作锁定机制?

multithreading - perl 模块 Perl-LDAP (Net::LDAP) 是线程安全的吗?

c++ - C++ 所需的具有 C 链接的回调函数

在c/c++中调用多个popen

python - subprocess.Popen.stdout - 实时读取标准输出(再次)

synchronization - 如果不同处理器中的两个进程试图同时获取锁会发生什么