06 - Pipes Flashcards

1
Q

Explain the pipe operation in the shell.

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Give two examples of pipe types.

A
  • File operations (read/write)
  • Processes - (related -father/son or brothers)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Describe pipe creation with relevant code.

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Describe the communication process in pipes with relevant code.

A
  • 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] or fd[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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What happens if a process attempts to read from an empty pipe?

A

read(2) will block until data is available

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What happens if a process attempts to write to a full pipe?

A

write(2) blocks until sufficient data has been read from the pipe to allow the write to complete

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can nonblocking I/O be achieved?

A

Using O_NONBLOCK status flag

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the communication channel provided by a pipe?

A

It is a byte stream, so there is no concept of message boundaries.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the major limitation of pipes?

A

Processes should be related.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to implement pipes that are accessible by other processes?

A
  • Give a name to the pipes
  • Register them in the file system
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Explain the process of closing pipes.

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

List the characteristics of pipes.

A
  • Implementation – Kernel/syscall
  • Scope - local
  • No Duplex
  • Time-coupling
  • Space-coupling +-
  • Explicit
  • Synchronization – Yes, by default
  • Process relation - related
  • Identification - NA
  • API – file operations
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Name an alternative to pipes to solve its limitations, what problem they fix and what characteristics change.

A

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()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Explain process creation.

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Explain UNIX process creation and management.

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to execute different programs with a fork?

A

Replace the code after the fork.