Get the App
SLTechnology News&Howtos  ›  Servers  › 

Detailed explanation of linux named pipe instance

Shulou Source: shulou.com Published: 2022-06-02 04:05:55 09月29日 Update

Linux Inter-process Communication-named pipes

FIFO (named pipe) is different from anonymous pipe in that it provides two pathnames associated with it and is stored in the component system in the form of FIFO. Named pipes are "device" pieces, so even if the process is not related to the process that created the FIFO, as long as the path is accessible, they can communicate with each other through the FIFO. It is worth noting that FIFO (first input first output) always works according to the first-in-first-out principle, and the first-written data will be read out of the pipeline first.

There are two system functions for creating named pipes: mknod and mkfifo. Both functions are defined in the header sys/stat.h. The function prototype is as follows:

# include # include int mknod (const char * path,mode_t mod,dev_t dev); int mkfifo (const char * path,mode_t mode)

In the function mknod parameter, path is the full path name of the created named pipe: mod is the mode of the created named pipe, indicating its access permission; dev is the device value, which depends on the type of device created, and it will be found only when the device component is created. Both functions return 0 for success and-1 for failure. The next "make" mknod function creates a named pipe:

Umask (0); if (mknod ("/ tmp/fifo", S_IFIFO | 0666) =-1) {perror ("mkfifo error"); exit (1);}

The first two parameters of the function mkfifo have the same meaning as mknod. The following is the example code that makes the mkfifo:

Umask (0); if (mkfifo ("/ tmp/fifo", S_IFIFO | 0666) =-1) {perror ("mkfifo error!"); exit (1);}

The following is a trial example:

Read end

# include # define PATH ". / fifo" # define SIZE 128 int main () {umask (0); if (mkfifo (PATH,0666 | S_IFIFO) = =-1) {perror ("mkefifo error"); exit (0);} int fd = open (PATH,O_RDONLY); if (fd)

Tags: Pipes functions two devices processes parameters systems paths pathnames communications identity success consanguinity code advanced principles prototypes depending on meaning form Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Redmi Linux MariaDB vpn