12

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:

  1. Should I use these to create a process on the Mac or are there any platform specific functions to be used?

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

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.

Thank you!

Chris
  • 3,245
  • 4
  • 29
  • 53

2 Answers2

16

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.

Arkku
  • 41,011
  • 10
  • 62
  • 84
  • ok - I think I know what my problem is now, I actually tried to access memory in the parent process (to look up the actual child process name), which seems to fail miserably - any suggestion how I would do this? to be more clear: I need to access an arbitrary application name after calling `fork()` to pass it to the `execv()` command. – Chris Dec 01 '10 at 19:38
  • @Chris You probably ought to create a new question about that problem and illustrate it with a code sample. I, for one, am not sure I understood what you are doing. – Arkku Dec 01 '10 at 20:57
  • it was just a bug in my code in the end, which I fixed now - thanks for your assistance - the example was really helpful! – Chris Dec 01 '10 at 21:18
  • As the man page for 'fork' (https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man2/fork.2.html) says: `LEGACY SYNOPSIS #include #include The include file is necessary.` Maybe it was that. – Alejandro García Iglesias May 07 '12 at 20:15
5

Is TestApplication an 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.

Justin Spahr-Summers
  • 16,893
  • 2
  • 61
  • 79
  • thank you Justin - but TestApplication is a "plain" unix executable that I build myself and also use under Windows & Linux without issues - so it is not an application bundle :( – Chris Dec 01 '10 at 16:48
  • 1
    To answer your original question, both `fork()` and `execv()` are perfectly fine to use. What does `strerror()` say after you try your `execv()`? – Justin Spahr-Summers Dec 01 '10 at 16:52
  • hmm - I don't really know, since execv() will be executed in the child process & I don't really know how I could debug that part inside the if statement of the above code – Chris Dec 01 '10 at 17:01
  • I tried this: http://stackoverflow.com/questions/377195/how-to-debug-the-entry-point-of-fork-exec-process[1] but it didn't work – Chris Dec 01 '10 at 17:15
  • The child process will inherit the parent's file descriptors, and this should include `stdout`, `stderr`, etc., so you might be able to do this: `fprintf(stderr, "Could not create child process: %s\n", strerror(errno))` – Justin Spahr-Summers Dec 01 '10 at 17:16