Tasks till trace03 and added todo.org

main
TuDatTr 2021-02-02 19:16:33 +01:00
parent cf88f9601d
commit 1540d090d7
3 changed files with 283 additions and 210 deletions

View File

@ -7,7 +7,8 @@ TSH = ./tsh
TSHREF = ./tshref TSHREF = ./tshref
TSHARGS = "-p" TSHARGS = "-p"
CC = gcc CC = gcc
CFLAGS = -Wall -O2 # CFLAGS = -Wall -O2
CFLAGS = -Wall -O0 -g3
FILES = $(TSH) ./myspin ./mysplit ./mystop ./myint FILES = $(TSH) ./myspin ./mysplit ./mystop ./myint
all: $(FILES) all: $(FILES)

17
todo.org Normal file
View File

@ -0,0 +1,17 @@
Testcases:
- [x] trace01.txt - Properly terminate on EOF.
- [x] trace02.txt - Process builtin quit command.
- [x] trace03.txt - Run a foreground job.
- [ ] trace04.txt - Run a background job.
- [ ] trace05.txt - Process jobs builtin command.
- [ ] trace06.txt - Forward SIGINT to foreground job.
- [ ] trace07.txt - Forward SIGINT only to foreground job.
- [ ] trace08.txt - Forward SIGTSTP only to foreground job.
- [ ] trace09.txt - Process bg builtin command
- [ ] trace10.txt - Process fg builtin command.
- [ ] trace11.txt - Forward SIGINT to every process in foreground process group
- [ ] trace12.txt - Forward SIGTSTP to every process in foreground process group
- [ ] trace13.txt - Restart every stopped process in process group
- [ ] trace14.txt - Simple error handling
- [ ] trace15.txt - Putting it all together

55
tsh.c
View File

@ -165,6 +165,28 @@ int main(int argc, char **argv)
*/ */
void eval(char *cmdline) void eval(char *cmdline)
{ {
char *argv[MAXARGS]; // Save flags for process
int bg = parseline(cmdline, argv); // See whether cmd is back or foreground and parse cmdline
struct job_t *job;
if (!builtin_cmd(argv)) { // if cmdline is not a builtin
int pid = fork(); // fork process
if (pid == 0) { // Child Process
if (execve(argv[0], argv, environ) != 0) { // if exec failed
printf("Command not found.\n");
exit(0);
}
exit(0);
} else { // Parent Process
wait(NULL);
if (bg) {
addjob(jobs, pid, BG, cmdline);
job = getjobpid(jobs, pid);
printf("[%d] (%d) %s", job->jid, pid, cmdline);
} else {
addjob(jobs, pid, FG, cmdline);
}
}
}
return; return;
} }
@ -231,6 +253,15 @@ int parseline(const char *cmdline, char **argv)
*/ */
int builtin_cmd(char **argv) int builtin_cmd(char **argv)
{ {
if (!strcmp(argv[0], "quit")) {
exit(0);
} else if (!strcmp(argv[0], "jobs")) {
listjobs(jobs);
return 1;
} else if (!strcmp(argv[0], "bg") || !strcmp(argv[0], "fg")) {
do_bgfg(argv);
return 1;
}
return 0; /* not a builtin command */ return 0; /* not a builtin command */
} }
@ -239,6 +270,26 @@ int builtin_cmd(char **argv)
*/ */
void do_bgfg(char **argv) void do_bgfg(char **argv)
{ {
// struct job_t* job;
// if (argv[1][0] == '%') { // if job is chosing via jid
// printf("jid: %s", &argv[1][1]);
// job = getjobjid(jobs, atoi(&argv[1][1]));
// } else { // if job is chosing via pid
// printf("pid: %s", argv[1]);
// job = getjobpid(jobs, atoi(argv[1]));
// }
// if (job != NULL) {
// if (!strcmp(argv[0], "bg")) { // Do this if bg
// if (job->state == ST) {
// job->state = BG;
// }
// } else { // Do this if fg
// if (job->state == ST || job->state == BG) {
// job->state = FG;
// }
// }
// }
return; return;
} }
@ -247,6 +298,9 @@ void do_bgfg(char **argv)
*/ */
void waitfg(pid_t pid) void waitfg(pid_t pid)
{ {
// while (pid == fgpid(jobs)) {
// sleep(1);
// }
return; return;
} }
@ -263,6 +317,7 @@ void waitfg(pid_t pid)
*/ */
void sigchld_handler(int sig) void sigchld_handler(int sig)
{ {
deletejob(jobs, fgpid(jobs));
return; return;
} }