int main() { pid_t pid = fork(); if (pid < 0) { printf("fail to fork\n"); exit(1); } else if (pid == 0) { if (execp("hello", NULL) < 0) { printf("fail to exec\n"); exit(0); } printf("the child is not hello\n"); exit(0); } printf("the parent\n"); return 0; }
/** * Parse and executes a command. It may be a program or a script. * * @param a command line string * @param return code from program */ int parse_and_execp(const std::string& cmdline) { std::vector<char*> arg = parse(cmdline); return execp(arg); }