Question: Pipes We have covered inter-process communication (IPC) via message passing and shared memory mechanism in the class. Another way of IPC is a pipe that
- Pipes
We have covered inter-process communication (IPC) via message passing and shared memory mechanism in the class. Another way of IPC is a pipe that is a pseudo file used for IPC, allowing data flow from one process to another. In UNIX, you can use pipes to link shell commands:
| $ command1 | command2 | command2 |
The shell arranges the standard input and output of the commands, so that
- Standard input to command1 comes from the keyboard.
- The standard output from command1 is fed to command2.
- The standard output from command2 is fed to command3.
- The standard output from command3 is fed to the terminal screen.
- Process Pipes
We can use popen and pclose to pass data between two processes:
| #include FILE * popen(const char *command, const char *type); int pclose(FILE *stream); |
A pipe is unidirectional, so the type argument may specify only reading or writing, not both; the resulting stream is correspondingly read-only or write-only.
Use "man" to study popen() and pclose().
You can read data from a pipe which "connects" to a command. Try the following program:
| //pipe1.cpp #include #include #include #include #include using namespace std; int main() { FILE *fpi; //for reading a pipe char buffer[BUFSIZ+1]; //BUFSIZ defined in
int chars_read; memset (buffer, 0,sizeof(buffer)); //clear buffer fpi = popen ("ps -auxw", "r"); //pipe to command "ps -auxw" if (fpi != NULL) { //read data from pipe into buffer chars_read = fread(buffer, sizeof(char), BUFSIZ, fpi); if (chars_read > 0) cout << "Output from pipe: " << buffer << endl; pclose (fpi); //close the pipe return 0; } return 1; } |
Requirement:
- What do you see when you execute "pipe1"? Why?
- Modify the program pipe1.cpp to pipe1a.cpp so that it accepts a command (e.g. "ls -l") from the keyboard. For example, when you execute "./pipe1a ps -auxw", it should give you the same output as pipe1.cpp. (Hint: Use string functions strcpy() and strcat() to store the commands in a buffer. Your main function should be like: int main(int argc, char *argv[]))
- The pipe Call
The lower-level pipe() function provides a means of passing data between two processes, without the overhead of invoking a shell to interpret the requested command.
| #include int pipe (int fd[2]); |
Use "man" to study more of pipe(). The function pipe() takes an array address of two integer file descriptors; it fills the array with two new file descriptors and returns 0 if successful. The two file descriptors returned are connected in a special way. Any data written to fd[1] can be read back from fd[0]. The data are processed on a first in, first out (FIFO) basis. Try the following program that illustrates this concept.
| //pipe3.cpp #include #include #include #include #include using namespace std; int main() { int nbytes; int fd[2]; //file descriptors for pipe const char s[] = "CSUSB"; char buffer[BUFSIZ+1]; memset (buffer, 0, sizeof(buffer)); //clear buffer if (pipe(fd) == 0) { //create a pipe nbytes = write(fd[1], s, strlen(s)); //send data to pipe cout << "Sent " << nbytes << " bytes to pipe." << endl; nbytes = read (fd[0], buffer, BUFSIZ); //read data from pipe cout << "Read " << nbytes << " from pipe: " << buffer << endl; return 0; } return 1; } |
Requirement:
What do you see when you execute "pipe3" ? Why?
can someone help me please using linux?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
