14. Famous Pipeline communication

Source: Internet
Author: User

14. Famous Pipeline communication

Famous pipes:

Famous pipelines are also known as FIFO files, so we can operate on a well-known pipeline by manipulating the file, such as using

Open,read,write and so on.

Learning of Famous pipelines:

?

Comparison of famous pipes with other documents:

FIFO files have similarities to normal files in use, but there are differences:

1. Processes that read FIFO files can only open FIFO files in "rdonly" mode.

2. The process of writing FIFO files can only open FIFO files in "wronly" mode.

3. When the contents of the FIFO file are read, it disappears. However, the contents of the normal file are still available after reading.

?

Create a named pipe: Man 3 Mkfifo:

NAME

Mkfifo-make a FIFO Special file (a named pipe)

?

Synopsis

#include <sys/types.h>

#include <sys/stat.h>

?

int Mkfifo (const char *pathname, mode_t mode);

?

DESCRIPTION

Mkfifo () makes a FIFO special file with name pathname. mode Specifies the

FIFO ' s permissions. It's modified by the process's umask in the usual:

The permissions of the created file is (mode & ~umask).

?

A FIFO special file is similar to a pipe, except that it's created in a dif-

Ferent. Instead of being an anonymous communications channel, a FIFO spe-

cial file is entered to the file system by calling Mkfifo ().

?

Once you has created a FIFO special file in the this to, any process can open it

For reading or writing, in the same-as an ordinary file. However, it has

To being open at both ends simultaneously before you can proceed to does any input

or output operations on it. Opening a FIFO for reading normally blocks until

Some other process opens the same FIFO for writing, and vice versa. See

FIFO (7) for non-blocking handling of FIFO special files.

?

RETURN VALUE

On Success Mkfifo () returns 0. In the case of a error,-1 is returned (in

Which case, errno is set appropriately).

?

ERRORS

Eacces one of the directories in pathname do not allow search (execute) per-

Mission.

?

Eexist pathname already exists. This includes, where pathname is a

Symbolic link, dangling or not.

?

Enametoolong

Either the total length of pathname was greater than Path_max, or an

Individual filename component has a length greater than Name_max. Inch

The GNU system, there is no imposed limit on overall filename length,

But some file systems could place limits on the length of a component.

?

ENOENT a directory component in pathname does no exist or is a dangling sym-

Bolic link.

?

ENOSPC the directory or file system has no, the new file.

?

Enotdir

A component used as a directory in pathname are not, in fact, a direc-

Tory.

?

Erofs pathname refers to a read-only file system.

?

Conforming to

posix.1-2001.

?

See ALSO

Mkfifo (1), close (2), open (2), read (2), stat (2), Umask (2), write (2), mkfi-

Foat (3), FIFO (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/.

To create a named pipe function name: Mkfifo:

Prototype: int Mkfifo (const char *pathname, mode_t mode);

The function is to create a well-known pipeline, which is a FIFO file.

Required Header files:

#include <sys/types.h>

#include <sys/stat.h>

Return value: Successful return 0, failure is return-1.

Parameter description:

Pathname: The name of the FIFO file to be created, with the path.

Mode: access rights to the FIFO file created

?

To delete a named pipe:

View information: Man 2 unlink:

NAME

Unlink-delete a name and possibly the file it refers to

?

Synopsis

#include <unistd.h>

?

int unlink (const char *pathname);

?

DESCRIPTION

Unlink () deletes a name from the file system. If that name is the last link

To a file and no processes the file open, the file is deleted and the space

It is a using is made available for reuse.

?

If The name is the last link to a file and any processes still has the file

Open the file would remain in existence until the last file descriptor referring

To it is closed.

?

If the name referred to a symbolic link, the link is removed.

?

If the name referred to a socket, FIFO or device the name of the it is removed but

Processes which has the object open may continue for use it.

?

RETURN VALUE

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

appropriately.

?

ERRORS

Eacces Write access to the directory containing pathname isn't allowed for the

Process ' s effective UID, or one of the directories in pathname do not

Allow Search permission. (See also Path_resolution (7).)

?

Ebusy (not on Linux)

The file pathname cannot be unlinked because it's being used by the

System or another process and the implementation considers this an

Error.

?

Efault Pathname points outside your accessible address space.

?

EIO an I/O error occurred.

?

Eisdir pathname refers to a directory. (This is the Non-posix value returned

by Linux since 2.1.132.)

?

Eloop Too Many symbolic links were encountered in translating pathname.

?

Enametoolong

Pathname was too long.

?

ENOENT a component in pathname does no exist or is a dangling symbolic link,

or pathname is empty.

?

ENOMEM Insufficient kernel memory was available.

?

Enotdir

A component used as a directory in pathname are not, in fact, a direc-

Tory.

?

Eperm the system does not allow unlinking of directories, or unlinking of

Directories requires privileges that the calling process doesn ' t has.

(This is the POSIX prescribed error return; As noted above, Linux

Returns EISDIR for the case.)

?

Eperm (Linux only)

The file system does not allow unlinking of files.

?

Eperm or Eacces

The directory containing pathname has the sticky bit (S_ISVTX) set and

The process ' s effective UID is neither the UID of the file to being deleted

Nor that of the directory containing it, and the process are not privi-

Leged (linux:does not having the cap_fowner capability).

?

Erofs pathname refers to a file on a read-only file system.

?

Conforming to

SVR4, 4.3BSD, posix.1-2001.

?

BUGS

Infelicities in the protocol underlying NFS can cause the unexpected disappear-

Ance of files which is still being used.

?

See ALSO

RM (1), chmod (2), Link (2), Mknod (2), open (2), rename (2), RmDir (2), Unlinkat (2),

Mkfifo (3), remove (3), path_resolution (7), Symlink (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 unlink (const char *pathname);

Required header file: Unistd.h.

The function's function is to delete the file.

A successful execution returns 0, and the failure is return-1.

Parameter description:

Pathname: Used to specify the name of the file to delete (including the path).

?

Here we will know a series of operations of the pipeline. Now write two separate processes to communicate using a known pipeline. The function of a write process is to create a FIFO pipeline and write data at the same time. The read process reads the data in the FIFO and displays the data that is read out. See if the write and read are consistent.

?

?

?

?

?

?

The first implementation of the program to write a famous pipe: write.c:

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <stdio.h>

?

void Main () {

???? int FD;

?

???? reate FIFO file

???? Mkfifo ("/home/jin", 0666);

???? Open FIFO file????

???? FD = open ("/home/jin", o_wronly);

???? Write data into FIFO file

???? Write (FD, "Forfish", 8);

???? Close (FD);

?

}

?

Above is to write data to a named pipe.

?

Read.c of famous pipes:

#include <sys/types.h>

#include <sys/stat.h>

#include <stdio.h>

#include <fcntl.h>

?

void Main () {

???? Char my_buf[15];

???? int FD;

?

???? FD = open ("/home/jin", o_rdonly);

???? Read (fd,my_buf,8);

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

???? Close (FD);

???? Unlink ("/home/jin");

}

This is the readout data for a well-known pipeline.

The process of execution:

Execute write.c First: as follows, the written data of the famous pipe enters the waiting process to read out the data and enter the blocking state. This is also a feature of named pipes.

The process above, when we run the read process. When the data is read from a named pipe, the blocking state of the write process is also ended.

?

14. Famous Pipeline communication

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.