1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| void do_bgfg(char **argv) {
int pid,jid;
//int isjob = 0; int bg = 0; sigset_t mask_one,prev_one; sigaddset(&mask_one,SIGCHLD); if(argv[1] == NULL ){ printf("bg command requires PID or %%jobid argument\n"); return; } if((argv[1][0] != '%' && (argv[1][0] < '0' || argv[1][0] > '9' ))){ printf("%s: argument must be a PID or %%jobid\n",argv[0]); return; } if(!strcmp(argv[0],"bg")) bg = 1; if(argv[1][0] != '%'){ pid = atoi(&argv[1][0]);
/* if(pid == 0){ printf("%s: No such job\n",argv[1]); return; } */
struct job_t *j = getjobpid(jobs,pid); if(!j) { printf("%s: No such job\n",argv[1]); return;
} //isjob = 1; if(bg){ //printf("first\n"); j->state = BG; printf("[%d] (%d) %s", j->jid, j->pid, j->cmdline); killpg(pid,SIGCONT);
}else{ // 进入前要对sigchld信号进行屏蔽,否则可能死循环。 sigprocmask(SIG_BLOCK,&mask_one,&prev_one);
if(j->state == ST) killpg(pid,SIGCONT); j->state = FG; waitfg(j->pid); sigprocmask(SIG_SETMASK,&prev_one,NULL); }
}else { jid = atoi(&argv[1][1]); /* if(jid == 0){ printf("(%s): No such process\n",argv[1]); return; } */ struct job_t *j = getjobjid(jobs,jid);
if(!j){ printf("(%s): No such process\n",argv[1]); return; } if(bg){ j->state = BG; printf("[%d] (%d) %s", j->jid, j->pid, j->cmdline); killpg(j->pid,SIGCONT);
}else{ // 进入前要对sigchld信号进行屏蔽,否则可能死循环。 //sigprocmask(SIG_BLOCK,&mask_one,&prev_one);
//printf("fg\n"); if(j->state== ST) killpg(j->pid,SIGCONT); j->state = FG;
waitfg(j->pid); //sigprocmask(SIG_SETMASK,&prev_one,NULL); } }
return; }
|