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)