在Mac OS X上进行分叉后,可以调用chdir或setenv吗? [英] Can I call chdir or setenv after fork on Mac OS X?

查看:119
本文介绍了在Mac OS X上进行分叉后,可以调用chdir或setenv吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在OS X上,fork的手册页上说:

On OS X, the man page for fork says this:

在子进程中可以执行的操作受到限制.为完全安全起见,应限制自己仅执行异步信号安全操作,直到调用exec函数之一为止.除非明确证明是安全的或异步信号安全的,否则任何框架或库中的所有API(包括全局数据符号)都应假定为在fork()之后是不安全的.如果需要在子进程中使用这些框架,则必须执行.在这种情况下,执行自己是合理的.

There are limits to what you can do in the child process. To be totally safe you should restrict yourself to only executing async-signal safe operations until such time as one of the exec functions is called. All APIs, including global data symbols, in any framework or library should be assumed to be unsafe after a fork() unless explicitly documented to be safe or async-signal safe. If you need to use these frameworks in the child process, you must exec. In this situation it is reasonable to exec yourself.

基于手册页的页脚,可能已经存在很长时间了

Based on the footer of the man page, this has probably been there a long time:

第四届Berkeley发行版1993年6月4日第四届Berkeley发行版

4th Berkeley Distribution June 4, 1993 4th Berkeley Distribution

我曾经以为chdir(2)在fork()和exec()之间进行调用是安全的,但是其手册页并未说明异步调用是安全的.实际上,这是不安全的吗?如果是这样,我真的应该在fork()之前更改目录吗?对我来说似乎不合理.

I'd have thought that chdir(2) would be safe to call between fork() and exec(), but its man page does not say that it's safe for async calls. Is it, in fact, unsafe? If so, am I really expected to change directory before fork()? Seems unreasonable to me.

setenv(3)也一样.考虑到它调用了malloc(),因此我认为它可能不安全.但是没有等效的execvp允许我通过环境.具体来说,execvp搜索PATH.我能找到的最接近的等价于环境arg的execve()不会搜索PATH.

Same goes for setenv(3). Considering that it calls malloc(), I guess that it's probably not safe. But there is no equivalent of execvp that allows me to pass an environment. Specifically, execvp searches the PATH. The closest equivalent I could find that takes an environment arg, execve(), does not search the PATH.

推荐答案

您引用的第一行文本("...有限制...")是专门为Mac OS X添加的-它试图指出fork()之后,Apple的大多数库和框架(AppKit,Carbon,Foundation等)将无法正常工作.

The first block of text you're quoting ("There are limits...") was added specifically for Mac OS X -- it's trying to point out that most of Apple's libraries and frameworks (AppKit, Carbon, Foundation, etc) will not work properly after a fork().

据我所知,在fork()之后可以安全使用所有标准C库函数,包括chdir() ARE .

As far as I'm aware, all standard C library functions, including chdir(), ARE safe to use after fork().

这篇关于在Mac OS X上进行分叉后,可以调用chdir或setenv吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆