The pipeline of inter-process communication between Linux

Source: Internet
Author: User
Tags save file

1, interprocess communication (IPC) inter-process communication

A better understanding of the concept is that inter-process communication is the transmission or exchange of information between different processes .

Classification of IPC mechanisms under 2,linux: pipelines, signals, shared memory, message queues, semaphores, sockets

3, this article mainly say pipeline: The essence is the document, other theories what the online already has a lot of, I just write a little usage.

3.1 Features

1) Pipelines are the oldest IPC, but are seldom used at present
2) Media with documents to interact with, pipelines divided into famous pipes and nameless pipes
3) Historically, pipelines are usually referred to as half-duplex pipes.

3.2 Pipes: There are two forms, command line and non-command line

(1) command line:

Mkfifo Testfifo
echo "Testfifo" >fifo
Cat FIFO

(2) Non-command line: Here are also famous pipes and nameless pipes

programming Model : Process a Create pipeline (MKFIFO), process a write open pipeline (open), process B read open pipeline (open), process a begins to write data to the pipeline (write),

Process B (read), process a close pipeline (close), process B close Pipeline (close), delete pipeline (unlink)

Famous pipe (instance):

Process A:

#include <sys/stat.h>#include<fcntl.h>#include<stdio.h>#definePipename "Pipetest"intMain () {//Creating Pipelines    if(Mkfifo (Pipename,0666) <0) {perror ("Mkfifo"); return-1; }    //Write open Pipeline    intFD =Open (Pipename, o_wronly); if(-1==FD) {Perror ("Open"); return-1;    } unlink (Pipename); inti =0;  for(i =0; I <Ten; i++) {write (FD,&i,sizeof(i)); printf ("%d\n", i); Sleep (1);//this is suspended in seconds    }    //Close PipeClose (FD); return 0;}

Process B:

#include <sys/stat.h>#include<fcntl.h>#include<stdio.h>#definePipename "Pipetest"intMain () {//Read Open Pipeline    intFD =Open (Pipename, o_rdonly); if(-1==FD) {Perror ("Open"); return-1; }    intnum =0; inti =0;  for(i =0; I <Ten; i++) {Read (FD,&num,sizeof(int)); printf ("%d\n", num); Fflush (stdout); //force flush of output buffers} printf ("\ n");    Close (FD); return 0;}

The results are as follows:

Open another terminal, run the read process

Nameless Pipe: applies to communication between parent and child processes
int pipe (int pipefd[2]): This function creates a pipe file in the kernel, returns two file descriptors via output parameter pipefd, where pipefd[0] is used for reading ,pipefd[1] for writing .

Attention:

Process of writing data close read-pipefd[0]
Process of reading data close write end pipefd[1]
Instance:

#include <stdio.h>#include<unistd.h>#include<fcntl.h>#include<stdlib.h>intMain () {intfd[2];//used to save file descriptorspipe (FD); pid_t PID= Fork ();//Create a process    if(PID >0)    {        //Parent Process write pipeline, need to close read EndClose (fd[0]); inti =0;  for(i=Ten; i< -; i++) {Write (fd[1], &i,sizeof(int)); Sleep (1); } Close (fd[1]);//Close the Write endExit0); }    //child process Read PipelineClose (fd[1]);//Close the write end first    intx; inti =0;  for(; i<Ten; i++) {Read (fd[0], &x,sizeof(int)); printf ("%d", x);    Setbuf (stdout, NULL); } Close (fd[0]); printf ("\ n"); return 0;}

The results are as follows:

The pipeline of inter-process communication between Linux

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.