For the creation of nameless pipes:
#include <sys/stat.h>
int Mkfifo (const char* pathname,mode_ mode);
If you are in a Linux environment, you can also use the MKFIFO directive to create the pipe file directly in the shell terminal.
FIFO Common uses:
(1) FIFO is used by the shell command to transfer data from one pipe line to another without creating intermediate temporary files.
(2) FIFO is used in the client process-server process application to pass data between the client process and the server process.
The second use is mainly discussed here. (because I have never used the first use)
The first embodiment uses Mkfifo to create a FIFO pipeline file, uses it as the relay, then can realize the communication between two irrelevant processes.
Program to write Data:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <fcntl.h>
int main (int argc,char* argv[])
{
int FDW = open ("./fifo", o_wronly);//Open one end of FIFO in write form
Char buff[128] = {0};
while (1)
{
printf ("Please enter \ n");
Fgets (Buff,128,stdin);
if (strncmp (Buff, "End", 3) ==0)
{
Break
}
Write (Fdw,buff,strlen (buff));//data is written directly to FIFO
}
Close (FDW);
return 0;
}
Program to read data:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
int main (int argc,char* argv[])
{
int FDR = Open ("./fifo", o_rdonly);//opens FIFO in read form
while (1)
{
Char buff[128]= {0};
int n = read (fdr,buff,127);//Read the data in the FIFO to the buff.
if (n==0)
{
Break
}
printf ("Get buff =%s", buff);
}
Fflush (stdout);//ensure Print succeed
Close (FDR);
return 0;
}