Skip to content

Commit 0b23e94

Browse files
committed
feat: complete the shell lab
The solution is in tsh.c. After running the command 'make', use file test.sh to check the correctness of the program.
1 parent 9484531 commit 0b23e94

29 files changed

+1828
-0
lines changed

Shell_Lab/shlab-handout.tar

80 KB
Binary file not shown.

Shell_Lab/shlab.pdf

34.8 KB
Binary file not shown.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Makefile for the CS:APP Shell Lab
2+
3+
TEAM = NOBODY
4+
VERSION = 1
5+
HANDINDIR = /afs/cs/academic/class/15213-f02/L5/handin
6+
DRIVER = ./sdriver.pl
7+
TSH = ./tsh
8+
TSHREF = ./tshref
9+
TSHARGS = "-p"
10+
CC = gcc
11+
CFLAGS = -Wall -O2
12+
FILES = $(TSH) ./myspin ./mysplit ./mystop ./myint
13+
14+
all: $(FILES)
15+
16+
##################
17+
# Handin your work
18+
##################
19+
handin:
20+
cp tsh.c $(HANDINDIR)/$(TEAM)-$(VERSION)-tsh.c
21+
22+
23+
##################
24+
# Regression tests
25+
##################
26+
27+
# Run tests using the student's shell program
28+
test01:
29+
$(DRIVER) -t trace01.txt -s $(TSH) -a $(TSHARGS)
30+
test02:
31+
$(DRIVER) -t trace02.txt -s $(TSH) -a $(TSHARGS)
32+
test03:
33+
$(DRIVER) -t trace03.txt -s $(TSH) -a $(TSHARGS)
34+
test04:
35+
$(DRIVER) -t trace04.txt -s $(TSH) -a $(TSHARGS)
36+
test05:
37+
$(DRIVER) -t trace05.txt -s $(TSH) -a $(TSHARGS)
38+
test06:
39+
$(DRIVER) -t trace06.txt -s $(TSH) -a $(TSHARGS)
40+
test07:
41+
$(DRIVER) -t trace07.txt -s $(TSH) -a $(TSHARGS)
42+
test08:
43+
$(DRIVER) -t trace08.txt -s $(TSH) -a $(TSHARGS)
44+
test09:
45+
$(DRIVER) -t trace09.txt -s $(TSH) -a $(TSHARGS)
46+
test10:
47+
$(DRIVER) -t trace10.txt -s $(TSH) -a $(TSHARGS)
48+
test11:
49+
$(DRIVER) -t trace11.txt -s $(TSH) -a $(TSHARGS)
50+
test12:
51+
$(DRIVER) -t trace12.txt -s $(TSH) -a $(TSHARGS)
52+
test13:
53+
$(DRIVER) -t trace13.txt -s $(TSH) -a $(TSHARGS)
54+
test14:
55+
$(DRIVER) -t trace14.txt -s $(TSH) -a $(TSHARGS)
56+
test15:
57+
$(DRIVER) -t trace15.txt -s $(TSH) -a $(TSHARGS)
58+
test16:
59+
$(DRIVER) -t trace16.txt -s $(TSH) -a $(TSHARGS)
60+
61+
# Run the tests using the reference shell program
62+
rtest01:
63+
$(DRIVER) -t trace01.txt -s $(TSHREF) -a $(TSHARGS)
64+
rtest02:
65+
$(DRIVER) -t trace02.txt -s $(TSHREF) -a $(TSHARGS)
66+
rtest03:
67+
$(DRIVER) -t trace03.txt -s $(TSHREF) -a $(TSHARGS)
68+
rtest04:
69+
$(DRIVER) -t trace04.txt -s $(TSHREF) -a $(TSHARGS)
70+
rtest05:
71+
$(DRIVER) -t trace05.txt -s $(TSHREF) -a $(TSHARGS)
72+
rtest06:
73+
$(DRIVER) -t trace06.txt -s $(TSHREF) -a $(TSHARGS)
74+
rtest07:
75+
$(DRIVER) -t trace07.txt -s $(TSHREF) -a $(TSHARGS)
76+
rtest08:
77+
$(DRIVER) -t trace08.txt -s $(TSHREF) -a $(TSHARGS)
78+
rtest09:
79+
$(DRIVER) -t trace09.txt -s $(TSHREF) -a $(TSHARGS)
80+
rtest10:
81+
$(DRIVER) -t trace10.txt -s $(TSHREF) -a $(TSHARGS)
82+
rtest11:
83+
$(DRIVER) -t trace11.txt -s $(TSHREF) -a $(TSHARGS)
84+
rtest12:
85+
$(DRIVER) -t trace12.txt -s $(TSHREF) -a $(TSHARGS)
86+
rtest13:
87+
$(DRIVER) -t trace13.txt -s $(TSHREF) -a $(TSHARGS)
88+
rtest14:
89+
$(DRIVER) -t trace14.txt -s $(TSHREF) -a $(TSHARGS)
90+
rtest15:
91+
$(DRIVER) -t trace15.txt -s $(TSHREF) -a $(TSHARGS)
92+
rtest16:
93+
$(DRIVER) -t trace16.txt -s $(TSHREF) -a $(TSHARGS)
94+
95+
96+
# clean up
97+
clean:
98+
rm -f $(FILES) *.o *~
99+
100+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
################
2+
CS:APP Shell Lab
3+
################
4+
5+
Files:
6+
7+
Makefile # Compiles your shell program and runs the tests
8+
README # This file
9+
tsh.c # The shell program that you will write and hand in
10+
tshref # The reference shell binary.
11+
12+
# The remaining files are used to test your shell
13+
sdriver.pl # The trace-driven shell driver
14+
trace*.txt # The 15 trace files that control the shell driver
15+
tshref.out # Example output of the reference shell on all 15 traces
16+
17+
# Little C programs that are called by the trace files
18+
myspin.c # Takes argument <n> and spins for <n> seconds
19+
mysplit.c # Forks a child that spins for <n> seconds
20+
mystop.c # Spins for <n> seconds and sends SIGTSTP to itself
21+
myint.c # Spins for <n> seconds and sends SIGINT to itself
22+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* myint.c - Another handy routine for testing your tiny shell
3+
*
4+
* usage: myint <n>
5+
* Sleeps for <n> seconds and sends SIGINT to itself.
6+
*
7+
*/
8+
#include <stdio.h>
9+
#include <unistd.h>
10+
#include <stdlib.h>
11+
#include <sys/types.h>
12+
#include <sys/wait.h>
13+
#include <signal.h>
14+
15+
int main(int argc, char **argv)
16+
{
17+
int i, secs;
18+
pid_t pid;
19+
20+
if (argc != 2) {
21+
fprintf(stderr, "Usage: %s <n>\n", argv[0]);
22+
exit(0);
23+
}
24+
secs = atoi(argv[1]);
25+
26+
for (i=0; i < secs; i++)
27+
sleep(1);
28+
29+
pid = getpid();
30+
31+
if (kill(pid, SIGINT) < 0)
32+
fprintf(stderr, "kill (int) error");
33+
34+
exit(0);
35+
36+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* myspin.c - A handy program for testing your tiny shell
3+
*
4+
* usage: myspin <n>
5+
* Sleeps for <n> seconds in 1-second chunks.
6+
*
7+
*/
8+
#include <stdio.h>
9+
#include <unistd.h>
10+
#include <stdlib.h>
11+
12+
int main(int argc, char **argv)
13+
{
14+
int i, secs;
15+
16+
if (argc != 2) {
17+
fprintf(stderr, "Usage: %s <n>\n", argv[0]);
18+
exit(0);
19+
}
20+
secs = atoi(argv[1]);
21+
for (i=0; i < secs; i++)
22+
sleep(1);
23+
exit(0);
24+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* mysplit.c - Another handy routine for testing your tiny shell
3+
*
4+
* usage: mysplit <n>
5+
* Fork a child that spins for <n> seconds in 1-second chunks.
6+
*/
7+
#include <stdio.h>
8+
#include <unistd.h>
9+
#include <stdlib.h>
10+
#include <sys/types.h>
11+
#include <sys/wait.h>
12+
#include <signal.h>
13+
14+
int main(int argc, char **argv)
15+
{
16+
int i, secs;
17+
18+
if (argc != 2) {
19+
fprintf(stderr, "Usage: %s <n>\n", argv[0]);
20+
exit(0);
21+
}
22+
secs = atoi(argv[1]);
23+
24+
25+
if (fork() == 0) { /* child */
26+
for (i=0; i < secs; i++)
27+
sleep(1);
28+
exit(0);
29+
}
30+
31+
/* parent waits for child to terminate */
32+
wait(NULL);
33+
34+
exit(0);
35+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* mystop.c - Another handy routine for testing your tiny shell
3+
*
4+
* usage: mystop <n>
5+
* Sleeps for <n> seconds and sends SIGTSTP to itself.
6+
*
7+
*/
8+
#include <stdio.h>
9+
#include <unistd.h>
10+
#include <stdlib.h>
11+
#include <sys/types.h>
12+
#include <sys/wait.h>
13+
#include <signal.h>
14+
15+
int main(int argc, char **argv)
16+
{
17+
int i, secs;
18+
pid_t pid;
19+
20+
if (argc != 2) {
21+
fprintf(stderr, "Usage: %s <n>\n", argv[0]);
22+
exit(0);
23+
}
24+
secs = atoi(argv[1]);
25+
26+
for (i=0; i < secs; i++)
27+
sleep(1);
28+
29+
pid = getpid();
30+
31+
if (kill(-pid, SIGTSTP) < 0)
32+
fprintf(stderr, "kill (tstp) error");
33+
34+
exit(0);
35+
36+
}

0 commit comments

Comments
 (0)