#import <spawn.h>
extern char **environ;
void run_cmd(const char *cmd)
{
pid_t pid;
const char *argv[] = {"sh", "-c", cmd, NULL};
int status;
status = posix_spawn(&pid, "/bin/sh", NULL, NULL, (char* const*)argv, environ);
if (status == 0) {
if (waitpid(pid, &status, 0) == -1) {
perror("waitpid");
}
}
}
然后像调用system()
一样调用run_cmd()
即可