-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.txt
68 lines (53 loc) · 1.36 KB
/
notes.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
File redirection (< and >)
- dup2 copies file descriptors
fd = open(file)
dup2(fd, 0) //0 for stdin, 1 is stdout, 2 is stderr
fd = open("fred.txt", O_RDONLY);
fork
if kid
for each arg
if arg is ">"
fdout = open for writing
error check
remove 2 args from the list
if arg is "<"
fdin = open for reading
error check
remove 2 args from the list
build the argument list
dup2(fd, 0)
for p in PATH
exec
print error
exit(3)
Good reference: https://stackoverflow.com/questions/8082932/connecting-n-commands-with-pipes-in-a-shell
Piping
- piping must be continuous
- pipe is a system call in linux
- MAKE SURE TO PIPE BEFORE FORK
- both sides have both file descriptors
reader must close write end of pipe (pipefd[1])
writer must close read end of pipe (pipefd[0])
1. close
2. dup2
3. exec
For example, "ls | wc" would go like this:
if arg is "|"
pipe()
fork() //we are now at 3 copies of the program (child forks itself on pipe)
if kid2 "ls"
close the read end (pipefd[0])
dup2(pipefd[1], 1) //stdout
exec "ls"
if parent "wc"
close the write end (pipefd[1])
dup2(pipefd[0]) //stdin
exec "wc"
using $ variables
-getenv() is allowed for anything besides PATH
if arg is $
replace with getenv(that arg without the $)
; is done before the fork
if ;
do everything before ;
do everything after ;