void RemoveJob(pid){ bgjobL* job = bgjobs; if(job != NULL) { while(job != NULL) { if(job->pid == pid) { if(job->prev == NULL && job->next == NULL) { bgjobs = NULL; } else { if(job->prev != NULL) { job->prev->next = job->next; } if(job->next != NULL) { job->next->prev = job->prev; } } ReleaseJob(job); break; } } } }
void CheckJobs(){ jobNode* thisJob = jobHead; jobNode* prevJob = NULL; //iterate over all jobs and kill if necessary while (thisJob != NULL){ if (thisJob->state == DONE) { if (thisJob->printedJob == FALSE){ printf("[%d] Done %s \n", thisJob->jid,thisJob->cmdline); thisJob->printedJob = TRUE; } else { //give control to the shell fgpid = - 1; } if (prevJob == NULL){ jobHead = thisJob->next; ReleaseJob(thisJob); thisJob = jobHead; } else { //skip over and join previous with next prevJob->next = thisJob->next; ReleaseJob(thisJob); thisJob = prevJob->next; } fflush(stdout); } else{ //just continue prevJob = thisJob; thisJob = thisJob->next; } } if (jobHead==NULL) { //no more jobs, reset job id nextJobId = 1; } }
void KillAllJobs() { bgjobL* head = bgjobs; if (head) { bgjobL *a, *b; a = head; do { b = a->child; kill(-a->pid, SIGINT); ReleaseJob(a); a = b; } while (a != head); } }
void CheckJobs() { char str_out[128]; bgjobL* node = bgjobs; bgjobL* next; if (node) { do { if (kill(node->pid, 0)) { sprintf(str_out,"[%d] Done %s\n", node->jid, node->cmd); if (write(STDOUT_FILENO, str_out, strlen(str_out)) == -1) { perror("write error in CheckJobs"); } } node = node->child; } while (node != bgjobs); node = bgjobs; node = node->child; while (node != bgjobs) { next = node->child; if (kill(node->pid, 0)) { node->parent->child = node->child; node->child->parent = node->parent; ReleaseJob(node); } node = next; } if (kill(bgjobs->pid, 0)) { if (bgjobs->child == bgjobs) { ReleaseJob(bgjobs); bgjobs = NULL; } else { node = bgjobs; bgjobs = bgjobs->child; node->parent->child = node->child; node->child->parent = node->parent; ReleaseJob(node); } } } }
void DelJob(bgjobL** bgjobHeadPtr, pid_t pid) { bgjobL *ptr = *bgjobHeadPtr; if (ptr) { do { if (ptr->pid == pid) { if (ptr == *bgjobHeadPtr) { if (ptr->child == ptr) { *bgjobHeadPtr = NULL; } else { *bgjobHeadPtr = ptr->child; } } ptr->parent->child = ptr->child; ptr->child->parent = ptr->parent; ReleaseJob(ptr); return; } ptr = ptr->child; } while (ptr != *bgjobHeadPtr); } }
void removeCompletedJobs(){ bgjobL* current = bgjobs; bgjobL* prev = NULL; bgjobL* temp = NULL; //while we are not at the end of the list while(current != NULL) { //if status is done if(strcmp(current->status, "Done") == 0) { //if we are at the beginning of the list, //move the beginning to the next job if(prev == NULL) { bgjobs = current->next; } else { //set the previous job's next pointer to the next job prev->next = current->next; //set the next job's prev pointer to the prev job current->next->prev = prev; } //set the temp pointer to the next job so we have it after we free the current job temp = current->next; //release the current job ReleaseJob(current); //update current to the next job current = temp; } else { //move on to the next job current = current->next; } } }