Famous pipes
1. View command: Man 3 Mkfifo
2. header file: #include <sys/types.h>
#include <sys/stat.h>
3. function prototype: int mkfifo (const char *pathname, mode_t mode);
A, *pathname: name of the famous pipe such as:/home/gec/myfifo
B, Mode: Octal permissions, for example: 0777
4. Return value:
Success: 0
Failed-1
5. Function Features:
A well-known pipeline exists in the file system, providing write-atomicity characteristics and high efficiency in shared memory
6, the characteristics of famous pipes:
A, with a name, stored in the ordinary file system
b, any process with appropriate permissions can use open () to obtain a FIFO file descriptor
C, as with ordinary files, read and write with read () and writ ()
D, can not use Lseek to locate
E, with write atomicity, support for multiple writers to write at the same time and data do not trample each other
(1) Read Data code: FIFO_READ.C
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
- #include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#define FIFO "/home/gec/fifo"/* Name of the famous pipe */
#define LENGTH of 50/* Character array */
int main (int argc,char **argv)
{
int ret,fd; /*ret is used to receive the value returned by the Mkfifo, FD is used to store the meal open the file descriptor of the famous pipe */
Char Buf[length];
if (Access (FIFO,F_OK)/* Determines whether a named pipe has been created, or if it has already been created, returns 0 otherwise returns a number other than 0 */
{
ret = Mkfifo (fifo,0777); /* Create a named pipe, return 0 successfully, fail to return -1*/
if (ret = =-1)/* Failed to create a named pipe */
{
Perror ("Mkfifo");
Exit (1);
}
}
FD = open (fifo,o_rdonly); /* Open a named pipe in read mode, cannot open with read and write permissions at the same time, successfully return file
Descriptor, failed to return -1*/
if (fd = =-1)/* Open failed */
{
Perror ("open");
Exit (1);
}
Puts ("Data read from a well-known pipe:");
while (1)
{
memset (buf,0,sizeof (BUF)); /* Empty buffer */
Read (fd,buf,sizeof (BUF)); /* Block reading data from a well-known pipe into BUF */
printf ("%s", buf);
if (strncmp (buf, "Q", 1) = = 0) The data in the/*BUF and the Q comparison, if equal, returns 0*/
{
Break
}
}
Close (FD); /* Close the famous pipe */
return 0;
}
(2) Write Data code: FIFO_WRITE.C
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#define FIFO "/home/gec/fifo"/* Name of the famous pipe * /
#define LENGTH of 50/* Character array * /
int main (int argc,char **argv)
{
int ret,fd;/*ret is used to receive the value returned by the Mkfifo, FD is used to store the meal open the file descriptor of the famous pipe * /
Char buf[length];
If (Access (FIFO,F_OK)/* Determines whether a named pipe has been created, or if it has already been created, returns 0 otherwise returns a number other than 0 * /
{
ret = Mkfifo (fifo,0777);/* Create a named pipe, return 0 successfully, fail back -1*/
if (ret = =-1)/* Failed to create a named pipe */
{
perror ("Mkfifo");
exit (1);
}
}
fd = open (fifo,o_wronly);/* Open a named pipe in write mode, cannot open with read and write permission at the same time, successfully return the file
descriptor, failed to return -1*/
if (fd = =-1)/* Open failed */
{
perror ("open");
exit (1);
}
puts ("Please enter data into the famous pipe, enter Q to exit:");
While (1)
{
memset (buf,0,sizeof (BUF));/* Empty buffer * /
fgets (buf,sizeof (BUF), stdin);/* input data from terminal to BUF */
Write (Fd,buf,strlen (BUF));/* Block write data to a named pipe * /
if (strncmp (buf, "Q", 1) = = 0) The data in the/*BUF and the Q comparison, if equal, returns 0*/
{
printf ("aaaa\n");
Break ;
}
}
Close (FD);/* Closes the famous pipe */
return 0;
}
Linux's famous pipeline