Linux IPC (Inter-Process Communication, Inter-process Communication) pipeline learning, ipcinter-Process

Source: Internet
Author: User

Linux IPC (Inter-Process Communication, Inter-process Communication) pipeline learning, ipcinter-Process
1. the standard Stream pipeline operation supports the file stream mode, which is used to create the pipeline linking another process. This blog introduces the function popen and pclosepopen in detail. 2. unknown pipeline (PIPE) features: 1) communication can only be performed between kinship processes (Parent and Child or brother) 2) Half Duplex (fixed read end and fixed write end) 3) it is a special file. You can use read, write, and so on. In the memory, the pipeline function prototype is: # include <unistd. h> int pipe (int fds [2]); the pipeline is represented by a pair of file descriptors in the program. One is a readable attribute, and the other is a writable attribute: fds [0] Read, fds [1] WriteThe two processes must have an inheritance relationship.To inherit the opened file descriptor.

Int main () {int fds [2]; if (pipe (fds) =-1) {perror ("pipe:"); return 0 ;} char buf [1024] = ""; if (fork () = 0) {close (fds [1]); while (memset (buf, 0, sizeof (buf) {if (read (fds [0], buf, 1024) = 0) {// exit break when there is no data in the pipeline ;} printf ("child: read:"); puts (buf);} exit (1);} else {close (fds [0]); // char p [1024]; // char * p = "Hello world! "; While (memset (buf, 1024), fgets (buf, stdin )! = NULL) write (fds [1], buf, 1024); close (fds [1]); printf ("parents, finish \ n"); wait (NULL ); // wait must be placed behind close because the fds [1] of the parent process is disabled first, and the child process exits only when no data is read, otherwise it will lead to deadlocks }}

3. The Named Pipe (FIFO) is an unknown pipe that can only be used in pipelines with great restrictions on inter-process communication. The famous pipe has broken through this restriction, the Process Communication 3.1 Creation is achieved by specifying the path name paradigm. Deleting a FIFO file creates a FIFO file, which is similar to creating a common file, only the created file FIFO creates the function prototype of the FIFO file # include <sys/types. h> # include <sys/stat. h> int mkfifo (const char * pathname, mode_t mode); parameter pathname is the full path name of the FIFO file to be created; parameter mode is the file access permission creation, and 0 is returned, failed to create:-1 The function prototype for deleting the FIFO file is int unlink (const char * pathname). Example:
#include<iostream>using namespace std;int main(int argc,char *argv[]){if(mkfifo(argv[1],0666)==-1){perror("ew");return 0;}unlink(argv[1]);}
3.2 enable or disable a FIFO file. The open and close functions are used for opening/closing more common files of the FIFO type. Use O_WRONLY to open the write end of the FIFO, and use the O_RDONLY option to open the read end of the FIFO. The write read end can be opened by several processes at the same time. Fd_recv = open (argv [1], O_RDONLY );
Fd_send = open (argv [2], O_WRONLY );
If (fd_send =-1 ){
Perror ("fd_send ");
Exit (1 );
}
3.3 read/write FIFO: fd_recv = open (argv [1], O_RDONLY );
Char buf_recv [1024] = "";
If (read (fd_recv, buf_recv, 1024 )! = 0) write (1, buf_recv, strlen (buf_recv); close (fd_recv) write: fd_send = open (argv [2], O_WRONLY );
Char buf_send [1024] = "";
If (fgets (buf_send, 1024, stdin )! = NULL) write (fp_send, buf_send, strlen (buf_send); close (fd_send); program Demonstration: pork_send.c and pork_recv.c use fork to open up a process, and two programs can chat.
/*************************************** * *********************************> File Name: pork_send.c> Author: yang> Mail: 826123027@qq.com> Created Time: friday, August 22, 2014, 19:54:08 ************************************* * **********************************/# include <stdio. h> # include <string. h> # include <stdlib. h> # include <sys/types. h> # include <sys/stat. h> # include <fcntl. h> # include <memory. h> int main (int argc, char * Argv []) {if (mkfifo (argv [1], 0666) =-1) {perror ("mk1_o1"); exit (1);} int fd_send, fd_recv; fd_send = open (argv [1], O_WRONLY); fd_recv = open (argv [2], O_RDONLY); char buf_recv [1024] = ""; char buf_send [1024] = ""; printf ("opening! \ N "); if (fork () = 0) {// open a process to accept information close (fd_send); while (memset (buf_recv, 0, sizeof (buf_recv), read (fd_recv, buf_recv, 1024)> 0) {write (1, buf_recv, strlen (buf_recv);} close (fd_recv ); exit (1);} close (fd_recv); while (memset (buf_send, 0, sizeof (buf_send), fgets (buf_send, 1024, stdin )! = NULL) {write (fd_send, buf_send, strlen (buf_send); // used to send information} close (fd_send); wait (NULL );}

/*************************************** * *********************************> File Name: pork_recv.c> Author: yang> Mail: 826123027@qq.com> Created Time: friday, August 22, 2014, 20:12:32 ************************************* * *********************************/# include <string. h> # include <stdlib. h> # include <sys/types. h> # include <sys/stat. h> # include <fcntl. h> # include <memory. h> # include <stdio. h> int main (int argc, char * Argv []) {if (mkfifo (argv [2], 0666) =-1) {perror ("mk1_o2"); exit (1);} int fd_send, fd_recv; fd_recv = open (argv [1], O_RDONLY); // note that this location is different from that of the above program. fd_send = open (argv [2], O_WRONLY ); char buf_recv [1024] = ""; char buf_send [1024] = ""; printf ("opening! \ N "); if (fork () = 0) {// accept information close (fd_send); while (memset (buf_recv, 0, sizeof (buf_recv )), read (fd_recv, buf_recv, 1024)> 0) {write (1, buf_recv, strlen (buf_recv);} close (fd_recv); exit (1 );} close (fd_recv); // send message while (memset (buf_send, 0, sizeof (buf_send), fgets (buf_send, 1024, stdin )! = NULL) {write (fd_send, buf_send, strlen (buf_send);} close (fd_send); wait (NULL );}








In linux, how do two processes of c implement communication? One process sends messages to another process, and the other receives and displays the messages.

Process communication in linux is divided into three parts: low-level communication, pipeline communication and inter-process communication IPC (inter process communication ). Linux Low-level communication is mainly used to transmit the control signals of processes-File locks and Soft Interrupt signal mechanisms. Linux inter-process communication IPC has three parts: ① semaphore, ② shared memory and ③ message queue. The following is the C language implementation code for linux Process Communication. The operating system is redhat9.0, the editor is vi, and the compiler uses gcc. All the following implementation code has passed the test and runs correctly.

1. Low-level communication-signal Communication

Signal. c

# Include <signal. h>
# Include <stdio. h>
# Include <unistd. h>

/* Execute the pre-defined action function after the signal sig is captured */
Void sig_alarm (int sig)
{
Printf ("--- the signal encoded Ed is % d./n", sig );
Signal (SIGINT, SIG_DFL); // SIGINT terminal interrupt signal, SIG_DFL: Restores default behavior, SIN_IGN: ignores signal
}

Int main ()
{
Signal (SIGINT, sig_alarm); // capture terminal interruption Signals
While (1)
{
Printf ("waiting here! /N ");
Sleep (1 );
}
Return 0;
}

2. Pipeline Communication

Pipe. c

# Include <stdio. h>
# Define BUFFER_SIZE 30

Int main ()
{
Int x;
Int fd [2];
Char buf [BUFFER_SIZE];
Char s [BUFFER_SIZE];
Pipe (fd); // create an MPS queue
While (x = fork () =-1); // when the pipeline fails to be created, it enters the loop

/* Enter the sub-process, and the sub-process writes a string to the pipeline */
If (x = 0)
{
Sprintf (buf, "This is an example of pipe! /N ");
Write (fd [1], buf, BUFFER_SIZE );
Exit (0 );
}

/* Enter the parent process, and the parent process reads the string just written from the other end of the Pipeline */
Else
{
Wait (0); // wait until the child process ends
Read (fd [0], s, BUFFER_SIZE); // read the string and store it in char s []
Printf ("% s", s); // print the string
}
Return 0;
}

3. inter-process communication-IPC

① Semaphore Communication

Sem. c

# Include <unistd. h>
# Include <stdlib. h>
# Include <stdio. h>
# Include <sys/types. h>
# Include <sys/ipc. h>
# Include <sys/sem. h>

/* Federated variable */
Union semun
{
Int val; & ...... the remaining full text>

What is ipc?

1 Processor

IPC (instruction per clock)
In fact, the frequency and IPC really affect the CPU performance. Accurate CPU performance judgment criteria should be: CPU performance = IPC (number of instructions executed by the CPU in each clock cycle) × frequency (MHz clock speed ), this formula was initially proposed by Intel and widely recognized by the industry.
2. inter-process communication

IPC (Inter-Process Communication) provides methods for Inter-Process Communication. There are several methods in Linux C Programming
(1) half-duplex Unix Pipeline
(2) FIFOs (Named Pipe)
(3) Message Queue
(4) semaphores
(5) shared memory
(6) network Socket

3. CMD command
4. Committee
International Olympic Committee for the disabled
5. International Patent Classification
IPC = International Patent Classification International Patent category
6. Institute of Physical and Chemical Technology, Chinese Emy of Sciences
The Technical Institute of Physics and Chemistry of the Chinese Emy of Sciences (IPC)
7. An important product in the history of Apple iPod Audio Player, full name: iPod classic.
8. Publications
9. Camera
10. company name.
Which one do you want ??

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.