fic minor lab03

This commit is contained in:
2025-11-25 17:27:46 +09:00
parent 1c50fbd315
commit e1da8a7292

View File

@@ -1,8 +1,8 @@
/*
* tsh - A tiny shell program with job control
*
* Name: <fill in>
* Student id: <fill in>
* Name: Hajin Ju
* Student id: 2024062806
*/
#include <ctype.h>
#include <errno.h>
@@ -169,13 +169,21 @@ int main(int argc, char **argv) {
*/
void eval(char *cmdline) {
pid_t pid;
sigset_t mask, prev_mask;
char *argv[MAXARGS];
int bg = parseline(cmdline, argv);
if (argv[0] && !builtin_cmd(argv)) {
sigemptyset(&mask);
sigaddset(&mask, SIGCHLD);
sigprocmask(SIG_BLOCK, &mask, &prev_mask);
if ((pid = fork()) == 0) {// Child process
setpgid(0, 0); // Set new process group
sigprocmask(SIG_SETMASK, &prev_mask, NULL);
setpgid(0, 0);// Set new process group
if (execve(argv[0], argv, environ) < 0) {
printf("%s: Command not found\n", argv[0]);
@@ -185,11 +193,14 @@ void eval(char *cmdline) {
if (!bg) {// Foreground job
addjob(jobs, pid, FG, cmdline);
waitfg(pid);
} else {// Background job
addjob(jobs, pid, BG, cmdline);
fprintf(stderr, "[%d] (%d) %s", pid2jid(pid), pid, cmdline);
}
sigprocmask(SIG_SETMASK, &prev_mask, NULL);
if (!bg) waitfg(pid);
}
}
@@ -271,8 +282,8 @@ int builtin_cmd(char **argv) {
*/
void do_bgfg(char **argv) {
struct job_t *job = NULL;
pid_t pid;
int jid;
pid_t pid = 0;
int jid = 0;
if (argv[1] == NULL) {
printf("%s command requires PID or %%jobid argument\n", argv[0]);