Skip to content

Commit

Permalink
Merge pull request #302 from kennytm/posix_spawn
Browse files Browse the repository at this point in the history
Use posix_spawn() instead of fork() for Linux and OS X
  • Loading branch information
WalterBright committed Aug 12, 2011
2 parents 7489948 + eaf035b commit 9bf9187
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/link.c
Expand Up @@ -25,6 +25,17 @@
#include <unistd.h>
#endif

#if linux || __APPLE__
#define HAS_POSIX_SPAWN 1
#include <spawn.h>
#if __APPLE__
#include <crt_externs.h>
#define environ (*(_NSGetEnviron()))
#endif
#else
#define HAS_POSIX_SPAWN 0
#endif

#include "root.h"

#include "mars.h"
Expand Down Expand Up @@ -222,7 +233,7 @@ int runLINK()

const char *cc = getenv("CC");
if (!cc)
cc = "gcc";
cc = "/usr/bin/gcc";
argv.push((char *)cc);
argv.insert(1, global.params.objfiles);

Expand Down Expand Up @@ -377,13 +388,27 @@ int runLINK()
}

argv.push(NULL);
#if HAS_POSIX_SPAWN
int spawn_err = posix_spawn(&childpid, argv.tdata()[0], NULL, NULL, argv.tdata(), environ);
if (spawn_err != 0)
{
perror(argv.tdata()[0]);
return -1;
}
#else
childpid = fork();
if (childpid == 0)
{
execvp(argv.tdata()[0], argv.tdata());
perror(argv.tdata()[0]); // failed to execute
return -1;
}
else if (childpid == -1)
{
perror("Unable to fork");
return -1;
}
#endif

waitpid(childpid, &status, 0);

Expand Down

0 comments on commit 9bf9187

Please sign in to comment.