我目前正在潜心研究C
使用&
. 我需要实现非阻塞waitpid
才能使其正常工作。我知道。&
另外,如果在命令行末尾输入,我已经掌握了条件。我只是不确定如何准确地将进程最终发送为后台作业,并将其实现为正在执行,而另一个提示正在提示输入下一个命令。
任何事情都会有所帮助,谢谢。
struct bgprocess{
int pid;
struct bgprocess * next;
struct bgprocess * prev;
};
struct bgprocess * bgprocess1;
bgprocess1 = malloc(sizeof(struct bgprocess));
bgprocess1->prev = NULL;
bgprocess1->next = NULL;
bgprocess1->pid = NULL;
struct bgprocess * current;
current = bgprocess1;
do{
int bgreturn = 0;
while (current != NULL){
if (waitpid(current->pid, &bgreturn, WNOHANG)){
printf("Child exited");
current->prev->next = current->next;
current->next->prev = current->prev;
current->prev = NULL;
current->next = NULL;
free(current);
}
current = current->next;
}
if (end){
int pid = fork();
if (pid < 0){
exit(1);
}
if (pid) {
execvp(args[0], args);
exit(0);
}
struct bgprocess * newNode;
newNode = malloc(sizeof(struct bgprocess));
newNode->pid = pid;
newNode->next = NULL;
if (current->next == NULL){
current->next = newNode;
}
while (1){
if (current->next == NULL){
current->next = newNode;
}
current = current->next;
}
}
}
while (current != NULL);
int bgreturn = 0;
while (current != NULL){
if (waitpid(current->pid, &bgreturn,0)){
printf("Child exited");
current->prev->next = current->next;
current->next->prev = current->prev;
current->prev = NULL;
current->next = NULL;
free(current);
}
current = current->next;
}
}
好吧,所以我一直在努力解决这个问题,我想我可能开始理解了。我仍然有一些我不知道如何修复的语法错误,所以我可能会使用 gdb 或其他东西,除非其他人能指出它们。我是以正确的方式去做还是我完全错了?