interprocess communication: Named pipes

Source: Internet
Author: User

First, Named Pipes (FIFO)

One limitation of anonymous pipeline applications is that they can only communicate between processes that have a common ancestor (affinity).
If we want to exchange data between unrelated processes, you can do this with a FIFO file, which is often referred to as a named pipe.

Named pipes can be created from the command line by using the following command:
$ mkfifo filename
Named pipes can also be created from a program, and related functions are:
int Mkfifo (const char *filename,mode_t mode);

Ii. Named pipes and anonymous pipelines

The anonymous pipeline is created and opened by the pipe function.
Named pipes are created by the Mkfifo function and opened with open.
The only difference between FIFO (named pipes) and pipe (anonymous pipes) is that they are created and opened in a different way, and they have the same semantics after they are completed.

The only difference between pipes and FIFOs was the manner in which they are created and opened. Once These tasks have been accomplished, I/O on pipes and FIFOs had exactly the same semantics.

Iii. opening rules for Named pipes

If the current open operation is open FIFO for read
O_nonblock disable: block until a corresponding process is written to open the FIFO
O_nonblock enable: Return to Success now
If the current open operation is open FIFO for write
O_nonblock disable: block until a corresponding process opens the FIFO for reading
O_nonblock enable: Return failure immediately, error code Enxio

#include <stdio.h>#include<stdlib.h>#include<pthread.h>#include<string.h>#include<sys/syscall.h>#include<unistd.h>#include<fcntl.h>#include<errno.h>#include<sys/types.h>#include<sys/stat.h>#ifndef T_desc#defineT_desc (x, y) (y)#endif#ifT_desc ("TU1", 1)intTu1_proc (void){    intfifo_fd; intret; Charbuffer[ -]; Mkfifo ("My_fifo",0644); FIFO_FD= Open ("My_fifo", o_wronly); printf ("fifo_fd:%d\n", FIFO_FD);  for( ; ; ) {printf ("\ n Input Write buffer:"); Fgets (Buffer,sizeof(buffer), stdin); if(STRNCMP (Buffer,"Exit",4) ==0) Break; RET= Write (fifo_fd, buffer, strlen (buffer) +1); printf ("write:%d\n", ret);    } close (FIFO_FD); Unlink ("My_fifo"); return 0;}#endif#ifT_desc ("TU2", 1)intTu2_proc (void){    intfifo_fd; intret; Charbuffer[ -]; //Mkfifo ("My_fifo", 0644);FIFO_FD = open ("My_fifo", o_rdonly); printf ("fifo_fd:%d\n", FIFO_FD);  for( ; ; ) {ret= Read (fifo_fd, buffer, -); if(Ret >0) {printf ("read:%s\n", buffer); }    }    return 0;}#endif#ifT_desc ("Global", 1)voidusage () {printf ("\ n Usage: <cmd> <tu> <p1> <...>"); printf ("\ n 1--Create thread 1"); printf ("\ n 2--Create thread 2"); printf ("\ n");}intMainintargcChar**argv) {    intret; if(ARGC <2) {usage (); return 0; }    intTu = atoi (argv[1]); if(Tu = =1) ret =Tu1_proc (); if(Tu = =2) ret =Tu2_proc (); returnret;}#endif

interprocess communication: Named pipes

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.