Linux inter-process communication pipeline

Source: Internet
Author: User

In Linux, there are several main inter-process communication methods:
1) pipeline Pipe
2) signal
3) Message Queue: A chain table of messages.
4) shared memory: the most effective way of inter-process communication
5) semaphores semaphore: used for synchronization and mutual exclusion between processes and different threads of the same process.
6) Socket socket: used for communication between different machines in the Network
1. Pipe (create an unknown pipeline)
Header file # include <unistd. h>
Function Definition int pipe (INT filedes [2]);
Function Description pipe () creates an unknown pipeline and returns the file descriptor from the filedes array. filedes [0] is used to read the pipeline, and filedes [1] is used to write the pipeline.
It can only be used for communication between unrelated processes.

0 is returned for success, and-1 is returned for error.

# Include <stdio. h> # include <stdlib. h> # include <unistd. h> # include <string. h> int main (INT argc, char ** argv) {int FD [2]; char Buf [1024] = "every day is good day"; int ret = 0; if (pipe (FD) <0) {// create an unknown pipeline perror ("piple"); exit (1) ;}pid_t PID; If (pid = fork ()) = 0) {// create a sub-process ret = write (FD [1], Buf, strlen (BUF); If (Ret <0) {perror ("write"); exit (1);} printf ("write % d bytes [% s] \ n", RET, Buf );} else if (pid> 0) {sleep (1); ret = read (FD [0], Buf, sizeof (BUF )); printf ("% d bytes read from the pipe is [% s] \ n", RET, Buf); If (Ret <0) {perror ("read "); exit (1) ;}} close (FD [0]); close (FD [1]); Return 0 ;}------------------------------------------/pipe #. /test write 21 bytes [every day is good day] 21 bytes read from the pipe is [every day is good day]

2. Create a famous pipe in mafifo ()
Kfifo (create a famous Pipeline)
Header file # include <sys/types. h>
# Include <sys/STAT. h>
Function Definition int mkfifo (const char * pathname, mode_t mode );
Function Description: mkfifo () creates a special FIFO file according to the pathname parameter. This file must not exist, but the Parameter
Mode is the permission of the file (Mode &~ Umask)
Mode specifies the FIFO's permissions. It is modified by
Process's umask in the usual way: the permissions of the created file are (Mode &~ Umask)
The. umask value also affects the permissions of the FIFO file. Other processes of the FIFO file created by mkfifo () can read and write General files,
When open () is used to open a FIFO file, o_nonblock will be affected.
When o_nonblock is used, the read operation to open the FIFO file will return immediately, but if no other process opens the FIFO for reading,
Then the write operation will return the enxio error code.
If the o_nonblock file is not used, the read operation on the first-in-first-out (FIFO) file will not be returned until other processes open the first-in-first-out (FIFO) file for writing,
Similarly, operations to open a FIFO file to write data will not be returned until other processes open a FIFO file to read data.

# Include <stdio. h> # include <stdlib. h> # include <sys/types. h> # include <sys/STAT. h> # include <fcntl. h> # include <unistd. h> # include <string. h> # define FIFO "/tmp/myfifo" // famous pipe file name int main (INT argc, char ** argv) {int FD; int ret = 0; umask (0); char Buf [] = "Hello World"; char buf1 [1024]; If (mkfifo (FIFO, 07777) <0) {perror ("mkfifo "); exit (1);} pid_t PID; If (pid = fork () = 0) {FD = open (FIFO, o_wronly); ret = write (FD, Buf, strlen (BUF); printf ("write % d bytes, [% s] \ n", RET, Buf); close (FD);} else if (pid> 0) {sleep (1); FD = open (FIFO, o_rdonly); ret = read (FD, buf1, sizeof (buf1); printf ("read % d bytes, [% s] \ n ", RET, Buf); close (FD) ;}-------------------------------------------------------/myfifo #. /test write 11 bytes, [Hello world] read 11 bytes, [Hello world] second run myfifo #. /test mkfifo: file exists because the pipeline has also created myfifo # ls/tmp/myfifo

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.