Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CSEP551
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Krishna Vinnakota
CSEP551
Commits
21a88dd0
Commit
21a88dd0
authored
18 years ago
by
kaashoek
Browse files
Options
Downloads
Patches
Plain Diff
some pipe support in sh
bug in proc_wait
parent
d49a2d53
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
cat.c
+10
-10
10 additions, 10 deletions
cat.c
proc.c
+2
-2
2 additions, 2 deletions
proc.c
sh.c
+129
-73
129 additions, 73 deletions
sh.c
with
141 additions
and
85 deletions
cat.c
+
10
−
10
View file @
21a88dd0
...
@@ -27,17 +27,17 @@ main(int argc, char *argv[])
...
@@ -27,17 +27,17 @@ main(int argc, char *argv[])
if
(
argc
<=
1
)
{
if
(
argc
<=
1
)
{
rfile
(
0
);
rfile
(
0
);
}
else
{
}
else
{
for
(
i
=
1
;
i
<
argc
;
i
++
){
for
(
i
=
1
;
i
<
argc
;
i
++
){
fd
=
open
(
argv
[
i
],
0
);
fd
=
open
(
argv
[
i
],
0
);
if
(
fd
<
0
){
if
(
fd
<
0
){
puts
(
"cat: cannot open "
);
puts
(
"cat: cannot open "
);
puts
(
argv
[
i
]);
puts
(
argv
[
i
]);
puts
(
"
\n
"
);
puts
(
"
\n
"
);
exit
();
exit
();
}
rfile
(
fd
);
close
(
fd
);
}
}
rfile
(
fd
);
close
(
fd
);
}
}
}
exit
();
exit
();
...
...
This diff is collapsed.
Click to expand it.
proc.c
+
2
−
2
View file @
21a88dd0
...
@@ -371,7 +371,7 @@ proc_wait(void)
...
@@ -371,7 +371,7 @@ proc_wait(void)
havekids
=
0
;
havekids
=
0
;
for
(
i
=
0
;
i
<
NPROC
;
i
++
){
for
(
i
=
0
;
i
<
NPROC
;
i
++
){
p
=
&
proc
[
i
];
p
=
&
proc
[
i
];
if
(
p
->
ppid
==
cp
->
pid
){
if
(
p
->
state
!=
UNUSED
&&
p
->
ppid
==
cp
->
pid
){
if
(
p
->
state
==
ZOMBIE
){
if
(
p
->
state
==
ZOMBIE
){
// Found one.
// Found one.
kfree
(
p
->
mem
,
p
->
sz
);
kfree
(
p
->
mem
,
p
->
sz
);
...
@@ -385,7 +385,7 @@ proc_wait(void)
...
@@ -385,7 +385,7 @@ proc_wait(void)
havekids
=
1
;
havekids
=
1
;
}
}
}
}
// No point waiting if we don't have any children.
// No point waiting if we don't have any children.
if
(
!
havekids
){
if
(
!
havekids
){
release
(
&
proc_table_lock
);
release
(
&
proc_table_lock
);
...
...
This diff is collapsed.
Click to expand it.
sh.c
+
129
−
73
View file @
21a88dd0
...
@@ -7,21 +7,28 @@
...
@@ -7,21 +7,28 @@
#define BUFSIZ 512
#define BUFSIZ 512
#define MAXARGS 10
#define MAXARGS 10
#define MAXNODE 2
#define MAXNODE 2
#define MAXCMD 2
// only allocate nodes for i/o redir; at some point we may have to build a
// an embarrassingly naive shell
// a real parse tree.
struct
node
{
// some day a real parse tree; for now ad-hoc
struct
ionode
{
int
token
;
int
token
;
char
*
s
;
char
*
s
;
};
};
struct
node
list
[
MAXNODE
];
struct
io
node
io
list
[
MAXNODE
];
int
next
node
;
int
next
io
;
char
buf
[
BUFSIZ
];
struct
cmd
{
char
*
argv
[
MAXARGS
];
char
*
argv
[
MAXARGS
];
char
argv0buf
[
BUFSIZ
];
char
argv0buf
[
BUFSIZ
];
int
argc
;
int
argc
;
int
token
;
};
struct
cmd
cmdlist
[
MAXCMD
];
int
nextcmd
;
char
buf
[
BUFSIZ
];
int
debug
=
0
;
int
debug
=
0
;
int
parse
(
char
*
s
);
int
parse
(
char
*
s
);
...
@@ -29,7 +36,7 @@ void runcmd(void);
...
@@ -29,7 +36,7 @@ void runcmd(void);
int
ioredirection
(
void
);
int
ioredirection
(
void
);
int
gettoken
(
char
*
s
,
char
**
token
);
int
gettoken
(
char
*
s
,
char
**
token
);
int
_gettoken
(
char
*
s
,
char
**
p1
,
char
**
p2
);
int
_gettoken
(
char
*
s
,
char
**
p1
,
char
**
p2
);
void
add
node
(
int
token
,
char
*
s
);
void
add
io
(
int
token
,
char
*
s
);
int
int
main
(
void
)
main
(
void
)
...
@@ -48,21 +55,25 @@ int
...
@@ -48,21 +55,25 @@ int
parse
(
char
*
s
)
parse
(
char
*
s
)
{
{
char
*
t
;
char
*
t
;
int
c
;
int
c
,
i
;
gettoken
(
s
,
0
);
gettoken
(
s
,
0
);
argc
=
0
;
nextio
=
0
;
nextnode
=
0
;
nextcmd
=
0
;
for
(
i
=
0
;
i
<
MAXCMD
;
i
++
)
{
cmdlist
[
i
].
argc
=
0
;
cmdlist
[
i
].
token
=
0
;
}
while
(
1
)
{
while
(
1
)
{
switch
((
c
=
gettoken
(
0
,
&
t
)))
{
switch
((
c
=
gettoken
(
0
,
&
t
)))
{
case
'w'
:
// Add an argument
case
'w'
:
// Add an argument
if
(
argc
=
=
MAXARGS
)
{
if
(
cmdlist
[
nextcmd
].
argc
>
=
MAXARGS
)
{
printf
(
2
,
"too many arguments
\n
"
);
printf
(
2
,
"too many arguments
\n
"
);
return
-
1
;
return
-
1
;
}
}
argv
[
argc
++
]
=
t
;
cmdlist
[
nextcmd
].
argv
[
cmdlist
[
nextcmd
].
argc
++
]
=
t
;
break
;
break
;
case
'<'
:
// Input redirection
case
'<'
:
// Input redirection
...
@@ -71,7 +82,7 @@ parse(char *s)
...
@@ -71,7 +82,7 @@ parse(char *s)
printf
(
2
,
"syntax error: < not followed by word
\n
"
);
printf
(
2
,
"syntax error: < not followed by word
\n
"
);
return
-
1
;
return
-
1
;
}
}
add
node
(
'<'
,
t
);
add
io
(
'<'
,
t
);
break
;
break
;
case
'>'
:
// Output redirection
case
'>'
:
// Output redirection
...
@@ -80,7 +91,13 @@ parse(char *s)
...
@@ -80,7 +91,13 @@ parse(char *s)
printf
(
2
,
"syntax error: > not followed by word
\n
"
);
printf
(
2
,
"syntax error: > not followed by word
\n
"
);
return
-
1
;
return
-
1
;
}
}
addnode
(
'>'
,
t
);
addio
(
'>'
,
t
);
break
;
case
';'
:
// command sequence
case
'|'
:
// pipe
cmdlist
[
nextcmd
].
token
=
c
;
nextcmd
++
;
break
;
break
;
case
0
:
// String is complete
case
0
:
// String is complete
...
@@ -98,88 +115,127 @@ parse(char *s)
...
@@ -98,88 +115,127 @@ parse(char *s)
void
void
runcmd
(
void
)
runcmd
(
void
)
{
{
int
i
,
r
,
pid
;
int
c
,
i
,
r
,
pid
,
tfd
;
int
fdarray
[
2
];
// Return immediately if command line was empty.
// Return immediately if command line was empty.
if
(
argc
==
0
)
{
if
(
cmdlist
[
0
].
argc
==
0
)
{
if
(
debug
)
if
(
debug
)
printf
(
2
,
"EMPTY COMMAND
\n
"
);
printf
(
2
,
"EMPTY COMMAND
\n
"
);
return
;
return
;
}
}
// Clean up command line.
for
(
c
=
0
;
c
<=
nextcmd
;
c
++
)
{
// Read all commands from the filesystem: add an initial '/' to
// Clean up command line.
// the command name.
// Read all commands from the filesystem: add an initial '/' to
// This essentially acts like 'PATH=/'.
// the command name.
if
(
argv
[
0
][
0
]
!=
'/'
)
{
// This essentially acts like 'PATH=/'.
argv0buf
[
0
]
=
'/'
;
if
(
cmdlist
[
c
].
argv
[
0
][
0
]
!=
'/'
)
{
strcpy
(
argv0buf
+
1
,
argv
[
0
]);
cmdlist
[
c
].
argv0buf
[
0
]
=
'/'
;
argv
[
0
]
=
argv0buf
;
strcpy
(
cmdlist
[
c
].
argv0buf
+
1
,
cmdlist
[
c
].
argv
[
0
]);
}
cmdlist
[
c
].
argv
[
0
]
=
cmdlist
[
c
].
argv0buf
;
argv
[
argc
]
=
0
;
}
cmdlist
[
c
].
argv
[
cmdlist
[
c
].
argc
]
=
0
;
// Print the command.
// Print the command.
if
(
debug
)
{
if
(
debug
)
{
printf
(
2
,
"[%d] SPAWN:"
,
getpid
());
printf
(
2
,
"[%d] SPAWN:"
,
getpid
());
for
(
i
=
0
;
argv
[
i
];
i
++
)
for
(
i
=
0
;
cmdlist
[
c
].
argv
[
i
];
i
++
)
printf
(
2
,
" %s"
,
argv
[
i
]);
printf
(
2
,
" %s"
,
cmdlist
[
c
].
argv
[
i
]);
for
(
i
=
0
;
i
<
nextnode
;
i
++
)
{
for
(
i
=
0
;
i
<
nextio
;
i
++
)
{
printf
(
2
,
"%c %s"
,
list
[
i
].
token
,
list
[
i
].
s
);
printf
(
2
,
"%c %s"
,
iolist
[
i
].
token
,
iolist
[
i
].
s
);
}
printf
(
2
,
"
\n
"
);
}
}
printf
(
2
,
"
\n
"
);
}
if
(
strcmp
(
argv
[
0
],
"/cd"
)
==
0
)
{
if
(
debug
)
printf
(
2
,
"/cd %s is build in
\n
"
,
argv
[
1
]);
chdir
(
argv
[
1
]);
return
;
}
pid
=
fork
();
if
(
strcmp
(
cmdlist
[
c
].
argv
[
0
],
"/cd"
)
==
0
)
{
if
(
pid
==
0
)
{
if
(
debug
)
printf
(
2
,
"/cd %s is build in
\n
"
,
cmdlist
[
c
].
argv
[
1
]);
if
(
ioredirection
()
<
0
)
chdir
(
cmdlist
[
c
].
argv
[
1
]);
exit
();
return
;
if
((
r
=
exec
(
argv0buf
,
(
char
**
)
argv
))
<
0
)
{
printf
(
2
,
"exec %s: %d
\n
"
,
argv
[
0
],
r
);
exit
();
}
}
}
if
(
pid
>
0
)
{
if
(
cmdlist
[
c
].
token
==
'|'
)
if
(
debug
)
if
(
pipe
(
fdarray
)
<
0
)
printf
(
2
,
"[%d] WAIT %s
\n
"
,
getpid
(),
argv
[
0
]);
printf
(
2
,
"cmd %d pipe failed
\n
"
,
c
);
wait
();
if
(
debug
)
pid
=
fork
();
printf
(
2
,
"[%d] wait finished
\n
"
,
getpid
());
if
(
pid
==
0
)
{
if
(
cmdlist
[
c
].
token
==
'|'
)
{
if
(
close
(
1
)
<
0
)
printf
(
2
,
"close 1 failed
\n
"
);
if
((
tfd
=
dup
(
fdarray
[
1
]))
<
0
)
printf
(
2
,
"dup failed
\n
"
);
if
(
close
(
fdarray
[
0
])
<
0
)
printf
(
2
,
"close fdarray[0] failed
\n
"
);
if
(
close
(
fdarray
[
1
])
<
0
)
printf
(
2
,
"close fdarray[1] failed
\n
"
);
}
if
(
c
>
0
&&
cmdlist
[
c
-
1
].
token
==
'|'
)
{
if
(
close
(
0
)
<
0
)
printf
(
2
,
"close 0 failed
\n
"
);
if
((
tfd
=
dup
(
fdarray
[
0
]))
<
0
)
printf
(
2
,
"dup failed
\n
"
);
if
(
close
(
fdarray
[
0
])
<
0
)
printf
(
2
,
"close fdarray[0] failed
\n
"
);
if
(
close
(
fdarray
[
1
])
<
0
)
printf
(
2
,
"close fdarray[1] failed
\n
"
);
}
if
(
ioredirection
()
<
0
)
exit
();
if
((
r
=
exec
(
cmdlist
[
c
].
argv0buf
,
(
char
**
)
cmdlist
[
c
].
argv
))
<
0
)
{
printf
(
2
,
"exec %s: %d
\n
"
,
cmdlist
[
c
].
argv
[
0
],
r
);
exit
();
}
}
else
if
(
pid
>
0
)
{
int
p
;
if
(
debug
)
printf
(
2
,
"[%d] FORKED child %d
\n
"
,
getpid
(),
pid
);
if
(
c
>
0
&&
cmdlist
[
c
-
1
].
token
==
'|'
)
{
close
(
fdarray
[
0
]);
close
(
fdarray
[
1
]);
}
if
(
cmdlist
[
c
].
token
!=
'|'
)
{
if
(
debug
)
printf
(
2
,
"[%d] WAIT for children
\n
"
,
getpid
());
do
{
p
=
wait
();
if
(
debug
)
printf
(
2
,
"[%d] WAIT child %d finished
\n
"
,
getpid
(),
p
);
}
while
(
p
>
0
);
if
(
debug
)
printf
(
2
,
"[%d] wait finished
\n
"
,
getpid
());
}
}
}
}
}
}
int
int
ioredirection
(
void
)
ioredirection
(
void
)
{
{
int
i
,
fd
,
dfd
;
int
i
,
fd
;
for
(
i
=
0
;
i
<
next
node
;
i
++
)
{
for
(
i
=
0
;
i
<
next
io
;
i
++
)
{
switch
(
list
[
i
].
token
)
{
switch
(
io
list
[
i
].
token
)
{
case
'<'
:
case
'<'
:
if
(
close
(
0
)
<
0
)
if
(
close
(
0
)
<
0
)
printf
(
2
,
"close 0 failed
\n
"
);
printf
(
2
,
"close 0 failed
\n
"
);
if
((
fd
=
open
(
list
[
i
].
s
,
O_RDONLY
))
<
0
)
{
if
((
fd
=
open
(
io
list
[
i
].
s
,
O_RDONLY
))
<
0
)
{
printf
(
2
,
"failed to open %s for read: %d"
,
list
[
i
].
s
,
fd
);
printf
(
2
,
"failed to open %s for read: %d"
,
io
list
[
i
].
s
,
fd
);
return
-
1
;
return
-
1
;
}
}
if
(
debug
)
if
(
debug
)
printf
(
2
,
"redirect 0 from %s
\n
"
,
list
[
i
].
s
);
printf
(
2
,
"redirect 0 from %s
\n
"
,
io
list
[
i
].
s
);
break
;
break
;
case
'>'
:
case
'>'
:
if
(
close
(
1
)
<
0
)
if
(
close
(
1
)
<
0
)
printf
(
2
,
"close 1 failed
\n
"
);
printf
(
2
,
"close 1 failed
\n
"
);
if
((
fd
=
open
(
list
[
i
].
s
,
O_WRONLY
|
O_CREATE
))
<
0
)
{
if
((
fd
=
open
(
io
list
[
i
].
s
,
O_WRONLY
|
O_CREATE
))
<
0
)
{
printf
(
2
,
"failed to open %s for write: %d"
,
list
[
i
].
s
,
fd
);
printf
(
2
,
"failed to open %s for write: %d"
,
io
list
[
i
].
s
,
fd
);
exit
();
exit
();
}
}
if
(
debug
)
if
(
debug
)
printf
(
2
,
"redirect 1 to %s
\n
"
,
list
[
i
].
s
);
printf
(
2
,
"redirect 1 to %s
\n
"
,
io
list
[
i
].
s
);
break
;
break
;
}
}
}
}
...
@@ -187,16 +243,16 @@ ioredirection(void)
...
@@ -187,16 +243,16 @@ ioredirection(void)
}
}
void
void
add
node
(
int
token
,
char
*
s
)
add
io
(
int
token
,
char
*
s
)
{
{
if
(
next
node
>=
MAXNODE
)
{
if
(
next
io
>=
MAXNODE
)
{
printf
(
2
,
"add
node
: ran out of nodes
\n
"
);
printf
(
2
,
"add
io
: ran out of nodes
\n
"
);
return
;
return
;
}
}
list
[
next
node
].
token
=
token
;
io
list
[
next
io
].
token
=
token
;
list
[
next
node
].
s
=
s
;
io
list
[
next
io
].
s
=
s
;
next
node
++
;
next
io
++
;
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment