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