Tasks till trace03 and added todo.org
parent
cf88f9601d
commit
1540d090d7
3
Makefile
3
Makefile
|
@ -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)
|
||||||
|
|
|
@ -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
|
57
tsh.c
57
tsh.c
|
@ -162,9 +162,31 @@ int main(int argc, char **argv)
|
||||||
* each child process must have a unique process group ID so that our
|
* each child process must have a unique process group ID so that our
|
||||||
* background children don't receive SIGINT (SIGTSTP) from the kernel
|
* background children don't receive SIGINT (SIGTSTP) from the kernel
|
||||||
* when we type ctrl-c (ctrl-z) at the keyboard.
|
* when we type ctrl-c (ctrl-z) at the keyboard.
|
||||||
*/
|
*/
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue