[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]
The Linux system provides many methods for inter-process communication, such as pipelines, shared memory, and socket communication. The pipeline is very simple to use. After creating an anonymous pipeline, we only need to send data from one pipeline and then accept data from another pipeline.
# Include <stdio. h> # include <unistd. h> # include <stdlib. h> # include <string. h> int pipe_default [2]; int main () {pid_t PID; char buffer [32]; memset (buffer, 0, 32); If (pipe (pipe_default) <0) {printf ("failed to create pipe! \ N "); Return 0;} If (0 = (pid = fork () {close (pipe_default [1]); sleep (5 ); if (read (pipe_default [0], buffer, 32)> 0) {printf ("receive data from server, % s! \ N ", buffer);} Close (pipe_default [0]);} else {close (pipe_default [0]); If (-1! = Write (pipe_default [1], "hello", strlen ("hello") {printf ("send data to client, hello! \ N ");} Close (pipe_default [1]); waitpid (PID, null, 0);} return 1 ;}
Next we can start compiling and running. The old rule is divided into two steps: (1) Input GCC pipe. c-o pipe; (2) then input. /pipe. You can see the print below later.
[Test @ localhost pipe] $./pipesend data to client, hello! Receive data from server, hello!