06 - Pipes Flashcards
Explain the pipe operation in the shell.
- Connects the standard output of one program to the standard input of another
- A chain of programs connected in this way is called a pipeline
- All the stages in a pipeline run concurrently
- Each stage waits for input on the output of the previous one
- No stage has to exit before the next can run
- It is unidirectional
- p1 | p2 p1 -> p2
- Impossible to pass information back
- Protocol for passing data is simply the receiver’s input format
Give two examples of pipe types.
- File operations (read/write)
- Processes - (related -father/son or brothers)
Describe pipe creation with relevant code.
- Pipe creation
int pipe(int pipefd[2]); int pipe2(int pipefd[2], int flags); /* O_NONBLOCK */
- Opens two files
-
fd[0]
descriptor open for reading -
fd[1]
descriptor open for writing
-
- Returns
- 0 successful
- 1 unsuccessful (
errno
variable set)
- Pipes can only connect processes with a common antecessor
- Pipe information is managed as an open file
Describe the communication process in pipes with relevant code.
- Communication (data read/write)
ssize_t read(int fd, void *buf, size_t count); ssize_t write(int fd, void *buf, size_t count);
- 1st argument is the file descriptor (
fd[0]
orfd[1]
) - 2nd argument is the data buffer address (data destination/source)
- 3rd argument number of bytes to read/write
- Return the number of bytes read/write
- Blocks or not (O_NONBLOCK)
What happens if a process attempts to read from an empty pipe?
read(2)
will block until data is available
What happens if a process attempts to write to a full pipe?
write(2)
blocks until sufficient data has been read from the pipe to allow the write to complete
How can nonblocking I/O be achieved?
Using O_NONBLOCK status flag
What is the communication channel provided by a pipe?
It is a byte stream, so there is no concept of message boundaries.
What is the major limitation of pipes?
Processes should be related.
How to implement pipes that are accessible by other processes?
- Give a name to the pipes
- Register them in the file system
Explain the process of closing pipes.
- Closing all write ends makes the read return 0
- Closing all read ends makes the write produce SIGPIPE
- After, fork processes should close not needed ends
List the characteristics of pipes.
- Implementation – Kernel/syscall
- Scope - local
- No Duplex
- Time-coupling
- Space-coupling +-
- Explicit
- Synchronization – Yes, by default
- Process relation - related
- Identification - NA
- API – file operations
Name an alternative to pipes to solve its limitations, what problem they fix and what characteristics change.
FIFOs, also called named pipes.
- Can be used by unrelated processes (Process relation - unrelated)
- Referred and identified by a file in the file system (Identification - file name)
- Created in a different way. Instead of an anonymous communications channel, FIFO is entered into the file system by calling
mkfifo()
Explain process creation.
To start a new process call:
~~~
#include <unistd.h>
pid_t fork();
~~~
- A new process is created
- The new instruction to be executed is the one following the fork (on the father and son)
- All variables are duplicated with the same value. After the fork, all changes in values are local to each process
- Fork is a system call that returns two different values
- In the son, returns 0
- In the father, return the son’s PID
Slide 19, 20, chapter 6
Explain UNIX process creation and management.
Unix process creation - two steps
- Copy of the current process
- Optional - execution of a different program (slide 17, chapter 6)
Management
- UNIX fork - system call to create a copy of the current process and start it running with no arguments
- UNIX wait - system call to wait for a process to finish