Linux Learning Notes (12)-interprocess communication | pipeline

Source: Internet
Author: User
Tags anonymous

There are several ways to communicate between processes in Linux, including, pipelines, signals, semaphores, shared memory, Message Queuing and sockets ...

Now start learning from each other!

——————————————————————————————————————————————————

A pipeline is a data channel in which a process links to another process, which usually passes the output of one process to the input of another process to pass the data.

On a Linux terminal, use a single vertical bar | To show, then, what can this symbol do?

For a chestnut, if I use the ps-ef command, I can see all of my current processes:

  

As indicated, there are too many things to show, dazzling, if you want to find the process in which you want, such as to see if the sshd service is started, it will definitely find me vomiting blood.

In another way, it would be much simpler to use a pipe ...

First, with the ps-ef command, his output is a bunch of process lists, and I use this output as input to other commands, such as the grep command, so I can easily find the process I want.

 Ps-ef|grep sshd

Here's a try ...

  

So it is clear that the process, with the keyword sshd process has been filtered out, but also painted red. At a glance, my sshd service has started.

————————————————————————————————————————————————————————————————————————

  The pipeline is divided into anonymous pipe and named pipe Two kinds, anonymous pipeline mainly used in two have blood relationship between the process of communication, such as father and Son, Sun ...

The named pipes are mainly used for communication between two unfamiliar processes.

An anonymous pipe is a half-duplex pipe, half-duplex means that, at the same time, he can only process a message or a trusted one, unable to simultaneously.

  

Pipeline This function, if the performance in the program, then need to use the following function:

  int pipe (int pipefd[2])

Where parameter pipefd[2] is an array of file descriptor properties, which represents opening both ends of the pipe, where pipefd[0] is read, pipefd[1] is written .

Now write a program, the parent process uses the pipeline to send a signal to the child process, after the child process receives, returns a signal.

Because of a small error, this program has been written for nearly one hours ...

IMPORTANT: The pipe reads read and writes write, both of these functions are blocking operations!

Focus: Be sure to build a pipeline before the process is created.

Focus on the focus: the anonymous pipeline is half-duplex, before the communication begins, be sure to close the unwanted channel, after the end of the communication, must also perform the shutdown.

  In the anonymous pipeline, the read process and the write process initially will have [0],[1], two descriptors each set, write the program must pay attention to in the write processing inside close (pi[0]), in the reading process close (pi[1]), after the writing process write task execution finish must close (Pi[1] ), the read process must close (Pi[0]) after reading.

Otherwise there will be an infinite blocking problem, and I spent one hours on this issue ...

  Question: When an anonymous pipe is closed, can it be reopened again? For example, after using open (Pi2[1]), continue to send data?

I looked for a long time in the evening, but did not see this description, I feel that the pipeline can not be a one-time, right? That's too extravagant!!

#include <sys/wait.h>#include<stdio.h>#include<stdlib.h>#include<unistd.h>#include<string.h>intMainintargcChar*argv) {    intpi1[2]; intpi2[2];    pid_t pid_test; Charbuff[ -]; Charbuff2[ -]={"son: Hello, I'm a son!"}; Charbuff1[ -]= {"Lao Tzu: Hello, I am Lao Tzu! "}; if(pipe (PI1) = =-1) {printf ("Pipe # 1th creation failed! \ n"); Exit (0); }    if(pipe (PI2) = =-1) {printf ("failed to create pipe number 2nd! \ n"); } pid_test=Fork (); if(Pid_test <0) {printf ("failed to create child process! \ n"); Exit (0); }    if(Pid_test = =0) {printf ("I am a son, named:%d, my father is:%d\n", Getpid (), Getppid ()); Write (Stdout_fileno,"\ n",1); Close (pi1[1]);  while(Read (pi1[0],buff,1) >0) {write (Stdout_fileno,buff,1); } write (Stdout_fileno,"\ n",1); Close (pi1[0]); printf ("my son is finished! \ n"); printf ("my son is finished! \ n"); Sleep (2); Close (pi2[0]); Write (pi2[1],buff2,strlen (BUFF2)); Close (pi2[1]); printf ("my son's finished! \ n");        Exit (exit_success); Exit (0); }    Else{printf ("I am the father! My ID is:%d, my son's id is:%d\n", Getpid (), pid_test); Sleep (2); Close (pi1[0]); Write (pi1[1],buff1,strlen (buff1)); Close (pi1[1]); printf ("I've finished my writing! \ n"); Close (pi2[1]);  while(Read (pi2[0],buff,1) >0) {write (Stdout_fileno,buff,1); } write (Stdout_fileno,"\ n",1); printf ("I'm done reading! \ n"); Close (pi2[0]);        Wait (NULL); Exit (0); }    return 0;}

The result of the code execution is as follows:

The program is OK!!

  

Linux Learning Notes (12)-interprocess communication | pipeline

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.