ast_comparator/test/student_data/handout/shlab-handout/cases/reference_9_tsh.c
/////////////////////////////////////////////////////////////////////////////
//
// do_bgfg - Execute the builtin bg and fg commands
//
Execute the builtin bg and fg commands
Execute the builtin bg and fg commands
void do_bgfg(char **argv) 
{
  struct job_t *jobp=NULL;
  if (argv[1] == NULL) {
    printf("%s command requires PID or %%jobid argument\n", argv[0]);
    return;
  }
  if (isdigit(argv[1][0])) {
    pid_t pid = atoi(argv[1]);
    if (!(jobp = getjobpid(jobs, pid))) {
      printf("(%d): No such process\n", pid);
      return;
    }
  }
  else if (argv[1][0] == '%') {
    int jid = atoi(&argv[1][1]);
    if (!(jobp = getjobjid(jobs, jid))) {
      printf("%s: No such job\n", argv[1]);
      return;
    }
  }     
  else {
    printf("%s: argument must be a PID or %%jobid\n", argv[0]);
    return;
  }
   You need to complete rest. At this point,
   the variable 'jobp' is the job pointer
   for the job ID specified as an argument.
   Your actions will depend on the specified command
   so we've converted argv[0] to a string (cmd) for
   your benefit.
  string cmd(argv[0]);
  if((strcmp(argv[0],"bg")) == 0)  {
    jobp->state = BG; changes the state to bg
    kill(-jobp->pid, SIGCONT); tells the system to run the job again
    printf("[%d] (%d) %s", jobp->jid, jobp->pid, jobp->cmdline);
  }
  if((strcmp(argv[0],"fg")) == 0)  {
    jobp->state = FG; changes the state to bg
    kill(-jobp->pid, SIGCONT); tells the system to run the job again
    waitfg(jobp->pid); we need this waitfg because we need to have only one fg process running at a time
  }
  return;
}
void do_bgfg ( char * * argv ) { struct job_t * jobp = NULL ; if ( argv [ 1 ] == NULL ) { printf ( " \n " , argv [ 0 ] ) ; return ; } if ( isdigit ( argv [ 1 ] [ 0 ] ) ) { pid_t pid = atoi ( argv [ 1 ] ) ; if ( ! ( jobp = getjobpid ( jobs , pid ) ) ) { printf ( " \n " , pid ) ; return ; } } else if ( argv [ 1 ] [ 0 ] == ' ' ) { int jid = atoi ( & argv [ 1 ] [ 1 ] ) ; if ( ! ( jobp = getjobjid ( jobs , jid ) ) ) { printf ( " \n " , argv [ 1 ] ) ; return ; } } else { printf ( " \n " , argv [ 0 ] ) ; return ; } string cmd ( argv [ 0 ] ) ; if ( ( strcmp ( argv [ 0 ] , " " ) ) == 0 ) { jobp -> state = BG ; kill ( - jobp -> pid , SIGCONT ) ; printf ( " " , jobp -> jid , jobp -> pid , jobp -> cmdline ) ; } if ( ( strcmp ( argv [ 0 ] , " " ) ) == 0 ) { jobp -> state = FG ; kill ( - jobp -> pid , SIGCONT ) ; waitfg ( jobp -> pid ) ; } return ; }
do_bgfg - Execute the builtin bg and fg commands
do_bgfg - Execute the builtin bg and fg commands
/* Ignore command if no argument */ /* Parse the required PID or %JID arg */ // // You need to complete rest. At this point, // the variable 'jobp' is the job pointer // for the job ID specified as an argument. // // Your actions will depend on the specified command // so we've converted argv[0] to a string (cmd) for // your benefit. // //changes the state to bg //tells the system to run the job again //changes the state to bg //tells the system to run the job again //we need this waitfg because we need to have only one fg process running at a time