int cmd_bg(tok_t arg[]){ long pid = 0; process *p; if(arg[0]){ pid = strtol(arg[0], NULL, 10); for(p = first_process; p ; p = p->next){ if(p->pid == pid){ put_process_in_background(p,1); return 0; } } return -1; } else{ for(p = first_process; p->next; p = p->next){ } p = p->prev; if(p != first_process){ put_process_in_background(p,1); return 0; } else{ return -1; } } }
/* 06/22/2015 added thinkhy * Mark a stopped process as being running again. */ void continue_process(pid_t pid, int foreground) { process *p; for(p = first_process; p&&p->pid != pid; p = p->next); if(p == NULL) return; mark_process_as_running(p); if (foreground) put_process_in_foreground(p, 1); else put_process_in_background(p, 1); }
int manage_process(tok_t arg[], bool foreground) { process *p; pid_t pid = 0; if (arg[0]) pid = (pid_t)atoi(arg[0]); p = find_process(pid); if (!p) { printf("No such process: "); if (pid == 0) printf("most recently launched.\n"); else printf("%d.\n", pid); } else { p->stopped = false; if (foreground) { p->background = false; put_process_in_foreground(p, true); } else { p->background = true; put_process_in_background(p, true); } } return 1; }
void run_program(tok_t *t, char *s) { // execute another program specified in command process* proc; if ((proc = create_process(s)) != NULL) { add_process(proc); pid_t pid = fork(); // both parent and child should set put the child into its own process group if (pid == 0) { proc->pid = getpid(); launch_process(proc); } else { proc->pid = pid; if (shell_is_interactive) { setpgid(proc->pid, proc->pid); } if (proc->background) { put_process_in_background(proc, false); } else { put_process_in_foreground(proc, false); } } } }