in MIT 6.828 homework shell
https://pdos.csail.mit.edu/6.828/2018/homework/xv6-shell.html
I complement shell’s pipe part , which enable me to execute some pipe command such as ls > temp and it works well. But when I execute ./my_sh < t.sh
//t.sh
1 ls > y
2 cat < y | sort | uniq | wc > y1
3 cat y1
4 rm y1
5 ls | sort | uniq | wc
6 rm y
It gets error like:
10 10 62
10 10 62
rm: cannot remove 'y': No such file or directory
rm: cannot remove 'y': No such file or directory
rm: cannot remove 'y': No such file or directory
cat: y1: No such file or directory
rm: cannot remove 'y1': No such file or directory
9 9 60
rm: cannot remove 'y': No such file or directory
rm: cannot remove 'y': No such file or directory
rm: cannot remove 'y': No such file or directory
rm: cannot remove 'y': No such file or directory
cat: y1: No such file or directory
rm: cannot remove 'y1': No such file or directory
9 9 60
rm: cannot remove 'y': No such file or directory
rm: cannot remove 'y': No such file or directory
rm: cannot remove 'y': No such file or directory
rm: cannot remove 'y': No such file or directory
cat: y1: No such file or directory
rm: cannot remove 'y1': No such file or directory
9 9 60
rm: cannot remove 'y': No such file or directory
rm: cannot remove 'y': No such file or directory
rm: cannot remove 'y': No such file or directory
rm: cannot remove 'y': No such file or directory
Here is my code:
86 case '|':
87 pcmd = (struct pipecmd*)cmd;
88 // Your code here ...
89 //build pipe
90 if(pipe(p)== -1){
91 perror("pipe");
92 _exit(-1);
93 }
94 //pipe read at right
95 pid_t lpid,rpid;
96 if((lpid = fork1()) == 0){
97 close(1);//0-1-2|stdin-stdout-stderr
98 dup(p[1]);//let fd=1 point to write-side pipe
99 close(p[0]);
100 close(p[1]);
101 runcmd(pcmd->left);
102 }
103 wait(&r);
104 if((rpid = fork1()) == 0){
105 close(0);
106 dup(p[0]);//let fd=0 point to read-side pipe
107 close(p[0]);
108 close(p[1]);
109 runcmd(pcmd->right);
110 }
111 close(p[0]);
112 close(p[1]);
113 wait(&r);
114 break;
When I change way of fork , it works well
86 case '|':
87 pcmd = (struct pipecmd*)cmd;
88 // Your code here ...
89 //build pipe
90 if(pipe(p)== -1){
91 perror("pipe");
92 _exit(-1);
93 }
94 //pipe read at right
95 pid_t lpid;
96 lpid = fork1();
97 if(lpid == 0){
98 close(1);//0-1-2|stdin-stdout-stderr
99 dup(p[1]);//let fd=1 point to write-side pipe
100 close(p[0]);
101 close(p[1]);
102 runcmd(pcmd->left);
103 }
104 else{
105 close(0);
106 dup(p[0]);//let fd=0 point to read-side pipe
107 close(p[0]);
108 close(p[1]);
109 wait(&r);
110 runcmd(pcmd->right);
111 }
112 break;
Why the first way gets error , and what is the difference between those two ways.
New contributor
Jiaxi Huang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.