//在前台执行进程号为pid的进程 void fgCmd(int pid){ JOB *cur= NULL; sigchld = 1; int i; cur=findJob(pid); if(cur== NULL){ printf("Job %d do not exsit.\n", pid); return; } fgPid = cur->pid; //cur->state=(char*)malloc(STLEN); strcpy(cur->state, "running"); //设置signal信号,为下一次按下组合键Ctrl+Z做准备 signal(SIGTSTP, ctrlZ); //去掉后台程序运行标志 i = strlen(cur->command) - 1; while(i >= 0 && cur->command[i] != '&') i--; cur->command[i] = '\0'; printf("%s\n", cur->command); //发送SIGCONT信号使进程号为cur->pid的进程继续执行 kill(cur->pid, SIGCONT); //在前台作业执行完之前处于等待 waitpid(fgPid, NULL, 0); }
JobInMemory* loadJob(Memory* mem, Job* job, int clock){ if(job->pages==-1){ fprintf(stderr,"Memory management enabled without setting pages \n" "Please check the Job file\n" "Aborting...\n"); exit(EXIT_FAILURE); } JobInMemory* curjob = findJob(mem->jobs, job); if(curjob==NULL){ curjob = createJob(mem,job,clock); } int pages=0; if(curjob!=NULL){ for(int j=0;j<curjob->pages_for_job;j++){ Page* thispage = curjob->pages[j]; if(thispage!=NULL && thispage->job==job && thispage->last_accessed_at != clock) { debug_print("Found page: %d for job %s \n", PAGELOC(thispage,mem->pages[0]), job->jobname); thispage->last_accessed_at = clock; // updates the last accessed time pages++; list_iterator_Page* it= malloc(sizeof(*it)); list_Page_iterator_init(it,mem->LRU_list); Page* curpage = NULL; while((curpage= list_Page_examine(it))!=NULL){ if(curpage == thispage){ list_Page_remove(it); break; } list_Page_next(it); } list_Page_append(mem->LRU_list, thispage); free(it); } else{ if(!assignPage(mem,curjob,j,clock)){ fprintf(stderr,"Unable to assign pages, system error\n"); exit(EXIT_FAILURE); } } pages++; } debug_print("Job:%s has loaded %d pages in memory\n",job->jobname,pages); } //LRU stuff return curjob; }
//ctrl+c命令终止前台进程 void ctrlC(){ JOB *cur = NULL; if(fgPid == 0) return; sigchld = 0; cur=findJob(fgPid); if(cur == NULL) return ; //发送SIGTERM信号终止前台进程 kill(fgPid, SIGTERM); //fgPid = 0; }
//在后台执行进程号为pid的进程 void bgCmd(int pid){ JOB*cur=NULL; sigchld = 1; cur=findJob(pid); if(cur==NULL){ printf("Job %d do not exsit.\n", pid); return; } //由于已经挂起,所以已经获得了&标记 strcpy(cur->state, "running"); printf("%d\t%s\t%s\n", cur->pid, cur->state, cur->command); //发送SIGCONT信号使进程号为cur->pid的进程继续执行 kill(cur->pid, SIGCONT); }
//从一个作业中删除某进程 //返回所属进程组号,如果返回后该作业被撤销,返回进程组号的相反数 pid_t rmJob(pid_t pid) { Job* tmp; plistInt p; int i; i = findJob(pid,&tmp); if(!tmp) return 0; p = tmp->pids; if(p->pid == pid){ tmp->pids = p->next; //fprintf(stderr,"----- rm job %d\n",pid); free(p); } else{ while(p->next && p->next->pid != pid) p = p->next; if(p->next && p->next->pid == pid){ plistInt t = p->next; p->next = t->next; free(t); //fprintf(stderr,"----- rm job %d\n",pid); } } if(tmp->pids==NULL){ Job* t = list; pid_t tpp; tmp->state = STOPPED; tpp = tmp->pgid; fprintf(stderr,"[%d]\t%s\t%s",i,tmp->state,tmp->cmd); if(list == tmp) list = tmp->next; else{ while(t->next != tmp) t = t->next; t->next = tmp->next; } free(tmp); //fprintf(stderr,"----- destroy job %d\n",pid); return -tpp; } return tmp->pgid; }
// Remove the job from out list // If the list is empty, set the job counter to zero. void removeJob(pid_t pid) { // printf("- [removeJob] Job_counter = %d at BEGIN\n", job_counter); jobT *job; if ((job = findJob(pid)) != NULL) { if (job->next != NULL) { job->prev->next = job->next; job->next->prev = job->prev; // printf("Head == tail : %d", (head == tail)); } else { tail = job->prev; tail->next = NULL; } free(job); } if (head == tail) { job_counter = 0; } // printf("- [removeJob] Job_counter = %d at END\n", job_counter); }
//ctrl+z命令挂起前台进程 void ctrlZ(){ JOB *cur = NULL; if(fgPid == 0) return; sigchld = 1; cur=findJob(fgPid); if(cur == NULL) cur = insertJob(fgPid); //cur->state=(char*)malloc(STLEN); strcpy(cur->state, "stopped"); //cur->command=(char*)malloc(COMMLEN); cur->command[strlen(cur->command)] = '&'; cur->command[strlen(cur->command) + 1] = '\0'; printf("%d\t%s\t%s\n", cur->pid, cur->state, cur->command); //发送SIGTERM信号挂起前台进程 kill(fgPid, SIGSTOP); fgPid = 0; }
void handleNode(Node* node) { if(node->sleeping_for > 0) { node->sleeping_for--; return; } if(!node->current_job) { omp_set_lock(&jobs_lock); Job* job = findJob(node); if(job) { float corr = node->getCorrectnessScaledRandom(); job->removeFromSet(); job->assumed_correctness_sum += corr; job->assumed_correctness[node] = corr; job->insertToSet(); node->current_job = job; } omp_unset_lock(&jobs_lock); if(!node->current_job) { return; } node->work_scale = 10; } node->worked_for++; if(--node->work_scale == 0) { Job* job = node->current_job; omp_set_lock(&jobs_lock); job->submit(node, 0); omp_unset_lock(&jobs_lock); node->current_job = NULL; } }
void children (int signum) { int status; pid_t pid = waitpid(-1,&status,WUNTRACED); int exited = WIFEXITED(status); int stopped = WIFSTOPPED(status); JobList *j = findJob(pid); if(!j) return; if(exited) { remove_job(pid); printf("Job [%d] %s terminated\n", j->id, j->comm); } else if(stopped) { printf("Job [%d] %s stopped\n", j->id, j->comm); } }
// Edit the job's status. void editJob(pid_t pid, job_status status) { jobT *job = findJob(pid); job->status = status; }