FIFO for inter-process communication between Linux

Source: Internet
Author: User

The previous blog has introduced a way of inter-process communication, but it is only the needle for the process of blood relationship , that is, the communication between the father and son process, that for no blood relationship process, how to communicate it?
This will create a well-known pipeline to resolve the unrelated process communication, FIFO:

[Email protected]:~$ mkfifo xwp[email protected]:~$ ls-l myfifo prw10 Feb   6   Myfifo

Mkfifo has both commands and functions.

#include <sys/types.h><sys/stat.h>int mkfifo (constchar *pathname, mode_t mode);
1 /*fifo_write.c*/2#include <stdio.h>3#include <unistd.h>4#include <stdlib.h>5#include <sys/types.h>6#include <string.h>7#include <fcntl.h>8#include <sys/stat.h>9 Ten voidSys_err (Char*STR,intExitno) One { A perror (str); - exit (EXITNO); - } the  - intMainintargcChar*argv[]) - { -     intFD, Len; +     Charbuf[1024x768] ="Hello world\n"; -     if(ARGC <2) +     { Aprintf"./app myfifo\n"); atExit1); -     } -  -FD = open (argv[1],o_wronly); -     if(FD <0) -Sys_err ("Open",1); in  - Write (FD, buf, strlen (BUF)); to Close (FD); +  -     return 0; the } *  $ Panax Notoginseng  -  the /*Fifo_read*/ +#include <stdio.h> A#include <unistd.h> the#include <stdlib.h> +#include <sys/types.h> -#include <fcntl.h> $#include <sys/stat.h> $#include <string.h> -  - voidSys_err (Char*STR,intExitno) the { - perror (str);Wuyi exit (EXITNO); the } -  Wu intMainintargcChar*argv[]) - { About     intFD, Len; $     Charbuf[1024x768] = {0}; -     if(ARGC <2) -     { -printf"./app myfifo\n"); AExit1); +     } the  -FD = open (argv[1],o_rdonly); $     if(FD <0) theSys_err ("Open",1); the  theRead (FD, BUF,sizeof(BUF)); the Write (Stdout_fileno, buf, strlen (BUF)); - Close (FD); in  the     return 0; the } About  the  the Operation Method: the compiled into executable program separately: Fifo_write and Fifo_read +Enter under a terminal.Fifo_write Myfifo -Enter it under the other terminal.Fifo_read Myfifo theYou can observe the results

Note:

    • When a FIFO pipeline is opened with write-only, the FIFO is not opened, and open write is blocked .
    • The FIFO kernel is implemented to support two-way communication . (Pipe unidirectional communication because the parent-child process shares the same file structure body)
    • FIFO can be one read end, multiple write end, also can one write end, multiple read end.

In fact, the FIFO point is still in the kernel buffer, but he points to all the kernel buffer, so he is not blocking, unlike pipe just pointing at both ends, causing blocking phenomenon

FIFO for inter-process communication between Linux

Related Article

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.