C语言 如何使用 fork() 和 exec() 在 Mac OS 上创建进程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4326684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:15:51  来源:igfitidea点击:

How to create a process on Mac OS using fork() and exec()

cmacosprocessexecfork

提问by Chris

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:

我正在开发一个相对简单、独立的“进程启动器”,我希望它能够在 Windows(XP、Vista、7)、Linux(Ubuntu 10.10),尤其是Mac OS X(10.6)上工作。Linux 和 Windows 基本上可以工作,但我在 Mac 版本上遇到了一些问题。我希望fork()exec()功能将工作在Mac OS同样的方式,因为他们在Linux下工作。所以我的第一个问题是:

  1. Should I use these to create a process on the Mac or are there any platform specific functions to be used?
  1. 我应该使用这些在 Mac 上创建一个进程还是有任何平台特定的功能要使用?

My current code (which worked fine under Linux) to debug this looks something like this:

我当前用于调试的代码(在 Linux 下运行良好)如下所示:

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
{
    //[...]
}

Any idea if this could work under Mac OS X as well, because my child process is simply not being launched, while there are no errors that would come up.

不知道这是否也可以在 Mac OS X 下工作,因为我的子进程根本没有启动,而不会出现错误。

Thank you!

谢谢!

回答by Arkku

The following program, adapted from your code, works just fine for me under OS X:

以下程序改编自您的代码,在 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.

我建议你从这个简单的片段开始,如果它有效,继续添加东西,直到你找到导致它崩溃的原因。

回答by Justin Spahr-Summers

Is TestApplicationan actual executable, or an application bundle (.app)? You can only launch actual executables using functions like execv(). Usually the executable inside an application bundle can be found at ApplicationName.app/Contents/MacOS/ApplicationName.

TestApplication实际的可执行文件还是应用程序包 (.app)?您只能使用诸如execv(). 通常应用程序包中的可执行文件可以在ApplicationName.app/Contents/MacOS/ApplicationName.