如何用fork()和exec创建在Mac OS的过程() [英] How to create a process on Mac OS using fork() and exec()
问题描述
我的工作比较简单的,独立的流程启动的我愿意去工作在Windows(XP,Vista中,7),Linux操作系统(Ubuntu的10.10),尤其是的Mac OS X (10.6)。 Linux和Windows的基本工作原理,但我有一些麻烦与Mac版本。
我希望叉()
和执行exec()
功能将工作在Mac OS同样的方式,因为他们在Linux下工作。所以我的第一个问题是:
I am working on a relatively simple, independent "process starter" that I would like to get to work on Windows (XP, Vista, 7), Linux (Ubuntu 10.10) and especially Mac OS X (10.6). Linux and Windows basically work, but I'm having some trouble with the Mac version.
I was hoping fork()
and exec()
functions would work the same way under Mac OS as they work in Linux. So my first question is:
- 我应该用这些来创建
在Mac或过程有什么
平台具体功能是
使用?
- Should I use these to create a process on the Mac or are there any platform specific functions to be used?
我目前的code(这在Linux下工作得很好)调试,这看起来是这样的:
My current code (which worked fine under Linux) to debug this looks something like this:
pid_t processId = 0;
if (processId = fork()) == 0)
{
const char * tmpApplication = "/Path/to/TestApplication";
int argc = 1;
char * argv[argc + 1];
argv[0] = tmpApplication;
argv[1] = NULL;
execv(tmpApplication, argv);
}else
{
//[...]
}
的任何想法,如果这可以在Mac OS X以及工作,因为我的子进程根本没有启动,而没有任何错误,将出现。
感谢您!
推荐答案
下面的程序,改编自您code,工作得很好,我在OS X:
The following program, adapted from your code, works just fine for me under OS X:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main (void) {
pid_t processId;
if ((processId = fork()) == 0) {
char app[] = "/bin/echo";
char * const argv[] = { app, "success", NULL };
if (execv(app, argv) < 0) {
perror("execv error");
}
} else if (processId < 0) {
perror("fork error");
} else {
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
我建议你开始用这个简单的片段,如果它的工作原理不断增加的东西,直到你找到什么使得它打破。
I suggest you start with this simple fragment, and if it works keep adding things until you find what makes it break.
这篇关于如何用fork()和exec创建在Mac OS的过程()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!