我正在为大学做一个 minishell 项目,但我不知道如何在后台执行命令。我知道的一件事是我必须使用 waitpid() 和 sigaction(),但我不知道如何使用。如果有人帮我解决这个问题,我将不胜感激。这是我使用的部分代码,用于使用 minishell 命令。
void execute_command_line(command* cmds, int n){
pid_t id,pid;
int status;
id=fork();
if(id==-1){
exit(EXIT_FAILURE);
}
else if(if==0){
execvp(cmds[0] . argv[0],&cmds[0] .argv[0]);
exit(0);
}
else{
pid=wait(&status);
if(pid==-1){
perror("Father: an error has ocurred.\n");
exit(EXIT_FAILURE);
}
else if(pid==id){
printf("Father: the son has ended.\n");
}
}
}