Named Pipes (FIFO)

Source: Internet
Author: User

First, Named pipes: two unrelated processes are associated by a path name, that is, you can implement interprocess communication (first in, first out) as long as you can access the path.

Second, create the function prototype: int mkfifo (const Char*path, mode_t mode); Successful return 0, failure return-1

Third, the code implementation:

Write End

#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <string.h> #include <fcntl.h> #include <stdlib.h> #define _PATH_ "/tmp/fifo.tmp" #define _size_ 1024int Main () {umask (0); int ret = Mkfifo (_path_, s_ififo|0666), if (ret = =-1) {perror ("Mkfifo"); return 1;} int fd = open (_path_, O_RDWR), if (FD < 0) {perror ("open");} Char Buf[_size_];memset (buf, ' n ', sizeof (BUF)), while (1) {scanf ("%s", buf), int ret = write (FD, buf, strlen (BUF) +1); RET < 0) {perror ("write"); if (strncmp (buf, "Quit", 4) = = 0) {break;}} Close (FD); return 0;}

Read End

#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <string.h> #include <fcntl.h> #include <stdlib.h> #define _PATH_ "/tmp/fifo.tmp" #define _size_ 1024int Main () {int fd = open (_path_, O_RDWR), if (FD < 0) {perror ("open"); return 1;} Char Buf[_size_];memset (buf, ' buf ', sizeof (BUF)), while (1) {int ret = read (FD, buf, sizeof (<=)), if (ret perror 0) {(" Read "); break;} printf ("%s\n", buf); if (strncmp (buf, "Quit", 4) = = 0) {break;}} Close (FD); return 0;}

Iv. Realization of communication:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7F/19/wKiom1cTSH3hGiNxAABKT996ffI556.png "style=" float: none; "title="]bf]m9f_7 ' _s0j (6[][l (xb.png "alt=" Wkiom1ctsh3hginxaabkt996ffi556.png "/>

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7F/16/wKioL1cTSTvzFeS-AAA7h8pgeII130.png "style=" float: none; "Title=" NN (8l]u9umx%8~e (v2rvkh4.png "alt=" Wkiol1ctstvzfes-aaa7h8pgeii130.png "/>


Named Pipes (FIFO)

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.