time_stats: Replace fork+exec with posix_spawn.
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <spawn.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -57,22 +58,16 @@ get_usage_infos(thread_info* infos)
|
|||||||
static pid_t
|
static pid_t
|
||||||
run_child(int argc, const char* const* argv)
|
run_child(int argc, const char* const* argv)
|
||||||
{
|
{
|
||||||
// fork
|
pid_t child;
|
||||||
pid_t child = fork();
|
int status = posix_spawnp(&child, argv[0], NULL, NULL, (char**)argv, NULL);
|
||||||
if (child < 0) {
|
if (status != 0) {
|
||||||
fprintf(stderr, "Error: fork() failed: %s\n", strerror(errno));
|
fprintf(stderr, "Error: spawn failed: %s\n", strerror(status));
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// exec child process
|
|
||||||
if (child == 0) {
|
|
||||||
execvp(argv[0], (char**)argv);
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// wait for child
|
// wait for child
|
||||||
int childStatus;
|
int childStatus;
|
||||||
while (wait(&childStatus) < 0);
|
while (waitpid(child, &childStatus, 0) < 0);
|
||||||
|
|
||||||
return child;
|
return child;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user