


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



THE UNIVERSITY
of
Prof. Dorian Arnold
CS 481: Operating System Principles
Spring ‘
2
3
execute command line in the background
redirect standard input of current command
redirect standard output of current command
redirect standard output of current
command (append if file exists)
redirect standard output of current command
to standard input of following command
4
5
6
while(1) {
char
*cmd
getcmd();
int
retval
forkforkforkfork
if(
retval
//forked
child process
exec exec
exec exec
( cmd
else {//parent
process
int
pid
retval;
wait wait
wait wait
( pid
7
“exec” is a family of functions
Use execvp()
int
execvp(char *path,
char
**argv
)
Search
path
environment
for executable
executable name
command arguments
8
main( ) {
*char newargv[4];int
i;
newargv[0]
= “ls";
newargv[1]
= “-l";
newargv[2]
= “-a”;
newargv[3]
= NULL;
i
=
execvp
(newargv[0],
newargv
);
perror("execvp() failed");exit(1);
}
This program is equivalent to:
prompt: ls
–l
–a
13
cmd
with argument
foo
and pipe
output to
cmd
executed with argument
bar
14
int
main
(
)
{
…int
fd[2];
pipe
(fd);
fork
();
if(
child )
{
close(
fd[1] );
read( fd[0], … );
}
else {
close(
fd[0] );
write(
fd[1], … );
}
}