Talk C together (89th back: C language instance -- use pipelines for inter-process communication II)

Source: Internet
Author: User

Talk C together (89th back: C language instance -- use pipelines for inter-process communication II)

Hello, everyone. In the previous session, we talked about the example of using pipelines for inter-process communication. This example is as follows:Use MPs queues for inter-process communication, but the MPs queues are used in different ways.. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!

We introduced three types of pipelines in the previous chapter. This section describes the second type of pipelines and how to use them. The most important thing is to let everyone know how to use pipelines for inter-process communication.

The second pipeline is called the entry-level pipeline.. Before introducing it, weFirst introduce a function: pipe.

Pipe function prototype
int pipe(int file_descriptor[2])
This function is used to create an MPS queue and get two file descriptors for our use. This function has only one parameter, which is an array of the int type and has two elements in the array. Both elements are file descriptors. The MPs queue writes data to the MPs queue through the file pointed to by the second element, and reads data from the MPs queue through the file pointed to by the first element; if the function is successfully executed, 0 is returned. Otherwise,-1 is returned and the errno value is modified. Different values indicate different errors. How to Use pipe Functions

After understanding this function, weNext, we will introduce how to use the entry-level MPs queue., The details are as follows:

1. use the pipe function to create an MPS queue and obtain two file descriptors. 2. use the write system call in the sub-process to write data to the pipeline through the second file descriptor; 3. use the read system call in the parent process to read data from the pipeline through the first file descriptor;

Next we will makeThe following is a detailed code.:

Int main () {char input [] = "IPC by pipe"; char output [BUFSIZ + 1]; int fd_pipe [2]; int count = 0; int stat_value; pid_t pid, pid_res; memset (output, '\ 0', sizeof (output); if (pipe (fd_pipe) = 0) // create a pipeline {pid = fork (); if (pid> 0) {pid_res = wait (& stat_value); if (pid_res> 0) {count = read (fd_pipe [0], output, BUFSIZ); // read data from the MPs queue printf ("father process read % d characters, they are: % s \ n ", count, output) ;}} else if (pid = 0) {count = write (fd_pipe [1], input, strlen (input )); // write data to the MPs queue printf ("son process write % d characters, they are: % s \ n", count, input );} else {printf ("create process failed \ n"); return 1 ;}} return 0 ;}

From the code above, we can see that:

First, write data to the pipeline using write in the sub-process, and write data through the second file descriptor. Then, read data from the Pipeline Using read in the parent process. The first file descriptor is used to read the data. In the parent process, we also use wait to synchronize processes to ensure that after the child process writes data to the pipeline, the parent process then reads data from the pipeline.

The readers will not write the code in the body, and the detailed code will be put into my resources. You can click here to download and use it.

The following is the running result of the program. For more information, see:

. /S // run the compiled program son process write 11 characters, they are: IPC by pipe // The child process writes data to the pipeline father process read 11 characters, they are: IPC by pipe // The parent process reads data from the pipeline

For more information, see the following. I want to know what examples will be provided later, and I will try again.


Related Article

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.