Inter-Process Communication Series (i) Examples of pipe reading and writing

Source: Internet
Author: User

first, the pipeline read and write attention points

1. It makes sense to write to a pipe only if the end of the pipe is present; otherwise, you receive an error signal from the kernel: sifpipe

2. Writing data to the pipe does not guarantee the atomic nature of the write, the pipeline buffer has an idle area, and the write process attempts to write to it. If the read process does not read the contents of the pipe, the write process blocks.

3. When the parent-child process runs, their order is not guaranteed. So here, to ensure that the parent process closes the read descriptor, you can add sleep (2) to the child process.

Second, the example

1. Nameless Pipe

/*pipe_rw.c*/#include <unistd.h> #include <sys/types.h> #include <errno.h> #include <stdio.h>
	#include <stdlib.h> int main () {int pipe_fd[2];
	pid_t pid;
	Char buf_r[100];
	char* P_wbuf;
	int r_num;
	memset (buf_r,0,sizeof (buf_r));
	if (pipe (PIPE_FD) <0) {printf ("Pipe Create error\n");
	return-1;
		if ((Pid=fork ()) ==0)//If the child process {printf (\ n); /* Close the child process pipeline write end.
		Sleep 2 seconds to ensure that the parent process has appropriately closed the pipe read end/close (pipe_fd[1]);
		Sleep (2); /* Subprocess Reads pipe content/if ((R_num=read (pipe_fd[0],buf_r,100) >0) {printf ("%d numbers read from the pipe is%s\n", R_num,bu
		F_R);
		/* Shut down child process read-end/close (pipe_fd[0]);
  	Exit (0);
		else if (pid>0) {* * Closes the parent process read End/close (pipe_fd[0]);
		/* Two times to the pipeline to write data/if (write (pipe_fd[1), "Hello", 5)!=-1) printf ("Parent write1 success!\n");
		if (write (pipe_fd[1), "pipe", 5)!=-1) printf ("Parent write2 success!\n");
		/* Close the parent process write end and sleep for 3 seconds, let the child process read Data * * (PIPE_FD[1);
		Sleep (3);
		/* Collect child process exit information/waitpid (pid,null,0);
	Exit (0);
 }
}
The results of the operation are as follows:
[Root@localhost ipc]#/pipe_rw

parent write1 success!
Parent Write2 success!
Numbers read from the pipe is Hello pipe


2. Famous Pipe

/*fifo_write.c*/#include <sys/types.h> #include <sys/stat.h> #include <errno.h> #include < fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define FIFO_SERVER "/tmp/
	Myfifo "main" (int argc,char** argv) {int fd;
	Char w_buf[100];
	
	int nwrite; if (fd==-1) if (Errno==enxio) printf ("Open error;
	No reading process\n "); /* Open a named pipe and set it to Non-blocking/Fd=open (fifo_server,o_wronly|
	o_nonblock,0);
	if (argc==1) printf ("Please send something\n");
	strcpy (w_buf,argv[1]); /* Write String to pipe/if ((Nwrite=write (fd,w_buf,100)) ==-1) {if (Errno==eagain) printf ("The FIFO has not been read yet.")
	Please try later\n ");
else printf ("write%s to the fifo\n", w_buf); }/*fifo_read.c*/#include <sys/types.h> #include <sys/stat.h> #include <errno.h> #include <fcntl. h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define FIFO "/tmp/myfifo" main (int ar
	gc,char** argv) {char buf_r[100]; Int Fd
	
	int nread; /* Create a well-known pipe and set the appropriate permissions/if (Mkfifo (fifo,o_creat|
	O_EXCL) <0 && (errno!=eexist)) printf ("Cannot create fifoserver\n");
	
	printf ("Preparing for reading bytes...\n");
	memset (buf_r,0,sizeof (buf_r)); /* Open a well-known pipe, and set the non-blocking flag * * Fd=open (fifo,o_rdonly|
	o_nonblock,0);
		if (fd==-1) {perror ("open");	
	Exit (1);
		while (1) {memset (buf_r,0,sizeof (buf_r));
		/* Read the string in the pipeline */if ((Nread=read (fd,buf_r,100)) ==-1) {if (Errno==eagain) printf ("No Data yet\n");
		printf ("Read%s from fifo\n", buf_r);
	Sleep (1);
	} pause ();
Unlink (FIFO);


 }
Run Result:

Terminal 1:

[Root@localhost ipc]#./fifo_write 123123
write 123123 to the FIFO
Terminal 2:

[Root@localhost ipc]#./fifo_read
Preparing for reading bytes ...
Read from-  FIFO
read from
FIFO read from FIFO
read 123123 from FIFO
read from  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.