POPEN - 锁或不是线程安全的?
-
19-09-2019 - |
题
我看到的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;
时它是什么样子 - 一个不是线程安全的实现?还是我失去了一些东西明显?
解决方案
在如果libc中被配置为可重入(它可能是)GNU libc实现是线程。然而,这可能不适合的libc的其它实施方式的情况。
其他提示
我不认为你失去了一些东西明显。我没有看到POPEN到线程安全的任何引用()或者要求它是线程安全与否。因此,你或许应该把它当作非线程安全的。
不隶属于 StackOverflow