CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... ·...
Transcript of CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... ·...
![Page 1: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/1.jpg)
CS143APrinciples on Operating Systems
Discussion 02:OS Interfaces
Instructor: Prof. Anton BurtsevTA: Saehanseul Yi (Hans)
Oct 16, 2020 Noon
Revision: 3
![Page 2: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/2.jpg)
About me
• Link for all office hours/discussion:https://uci.zoom.us/j/93369206818
• Teaching staff office hours:
Hari: Mon 12:00 PSTZhaofeng Li: Tue 12:00 PSTDeep: Wed 9:00 AM PSTHans: Thu 12:00 PSTSe-Min Lim: Fri 9:00 PST
![Page 3: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/3.jpg)
Motivating example: redirection
• Best example for explaining pipe(),fork(), and exec()• Program output -> stdout (default: screen)• | (pipe operator): send outputs to somewhere else
$$ lsa.out b.out asdfasdf$
$$ ls | grep asdfasdfasdf$
![Page 4: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/4.jpg)
Typical UNIX OS
(OS)
System calls are the interface of the OS
Lecture2 slides p3
![Page 5: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/5.jpg)
But what is shell?
● Normal process
● Kernel starts it for each user that logs in into the system
● In xv6 shell is created after the kernel boots
● Shell interacts with the kernel through system calls
● E.g., starts other processes
Lecture2 slides p14
![Page 6: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/6.jpg)
System calls, interface for...
● Processes
● Creating, exiting, waiting, terminating
● Memory
● Allocation, deallocation
● Files and folders
● Opening, reading, writing, closing
● Inter-process communication
● Pipes
Lecture2 slides p5
$$ ls | grep asdfasdfasdf$
parent process (shell)
Child process(left)
Child process(right)
parent process (shell)
![Page 7: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/7.jpg)
Wait… stdin? stdout?
$$ ls | grep asdfasdfasdf$(standard input, standard output)
IN OUTProcess
stdi
n
stdout
![Page 8: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/8.jpg)
Wait… stdin? stdout?
$$ ls | grep asdfasdfasdf$(standard input, standard output)
ProcessIN OUT
stdi
n
stdout
lsIN OUT
stdi
n
stdout
grepIN OUT
stdi
n
stdout
![Page 9: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/9.jpg)
Wait… stdin? stdout?
$$ ls | grep asdfasdfasdf$(standard input, standard output)
ProcessIN OUT
stdi
n
stdout
lsIN OUT
stdi
n
stdout
grepIN OUT
stdi
n
stdout
![Page 10: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/10.jpg)
Wait… stdin? stdout?
$$ ls | grep asdfasdfasdf$(standard input, standard output)
ProcessIN OUT
stdi
n
stdout
lsIN OUT
stdi
n
stdout
grepIN OUT
stdi
n
stdout
• stdin(0), stdout(1), and stderr(2) are file descriptors(just an integer in user-program)
![Page 11: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/11.jpg)
Wait… stdin? stdout?
$$ ls | grep asdfasdfasdf$(standard input, standard output)
ProcessIN OUT
stdi
n
stdout
lsIN OUT
stdi
n
stdout
grepIN OUT
stdi
n
stdout
• stdin(0), stdout(1), and stderr(2) are file descriptors(i.e. just an integer in user-program)
• Each program has its own descriptor table
![Page 12: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/12.jpg)
Wait… stdin? stdout?
$$ ls | grep asdfasdfasdf$(standard input, standard output)
ProcessIN OUT
stdi
n
stdout
lsIN OUT
stdi
n
stdout
grepIN OUT
stdi
n
stdout
IN OUT
• stdin(0), stdout(1), and stderr(2) are file descriptors(i.e. just an integer in user-program)
• Each program has its own descriptor table• How to modify process’ file descriptors?
![Page 13: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/13.jpg)
Wait… stdin? stdout?
$$ ls | grep asdfasdfasdf$(standard input, standard output)
ProcessIN OUT
stdi
n
stdout
lsIN OUT
stdi
n
stdout
grepIN OUT
stdi
n
stdout
• stdin(0), stdout(1), and stderr(2) are file descriptors(just an integer in user-program)
• Each program has its own descriptor table• How to modify process’ file descriptors?
• close, dup(or open)
![Page 14: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/14.jpg)
Wait… stdin? stdout?
$$ ls | grep asdfasdfasdf$(standard input, standard output)
ProcessIN OUT
stdi
n
stdout
lsIN OUT
stdi
n
stdout
grepIN OUT
stdi
n
stdout
• stdin(0), stdout(1), and stderr(2) are file descriptors(just an integer in user-program)
• Each program has its own descriptor table• How to modify process’ file descriptors?
• close, dup(or open)• What we need to do:
close appropriate descriptors for each process and set the appropriate descriptor by copying
![Page 15: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/15.jpg)
Wait… stdin? stdout?
$$ ls | grep asdfasdfasdf$(standard input, standard output)
ProcessIN OUT
stdi
n
stdout
lsIN OUT
stdi
n
stdout
grepIN OUT
stdi
n
stdout
IN OUT
• stdin(0), stdout(1), and stderr(2) are file descriptors(just an integer in user-program)
• Each program has its own descriptor table• How to modify process’ file descriptors?
• close, dup(or open)• What we need to do:
close appropriate descriptors for each process and set the appropriate descriptor by copying
pipe() creates a pair of file descriptors, pointing to a pipe inode, and places them in the array pointed to by filedes. filedes[0] is for reading, filedes[1] is for writing. pipe is uni-directional
POSIX.1-2001
![Page 16: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/16.jpg)
pipe() and fork()
---------------------Point 0--------------------case PIPE:pcmd = (struct pipecmd*)cmd;if(pipe(p) < 0)
panic("pipe");---------------------Point A--------------------if(fork1() == 0){
close(1);dup(p[1]);close(p[0]);close(p[1]);
---------------------Point B--------------------runcmd(pcmd->left);
}if(fork1() == 0){
close(0);dup(p[0]);close(p[0]);close(p[1]);runcmd(pcmd->right);
}
close(p[0]);close(p[1]);---------------------Point C--------------------wait();wait();break;
$$ ls | grep asdfasdfasdf$
parent process
fork1 child(left)
fork1 child(right)
parent process
int pipe(int pipefd[2]);Create a pipe & assign each end to pipefd
pid_t fork(void);Copy the current process (parent)Returns the PID of the child (parent)or 0 (child)
![Page 17: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/17.jpg)
pipe() and fork()
---------------------Point 0--------------------case PIPE:pcmd = (struct pipecmd*)cmd;if(pipe(p) < 0)
panic("pipe");---------------------Point A--------------------if(fork1() == 0){
close(1);dup(p[1]);close(p[0]);close(p[1]);
---------------------Point B--------------------runcmd(pcmd->left);
}PA
REN
T PR
OCE
SS
0
1
2
stdin
stdout
stderr
p[0]
p[1]
※ Throughout the example, stderr is always connected to the screen. Omitted for simplicityas well as p[0] and p[1] to the parent process
![Page 18: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/18.jpg)
pipe() and fork()
---------------------Point 0--------------------case PIPE:pcmd = (struct pipecmd*)cmd;if(pipe(p) < 0)
panic("pipe");---------------------Point A-------------------if(fork1() == 0){
close(1);dup(p[1]);close(p[0]);close(p[1]);
---------------------Point B--------------------runcmd(pcmd->left);
}PA
REN
T PR
OCE
SS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]IN OUT
int p[2]
![Page 19: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/19.jpg)
pipe() and fork()
---------------------Point 0--------------------case PIPE:pcmd = (struct pipecmd*)cmd;if(pipe(p) < 0)
panic("pipe");---------------------Point A-------------------if(fork1() == 0){
close(1);dup(p[1]);close(p[0]);close(p[1]);
---------------------Point B--------------------runcmd(pcmd->left);
}
PARE
NT
PRO
CESS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]IN OUT
CHIL
D PR
OCE
SS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]
fork() copies the descriptors too!
Executed by child process
![Page 20: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/20.jpg)
pipe() and fork()
---------------------Point 0--------------------case PIPE:pcmd = (struct pipecmd*)cmd;if(pipe(p) < 0)
panic("pipe");---------------------Point A-------------------if(fork1() == 0){
close(1);dup(p[1]);close(p[0]);close(p[1]);
---------------------Point B--------------------runcmd(pcmd->left);
}
PARE
NT
PRO
CESS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]IN OUT
CHIL
D PR
OCE
SS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]
lsIN OUT
IN OUT
fork() copies the descriptors too!
Executed by child process
![Page 21: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/21.jpg)
pipe() and fork()
---------------------Point 0--------------------case PIPE:pcmd = (struct pipecmd*)cmd;if(pipe(p) < 0)
panic("pipe");---------------------Point A-------------------if(fork1() == 0){
close(1);dup(p[1]);close(p[0]);close(p[1]);
---------------------Point B--------------------runcmd(pcmd->left);
}
PARE
NT
PRO
CESS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]IN OUT
CHIL
D PR
OCE
SS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]
lsIN OUT
fork() copies the descriptors too!
Executed by child process
![Page 22: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/22.jpg)
pipe() and fork()
---------------------Point 0--------------------case PIPE:pcmd = (struct pipecmd*)cmd;if(pipe(p) < 0)
panic("pipe");---------------------Point A-------------------if(fork1() == 0){
close(1);dup(p[1]);close(p[0]);close(p[1]);
---------------------Point B--------------------runcmd(pcmd->left);
}
PARE
NT
PRO
CESS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]IN OUT
CHIL
D PR
OCE
SS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]
lsIN OUT
IN OUT
fork() copies the descriptors too!dup()’s destination is the lowest & unused file descriptor!
Executed by child process
![Page 23: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/23.jpg)
pipe() and fork()
---------------------Point 0--------------------case PIPE:pcmd = (struct pipecmd*)cmd;if(pipe(p) < 0)
panic("pipe");---------------------Point A-------------------if(fork1() == 0){
close(1);dup(p[1]);close(p[0]);close(p[1]);
---------------------Point B--------------------runcmd(pcmd->left);
}
PARE
NT
PRO
CESS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]IN OUT
CHIL
D PR
OCE
SS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]
lsIN OUT
IN OUT
fork() copies the descriptors too!dup()’s destination is the lowest & unused file descriptor!
Executed by child process
![Page 24: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/24.jpg)
pipe() and fork()
---------------------Point 0--------------------case PIPE:pcmd = (struct pipecmd*)cmd;if(pipe(p) < 0)
panic("pipe");---------------------Point A-------------------if(fork1() == 0){
close(1);dup(p[1]);close(p[0]);close(p[1]);
---------------------Point B--------------------runcmd(pcmd->left);
}
PARE
NT
PRO
CESS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]IN OUT
CHIL
D PR
OCE
SS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]
lsIN OUT
IN OUT
fork() copies the descriptors too!dup()’s destination is the lowest & unused file descriptor!
Executed by child process
![Page 25: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/25.jpg)
pipe() and fork()
---------------------Point B--------------------runcmd(pcmd>left);
}if(fork1() == 0){
close(0);dup(p[0]);close(p[0]);close(p[1]);runcmd(pcmd->right);
}close(p[0]);close(p[1]);---------------------Point C--------------------wait();wait();break;
PARE
NT
PRO
CESS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]IN OUT
CHIL
D PR
OCE
SS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]
fork() copies the descriptors too!dup()’s destination is the lowest & unused file descriptor!
Executed by child process
![Page 26: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/26.jpg)
CHIL
D PR
OCE
SS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]
pipe() and fork()
---------------------Point B--------------------runcmd(pcmd>left);
}if(fork1() == 0){
close(0);dup(p[0]);close(p[0]);close(p[1]);runcmd(pcmd->right);
}close(p[0]);close(p[1]);---------------------Point C--------------------wait();wait();break;
PARE
NT
PRO
CESS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]IN OUT
CHIL
D PR
OCE
SS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]
fork() copies the descriptors too!dup()’s destination is the lowest & unused file descriptor!
grepIN OUT
stdi
nstdout
IN OUT
Executed by child process
![Page 27: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/27.jpg)
CHIL
D PR
OCE
SS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]
pipe() and fork()
---------------------Point B--------------------runcmd(pcmd>left);
}if(fork1() == 0){
close(0);dup(p[0]);close(p[0]);close(p[1]);runcmd(pcmd->right);
}close(p[0]);close(p[1]);---------------------Point C--------------------wait();wait();break;
PARE
NT
PRO
CESS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]IN OUT
CHIL
D PR
OCE
SS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]
fork() copies the descriptors too!dup()’s destination is the lowest & unused file descriptor!
grepIN OUT
stdi
nstdout
IN OUT
Executed by child process
![Page 28: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/28.jpg)
CHIL
D PR
OCE
SS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]
pipe() and fork()
---------------------Point B--------------------runcmd(pcmd>left);
}if(fork1() == 0){
close(0);dup(p[0]);close(p[0]);close(p[1]);runcmd(pcmd->right);
}close(p[0]);close(p[1]);---------------------Point C--------------------wait();wait();break;
PARE
NT
PRO
CESS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]IN OUT
CHIL
D PR
OCE
SS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]
fork() copies the descriptors too!dup()’s destination is the lowest & unused file descriptor!
grepIN OUT
stdi
nstdout
IN OUT
Executed by child process
![Page 29: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/29.jpg)
CHIL
D PR
OCE
SS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]
pipe() and fork()
---------------------Point B--------------------runcmd(pcmd>left);
}if(fork1() == 0){
close(0);dup(p[0]);close(p[0]);close(p[1]);runcmd(pcmd->right);
}close(p[0]);close(p[1]);---------------------Point C--------------------wait();wait();break;
PARE
NT
PRO
CESS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]IN OUT
CHIL
D PR
OCE
SS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]
fork() copies the descriptors too!dup()’s destination is the lowest & unused file descriptor!
grepIN OUT
stdi
nstdout
IN OUT
Executed by child process
![Page 30: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/30.jpg)
CHIL
D PR
OCE
SS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]
pipe() and fork()
---------------------Point B--------------------runcmd(pcmd>left);
}if(fork1() == 0){
close(0);dup(p[0]);close(p[0]);close(p[1]);runcmd(pcmd->right);
}close(p[0]);close(p[1]);---------------------Point C--------------------wait();wait();break;
PARE
NT
PRO
CESS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]IN OUT
CHIL
D PR
OCE
SS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]
fork() copies the descriptors too!dup()’s destination is the lowest & unused file descriptor!
grepIN OUT
stdi
nstdout
IN OUT
Executed by child process
![Page 31: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/31.jpg)
CHIL
D PR
OCE
SS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]
pipe() and fork()
---------------------Point B--------------------runcmd(pcmd>left);
}if(fork1() == 0){
close(0);dup(p[0]);close(p[0]);close(p[1]);runcmd(pcmd->right);
}close(p[0]);close(p[1]);---------------------Point C--------------------wait();wait();break;
PARE
NT
PRO
CESS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]IN OUT
CHIL
D PR
OCE
SS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]
fork() copies the descriptors too!dup()’s destination is the lowest & unused file descriptor!
Parent waits child processes
![Page 32: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/32.jpg)
pipe() and fork()and exec()
PARE
NT
PRO
CESS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]IN OUT
CHIL
D PR
OCE
SS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]
$$ ls | grep asdfasdfasdf$
int execvp(const char *file, char *const argv[]);replaces the current process image with a new process image.
runcmd() contains exec functions
if(fork1() == 0){…runcmd(pcmd->right);
}
![Page 33: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/33.jpg)
CHIL
D PR
OCE
SS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]
pipe() and fork()and exec()
PARE
NT
PRO
CESS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]IN OUT
LS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]
$$ ls | grep asdfasdfasdf$
int execvp(const char *file, char *const argv[]);replaces the current process image with a new process image.
runcmd() contains exec functions
if(fork1() == 0){…runcmd(pcmd->right);
}
![Page 34: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/34.jpg)
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]
pipe() and fork()and exec()
PARE
NT
PRO
CESS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]IN OUT
LS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]
$$ ls | grep asdfasdfasdf$
GREPint execvp(const char *file, char *const argv[]);
replaces the current process image with a new process image.
runcmd() contains exec functions
if(fork1() == 0){…runcmd(pcmd->right);
}
![Page 35: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1](https://reader035.fdocuments.in/reader035/viewer/2022081403/60a0ddf227eddf636850ac49/html5/thumbnails/35.jpg)
GREP
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]
pipe() and fork()and exec()
PARE
NT
PRO
CESS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]IN OUT
LS
0
1
2
3
4
stdin
stdout
stderr
p[0]
p[1]
$$ ls | grep asdfasdfasdf$
int execvp(const char *file, char *const argv[]);replaces the current process image with a new process image.
runcmd() contains exec functions
if(fork1() == 0){…runcmd(pcmd->left);
}