You are on page 1of 13

Topics

Pipes fork overview Pipe :Introduction Redirecting Input and Output Piping programs combining fork and pipe

Overview
Suppose a program has if part and else part. The program can execute either if part or else part. Program pip1.c But while we use fork(), we can make both part works. Program pip1.c Refresh of fork()

Contd

contd...
Students show the flow of execution for program 2 From the output: Which is executed first Parent or child?

Pipes
Pipes are a mechanism for IPC Pipes are used by shell to connect one utility std output to std input of another utility 1. Command line 2. Though program: using system call Command line: The output of one is given as input to other. Redirects the output of one command to the input of another command.

Getting info about files


file gives the content types of the specified files (e.g., text, binary, directory, program) file /bin/* e.g: file /home/networklab2 file /home/networklab2/two.c wc counts the number of words, lines and characters in a file -l for lines -c for characters

Pipe redirection
Cat

e.g:ls -alf|sort -r|more ls -alf = page by page list all files sort -r=arrange descending more = view % by %

Rules
1.Child reads using read(). After all data has been read, read() returns zero. 2.If a process reads from an empty pipe, it sleeps until some input becomes available. 3.If process reads more bytes than available byte, read() reads all available bytes & returns the number of bytes read. 4.If a process writes to a pipe,whose read end has been closed and if the write operation fails, the writer process receives a ?

Regular expressions
grep pattern <pathname...>

Hel in myfile if it has an upper case H followed by lower case ric e.g : grep Hel /home/prathap/CS/unixlab1/two

pipes
A process creates a pipe and uses this pipe to exchange information with its child. Disadvantages: 1. Half duplex 2. They can be used by processes which have a common ancestor

pipes
int pipe() returns 0 when succesful and returns -1 otherwise. Creates a pipe and returns two file descriptors fd[0] is open for reading fd[1] is open for writing The output of fd[1] becomes input for fd[0] Hence pipe is unidirectional one way communication channel between processes.

pipes
Pipe with fork: Program pip3.c using pipe() Logic: child uses write() to write the stirng to the buffer Parent reads the string from the buffer using read() So parent is reader, child is writer.

Thank You