반응형
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <spawn.h>
#include <sys/wait.h>
extern char **environ;
void run_cmd(char *cmd)
{
status = posix_spawn(&pid, "/bin/sh", NULL, NULL, argv, environ);
if(status == 0){
printf("Child pid: %i\n", pid);
if(waitpid(pid, &status, 0) != -1){
printf("Child exited with status %i\n", status);
}
else{
do{
if(waitpid(pid, &status, 0) != -1){
printf("Child status %d\n", WEXITSTATUS(status));
}
else{
perror("waitpid");
}
exit(1);
}
}
while(!WIFEXITED(status) && !WIFSIGNALED(status));
}
else{
printf("posix_spawn: %s\n", strerror(status));
}
}
void main(int argc, char* argv[])
{
run_cmd(argv[1]);
return;
}
반응형