ast_comparator/test/student_data/handout/shlab-handout/cases/24_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* job;
  pid_t pid;
  int jid;
  if(argv[1] == NULL){	
	printf("%s command requires PID or %%jobid argument\n", argv[0]);
	return;
  }
  if(isdigit(argv[1][0])){	
	pid = atoi(argv[1]);
	job = getjobpid(jobs, pid);
	if(job == 0){	
		printf("(%d): No such process\n", pid);
		return;
	}
  }else if(argv[1][0] == '%'){	
	jid = atoi(argv[1]+1);
	job = getjobjid(jobs, jid);
	if(job == 0){	
		printf("%s: No such job\n", argv[1]);
		return;
	}
  }else{	
	printf("%s: argument must be a PID or %%jobid\n", argv[0]);
	return;
  }
  if(!strcmp(argv[0], "bg")){		
	if(kill(-(job->pid), SIGCONT) < 0)		
			unix_error("Kill error");
	printf("[%d] (%d) %s", job->jid, job->pid, job->cmdline);
	job->state = BG;
  }
  else if(!strcmp(argv[0], "fg")){	
	while(fgpid(jobs));	
	if(kill(-(job->pid), SIGCONT) < 0)		
			unix_error("Kill error");
	job->state = FG;
	waitfg(job->pid);	
  }
  else printf("bg/fg error: %s\n", argv[0]);	
  return;
}
void do_bgfg ( char * * argv ) { struct job_t * job ; pid_t pid ; int jid ; if ( argv [ 1 ] == NULL ) { printf ( " \n " , argv [ 0 ] ) ; return ; } if ( isdigit ( argv [ 1 ] [ 0 ] ) ) { pid = atoi ( argv [ 1 ] ) ; job = getjobpid ( jobs , pid ) ; if ( job == 0 ) { printf ( " \n " , pid ) ; return ; } } else if ( argv [ 1 ] [ 0 ] == ' ' ) { jid = atoi ( argv [ 1 ] + 1 ) ; job = getjobjid ( jobs , jid ) ; if ( job == 0 ) { printf ( " \n " , argv [ 1 ] ) ; return ; } } else { printf ( " \n " , argv [ 0 ] ) ; return ; } if ( ! strcmp ( argv [ 0 ] , " " ) ) { if ( kill ( - ( job -> pid ) , SIGCONT ) < 0 ) unix_error ( " " ) ; printf ( " " , job -> jid , job -> pid , job -> cmdline ) ; job -> state = BG ; } else if ( ! strcmp ( argv [ 0 ] , " " ) ) { while ( fgpid ( jobs ) ) ; if ( kill ( - ( job -> pid ) , SIGCONT ) < 0 ) unix_error ( " " ) ; job -> state = FG ; waitfg ( job -> pid ) ; } else printf ( " \n " , argv [ 0 ] ) ; return ; }
do_bgfg - Execute the builtin bg and fg commands
do_bgfg - Execute the builtin bg and fg commands
/* If there is no argument, then require more argument */ //<job> is PID /* getjobpid() returns zero when there is no such process whose pid is 'pid'*/ //<job> is JID /* getjobjid() returns zero when there is no such process whose jid is 'jid'*/ /* Invalid argument */ /* command is bg */ /* signal to all process group members */ /* command is fg */ /* fg process can exist only one. So wait until previous fg process finished */ /* signal to all process group members */ /* Wait until fg process finished */ /* Invaild command */