13. Nameless Pipe Communication programming

Source: Internet
Author: User

13. Nameless Pipe Communication programming

?

1. Inter-process communication:

Linux, as a typical multi-process operating system, requires the communication of information between processes and processes, which requires process communications.

2. Purpose of Process communication:

???? 1. Data transfer: One process needs to send data to another process.

???? 2. Resource sharing: Multiple processes share the same resources.

???? 3. Notification events: One process needs to send a message to another/group process to notify them of something happening.

???? 4. Process Control: Some processes want full control over the execution of another process, such as the debug process. At this point the control process wants to be able to intercept all the actions of another process and be able to know his state changes in time.

?

3. Communication development:

???? The Linux interprocess communication (ipc:interprocess communication) is developed from the following sections:

    1. UNIX inter-process communication.
    2. Inter-process communication based on System V
    3. POSIX interprocess communication.

?

4. Communication Development-posix:

POSIX (Portable Operating system Interface) represents a portable operating system interface. IEEE The Institute of Electrical and Electronics Engineers originally developed the POSIX standard to improve the portability of applications in a UNIX environment. However, POSIX is not limited to UNIX, and many other operating systems, such as Microsoft Windows, support the POSIX standard.

?

5. Means of communication:

The main ways to communicate between Linux processes are:

1, Nameless pipe (pipe)

2. Well-known pipeline (FIFO)

3. Signal (signal)

4. Message Queuing

5. Shared Memory

6. Signal Volume

7. Socket (SOCKET)

?

6. Pipeline communication:

One process writes data at the end of the pipeline, and another process reads the data from the head of the pipe. Pipelines include nameless pipes and two of known pipelines, which can only be used for communication between parent and child processes, which can be used to communicate between any two processes running on the same system.

?

7. Characteristics of Pipeline communication:

1. Pipeline communication is unidirectional, with fixed read and write ends.

2. After the data has been read out of the pipeline by the process, the data does not exist in the pipeline.

3. When the process goes to read the empty pipeline, the process blocks.

4. The process is blocked when the process is writing data to a full pipeline.

5. Pipe capacity is 64KB

?

8. Nameless Pipe:

In a Linux system, once a nameless pipe is created, manipulating the nameless pipe is the same as manipulating the file. The read end of the nameless pipe is regarded as a file, to read the data of the nameless pipe, need to find the read-end file; The write end of the nameless pipe is also considered a file, to write data to an unnamed pipe, is to manipulate the write-side file.

?

First, nameless pipe

In a Linux system, once a nameless pipe is created, manipulating the nameless pipe is the same as manipulating the file. You can therefore use functions such as read,write,close to access nameless pipes.

?

?

?

?

?

?

Nameless Pipe: Pipe

Prototype of the function: Man 2 pile:

NAME

Pipe, Pipe2-create Pipe

?

Synopsis

#include <unistd.h>

?

int pipe (int pipefd[2]);

?

#define _gnu_source

#include <unistd.h>

?

int pipe2 (int pipefd[2], int flags);

?

DESCRIPTION

Pipe () Creates a pipe, a unidirectional data channel that can is used for

interprocess communication. The array pipefd is used to return the file

Descriptors referring to the ends of the pipe. Pipefd[0] refers to the read

End of the pipe. Pipefd[1] refers to the write end of the pipe. Data written

To the write end of the pipe was buffered by the kernel until it was read from

The read end of the pipe. For further details, see Pipe (7).

?

If flags are 0, then pipe2 () is the same as pipe (). The following values can be

Bitwise ORed in the flags to obtain different behavior:

?

O_nonblock Set The O_nonblock file status flag on the new open file

Descriptions. Using This flag saves extra calls to Fcntl (2) to

Achieve the same result.

O_cloexec Set the Close-on-exec (FD_CLOEXEC) flag on the new file

Descriptors. See the description of the same flag in open (2) for

Reasons why this may useful.

?

RETURN VALUE

On success, the zero is returned. On error, 1 was returned, and errno is set

appropriately.

?

ERRORS

Efault PIPEFD is not valid.

?

EINVAL (Pipe2 ()) Invalid value in flags.

?

Emfile Too Many file descriptors is in use by the process.

?

Enfile the system limit on the total number of open files have been reached.

?

VERSIONS

Pipe2 () was added to Linux in version 2.6.27; GLIBC support is available start-

ing with version 2.9.

?

Conforming to

Pipe (): posix.1-2001.

?

Pipe2 () is linux-specific.

EXAMPLE

The following program creates a pipe, and then fork (2) s to create a child pro-

Cess; The child inherits a duplicate set of file descriptors that refer to the

Same pipe. After the fork (2), each process closes the descriptors it

doesn ' t need for the pipe (see Pipe (7)). The parent then writes the string

Contained in the program's command-line argument to the pipe, and the child

Reads this string a, byte at a, time from the pipe and echoes it in standard out-

Put.

?

#include <sys/wait.h>

#include <assert.h>

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

?

Int

Main (int argc, char *argv[])

{

int pipefd[2];

pid_t Cpid;

Char buf;

?

ASSERT (argc = = 2);

?

if (pipe (PIPEFD) = =-1) {

Perror ("pipe");

Exit (Exit_failure);

}

?

Cpid = fork ();

if (cpid = =-1) {

Perror ("fork");

Exit (Exit_failure);

}

?

if (Cpid = = 0) {/* child reads from PIPE */

Close (pipefd[1]); /* Close Unused write end */

?

while (read (pipefd[0], &buf, 1) > 0)

Write (Stdout_fileno, &buf, 1);

?

Write (Stdout_fileno, "\ n", 1);

Close (pipefd[0]);

_exit (exit_success);

?

} else {/* Parent writes ARGV[1] to pipe */

Close (pipefd[0]); /* Close Unused read end */

Write (Pipefd[1], argv[1], strlen (argv[1]));

Close (pipefd[1]); /* Reader'll see EOF */

Wait (NULL); /* Wait for child */

Exit (exit_success);

}

}

See ALSO

Fork (2), read (2), Socketpair (2), write (2), Popen (3), pipe (7)

?

Colophon

This page was part of release 3.22 of the Linux man-pages project. A descrip-

tion of the project, and information about reporting bugs, can is found at

Http://www.kernel.org/doc/man-pages/.

The prototype of the function:

int pipe (int pipefd[2]);

The function is to create a nameless pipe. The required header file is unistd.h. return value:

Successful return 0, Failure returns-1.

The function has two parameters:

The parameter is an array that has two elements, a read end, a write end, and a return value of two file descriptors.

PIPEFD[0] is the read end of the fd,pipefd[1] is the write-end of FD.

?

Example: Create a pipeline for communication between parent-child processes:

PIPE.C:

#include <unistd.h>

#include <stdio.h>

?

void Main () {

?

???? pid_t pid=0;

???? int pipefd[2];

???? Char my_buf[12];

?

???? Before fork () create a pipe

???? Pipe (PIPEFD);

?

???? Create a child process

???? pid = fork ();

???? if (pid>0) {

???????? Father write something into pipe

???????? Write (pipefd[1], "Forfish", 8);

???????? Wait ();//wait child process

???????? Close (pipefd[1]);//close pipe

???????? Exit (0);

????}

???? if (PID = = 0) {

???????? Son

???????? Read (pipefd[0],my_buf,8);

???????? printf ("Child read%s \ n", my_buf);

???????? Close (pipefd[0]);

???????? Exit (0);

????}

}

?

Operation Result:

We see the nameless pipe operation above is based on the action of the parent-child process, because once the pipeline is created, it is two files, a read-only file, and a write-side file. Our operation of the pipeline is to operate these two files.

?

?

?

13. Nameless Pipe Communication programming

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.