popen - locks or not thread safe?

寵の児 提交于 2020-01-15 06:30:34

问题


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?


回答1:


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.




回答2:


I don't think you're missing something obvious. I don't see any reference to thread-safety with popen() either requiring it to be thread-safe or not. As a result you should probably treat it as non-thread-safe.



来源:https://stackoverflow.com/questions/1702442/popen-locks-or-not-thread-safe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!