Named Pipe (FIFO) communication between Linux processes Named Pipe (FIFO)

Source: Internet
Author: User

Communication between processes in Linux Named Pipe (FIFO)
Name the pipe (FIFO), which is the same as the normal pipe. It is used as an intermediate postman to implement communication between two processes.
The named pipe (FIFO) has the following features:
1. The Named Pipe (FIFO) exists as a special device file.
2. Data can be exchanged between different processes in the FIFO pipeline through the pipeline. Unlike pipe, pipe can only be used between unrelated processes.
3. After all I/O operations are performed by the process that uses the MPs queue, the named MPs queue still exists in the file system for future use.
How can we create a named pipe (FIFO?
1. We can create it using the command line method.
Mkfifo file name (the file name is the pipeline we want to create)

Pay attention to the file type and color size. In the following example, we will try again later.
2. Second, use the mkfifo function through system calls.
# Include <Stat. h>
# Include <sys/types. h>
Int mkfifo (const char * pathname, mode_t mode );
Function: Creates a named pipe.
Return Value: 0 is returned for success, and-1 is returned for failure.
Parameter: pathname is used to create a named pipe under that command.
Mode mode is the permission of the named pipe.

The following is an example:

# Include <stdio. h>
# Include <sys/STAT. h>
# Include <sys/types. h>
# Include <stdlib. h>
# Include <string. h>
# Include <fcntl. h>
# Include <sys/Wait. H>
# Include <unistd. h>
# Deprecision Len 1024
Int main (INT argc, char * argv [])
{
Int result;
Pid_t PID;
Char Buf [Len];
Int FD, flag = 0;

Unlink ("FIFO ");
Result = mkfifo ("FIFO", 0777); // The Name Of The created pipe is 755.
If (result =-1 ){
Perror ("mkfifo error:"); // print a system error message
Exit (exit_failure );
}
If (pid = fork () =-1) {// create a sub-process
Perror ("fork error :");
Exit (exit_failure );
} Else if (pid = 0) {// sub-process

FD = open ("FIFO", o_rdonly );
// Open the named pipe just created in read-only mode
While (1 ){
Read (FD, Buf, Len); // read the data in the named pipeline and put it in the Buf.
Printf ("read from pipe: % s \ n", Buf );
If (strcmp (BUF, "exit") = 0) {// If the string read is exit, exit
Exit (exit_success );
}
}

} Else {// parent process

FD = open ("FIFO", o_wronly );
// Open the named pipe just created in read-only mode
While (1 ){
Waitpid (PID, null, wnohang );
// Wait for the child process to exit the parent process to recycle resources.
If (flag = 1 ){
Exit (exit_success );
Close (FD );
}
Scanf ("% s", Buf );
Write (FD, Buf, strlen (BUF) + 1 );
// Write data to the MPs queue
If (strcmp (BUF, "exit") = 0 ){
Flag = 1; // exit flag
Sleep (1); // ensure that the sub-process exits first
}

}
}

Return 0;
}

The running result is:

The size of the first-in-first-out (FIFO) file of the MPs queue is O, which is only an intermediate medium. A tool used to implement inter-process 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.