I'm currently diving into creating a backgrounding a job in C
with &
. I need to implement a non-blocking waitpid
in order for this to work. I know that. Also, I already am catching the condition if &
is entered at the end of the command line. I'm just not sure how to exactly send the process with the end to be a background job and also implement it as executing while another prompt is prompting for the next command.
Anything would help at all, thank you.
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;
}
}
Alright, so I've been working on this some more and I think I may be starting to understand. I still have a few syntax errors that I'm unaware of how to fix so I'll probably use gdb or something unless someone else can point them out. Am I going about it the right way or am I completely wrong?