Académique Documents
Professionnel Documents
Culture Documents
Sarah Diesburg
8/3/2010
COP4610 / CGS5765
1
Redirection
2
Input Redirection
3
Output Redirection
4
Combination Redirection
5
How to Implement Redirection
6
Steps for Input Redirection
7
Input Redirection Visual
fork()
shell shell
Input Redirection Visual
open(input_file, O_RDONLY)
shell shell
Input Redirection Visual
open(input_file, O_RDONLY)
shell shell
Can be any
number > 2
Input Redirection Visual
shell shell
Input Redirection Visual
dup(3)
shell shell
Input Redirection Visual
dup(3)
shell shell
Input Redirection Visual
close(3)
shell shell
Steps for Output Redirection
15
Output Redirection Visual
fork()
shell shell
Output Redirection Visual
open(output_file,
O_RDWR | O_CREAT | O_TRUNC)
shell shell
Output Redirection Visual
open(output_file,
O_RDWR | O_CREAT | O_TRUNC)
shell shell
Output Redirection Visual
dup(3)
shell shell
Output Redirection Visual
dup(3)
shell shell
Output Redirection Visual
close(3)
shell shell
Note on Redirection
23
Pipes
24
Pipes
command1 | command 2
25
Pipes
26
Pipe
int p1_to_p2[2];
pipe(p1_to_p2);
file descriptors
0 stdin
1 stdout
2 stderr
shell
Pipe
int p1_to_p2[2];
pipe(p1_to_p2);
file descriptors
0 stdin
1 stdout
2 stderr
3 p1_to_p2[0]
4 p1_to_p2[1]
shell
Pipe
fork()
shell shell
Pipe
fork()
execv(bin_path, execv(bin_path,
argv) argv)
shell shell shell
Pipe
46
Background Processes
47
Waiting Problem
48
Background example main loop
int num_processes=0;
int status=0;
/* Main loop */
{
if((pid = fork())==0) {
*/child executes long processes*/
} else {
num_processes++;
if(waitpid(-1, &status, WNOHANG) > 0) {
num_processes--;
}
}
} /* End main loop */
49
What about on exit?
50
Foreground Execution
fork()
waitpid(-1, &status, 0)
Shell Shell
Foreground Execution
fork()
Shell Shell
Foreground Execution
fork()
waitpid(-1, &status, 0)
Shell Shell
Background Execution
fork()
waitpid(-1, &status,
WNOHANG)
Shell Shell
Background Execution
fork()
Shell Shell
Background Execution
fork()
waitpid(-1, &status,
WNOHANG)
Shell Shell
Summary
57
Next Recitation
Introduction of project 2
Kernel modification!
58
Reminders
59