Famous pipes and nameless pipes.

Source: Internet
Author: User

1.

(1) Data transmission

A process needs to send its data to another process

(2) resource sharing

Share the same resources between multiple processes

(3) Notification events

A process needs to send a message to another or a group of processes informing them that an event has occurred

(4) Process Control

Some processes want to fully control the execution of another process (such as the debug process), at which point the control process wants to be able to intercept all operations of another process and to be able to know its state changes in a timely manner

2. Development

Linux interprocess communication (IPC) is developed from the following sections:

(1) Communication between UNIX processes

(2) based on System V interprocess communication

(3) POSIX interprocess communication

3. Classification

The ways in which Linux is now used for interprocess communication include:

(1) Nameless pipe (pipe) and famous pipe (FIFO)

(2) signal (signal)

(3) Message Queuing

(4) Shared memory

(5) Signal volume

(6) socket (socket)

 

one. Piping

1. The concept of piping

Ø The pipe is unidirectional, FIFO, which connects the output of one process with the input of another.

Ø a process (write process) writes data at the end of the pipe, and another process (read process) reads the data from the head of the pipe.

Ø data is read out by a process and will be removed from the pipeline , and other read processes will no longer be able to read the data.

Ø The pipeline provides a simple flow control mechanism, and the process blocks when the process attempts to read the empty pipe. Similarly, when the pipe is full, the process attempts to write data to the pipe, and the process blocks

Ø pipelines include nameless pipes and two of well-known pipelines , which are used for communication between parent processes and child processes, which can be used to communicate between any two processes running on the same system .

2. Nameless Pipe

Nameless Pipe creation: intpipe (int filedis[2]);

When a pipe is established, it creates two file descriptors:

Filedis[0] is used to read pipes,

Filedis[1] for writing pipes


Pipelines are used for communication between different processes. Typically, a pipe is created and a child process is created through the fork function, which inherits the pipe created by the parent process

(1) Create pipe pipe

(2) Read pipeline

(3) Write pipe

(4) Closing the pipe close

Instance:

#include <unistd.h>

#include <sys/types.h>

#include <errno.h>

#include <stdio.h>

#include <stdlib.h>

Intmain ()

{

int pipe_fd[2];

pid_t pid;

Char buf_r[100];

char* P_wbuf;

int r_num;

memset (buf_r,0,sizeof (buf_r));

/* Create pipe/*

if (pipe (PIPE_FD) <0)

{

printf ("Pipe createerror\n");

return-1;

}

/* Create Child process * *

if ((Pid=fork ()) ==0)//child process or parent process.

{

printf ("\ n");

Close (pipe_fd[1]);

Sleep (2); * * Why to Sleep *

if ((R_num=read (pipe_fd[0],buf_r,100)) >0)

{

printf ("%d numbers read from the pipe is%s\n", r_num,buf_r);

}

Close (pipe_fd[0]);

Exit (0);

}

else if (pid>0)

{

Close (pipe_fd[0]);

if (write (pipe_fd[1], "Hello", 5)!=-1)

printf ("Parent write1hello!\n");

if (write (pipe_fd[1), "pipe", 5)!=-1)

printf ("Parent write2pipe!\n");

Close (pipe_fd[1]);

Sleep (3);

Waitpid (pid,null,0); /* Wait for child process to end * *

Exit (0);

}

return 0;

}


V pipeline communication is one-way, with fixed read end and write end.

When the V data is read out of the pipeline by the process, the data does not exist in the pipeline.

V when the process goes to read the empty pipe, the process blocks.

V when the process writes data to the full pipe, the process blocks.

V Duct capacity is 64KB

#define Pipe_buffers 16

#include/linux/pipe_fs_i.h)

pipe () must be called before the system calls fork (), otherwise the child process will not inherit the file descriptor

3. Famous Pipe

Famous pipes and nameless pipes are basically the same, but there are also differences: nameless pipes can only be used by parent-child processes, but with well-known pipelines, unrelated processes can also exchange data.

#include <sys/types.h>

#include <sys/stat.h>

int Mkfifo (const char * pathname, Mode_tmode)

Pathname:fifo file name

Mode: attributes (see File Operations section)

once a FIFO is created, it can be opened with open, and general file access functions (Close, read, write, etc.) are available for FIFO

V Create pipeline Mkfifo

V Open Pipe Opening

V Reading Pipe Read

V Write pipe

V Closing pipe Close

V Remove Pipeline Unlink

Example:

fifo_write.c

#include <sys/types.h>

#include <sys/stat.h>

#include <errno.h>

#include <fcntl.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#defineFIFO_SERVER "/tmp/myfifo"

Main (intargc,char** argv)

{

int FD;

Char w_buf[100];

int nwrite;

* * Open the pipe/

Fd=open (fifo_server,o_wronly| o_nonblock,0);

if (argc==1)

{

printf ("Please sendsomething\n");

Exit (-1);

}

strcpy (w_buf,argv[1]);

/* Write data to Pipeline * *

if ((Nwrite=write (fd,w_buf,100)) ==-1)

{

printf ("The FIFO hasnot been read yet." Please try later\n ");

}

Else

printf ("Write%s to thefifo\n", w_buf);

}

FIFO_READ.C

#include <sys/types.h>

#include <sys/stat.h>

#include <errno.h>

#include <fcntl.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define FIFO "/tmp/myfifo"

Main (intargc,char** argv)

{

Char buf_r[100];

int FD;

int nread;

/* Create pipe/*

if (Mkfifo (fifo,o_creat| O_EXCL) <0) && (errno!=eexist))

printf ("Cannot createfifoserver\n");

printf ("Preparing for readingbytes...\n");

memset (buf_r,0,sizeof (buf_r));

* * Open the pipe/

Fd=open (fifo,o_rdonly| o_nonblock,0);

if (fd==-1)

{

Perror ("open");

Exit (1);

}

while (1)

{

memset (buf_r,0,sizeof (buf_r));

if ((Nread=read (fd,buf_r,100)) ==-1)

{

if (Errno==eagain)

printf ("No datayet\n");

}

printf ("Read%s fromfifo\n", buf_r);

Sleep (1);

}

Pause (); /* suspend, wait for signal * *

Unlink (FIFO); deleting files

}

Note: The difference between pipe usage and file operation.

FIFO files have similarities with common files in use, but there are also differences:

Ø the process of reading FIFO files can only open FIFO files in "rdonly" mode.

Ø the process of writing FIFO files can only be opened FIFO in "wronly" mode

The contents of the Øfifo file were read and disappeared. However, the contents of the normal file after reading still exist.

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.