C Language Development in Linux (pipeline Communication)

Source: Internet
Author: User

[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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.