Presentation is loading. Please wait.

Presentation is loading. Please wait.

File redirection ls > out

Similar presentations


Presentation on theme: "File redirection ls > out"— Presentation transcript:

1 File redirection ls > out
Shell opens out, closes stdout and dups(fd of out). Man dup. Dup duplicates file descriptor to lowest unused descriptor number stdin 1) Open “out” stdout 2) close stdout out stderr 3) dup(3) out 4) close(3)

2 Exec. What we know so far.. As we know, the only way to run a new program is to use exec system call. Exec will replace the current process with the new process To keep the current process alive(running) we need to use a combination of fork and exec. The process ID, and list of open files does not change on exec() The system replaces the process spaces with the new program from disk.

3 Pipes pipes are created using the system call pipe()
Like named pipes, pipes are a FIFO byte stream between two processes Unlike named pipe, pipes require a common process ancestor Any process that writes to a pipe will hang until the data is read by another process. Any process that reads from a pipe will hang if there is no data in the pipe AND if there is any process with an open file handle to the pipe

4 file descriptor table after pipe()
Pipes are the oldest form of UNIX interprocess communication. The have the following limitations. The are uni-directional They are meant to be used between processes that have a common ancestor. ex: between parent and child, or between two children etc. stdin stdout stderr pd[0] pd[1] int pd[2]; pipe(pd)

5 pipe in one process parent: pipe() pd[0] pd[1]

6 pipe between two processes - after fork
parent: pipe() pd[0] pd[1] Child: pd[0] pd[1] fork()

7 pipe between two processes - setup!
parent: pipe() pd[0] pd[1] fork() Text Child: pd[0] pd[1] close (pd[1]) close (pd[0])

8 pipe between two commands
cat /etc/motd | grep upgrade && echo “New software installed” Parent can only get exit status of child not grand children. i.e. Parent has to fork both children. parent: pipe() fork() close.. child1: setup pipe child2: setup pipe

9 pipe between two commands
cat /etc/motd | grep upgrade && echo “New software installed” Parent can only get exit status of child not grand children. i.e. Parent has to fork both children. parent stdin stdout sterr pd[0] pd[1] child1: stdin stdout stderr pd[0] pd[1] child2: stdin stdout stderr pd[0] pd[1]

10 pipe between two commands
cat /etc/motd | grep upgrade && echo “New software installed” Parent can only get exit status of child not grand children. i.e. Parent has to fork both children. parent stdin stdout sterr pd[0] pd[1] child1: stdin stdout stderr pd[0] pd[1] child2: stdin stdout stderr pd[0] pd[1]


Download ppt "File redirection ls > out"

Similar presentations


Ads by Google