Communication effects between processes
1. Data transfer: A process needs to upload his data to other processes
2. Resource Sharing
3. Process notification Events
4, Process Control: Some processes completely control the execution of another process, such as debug State AH
We need to take full control of his every step of the operation;
Communication development History
The communication IPC between Linux processes evolved from the following sections:
1. Communication between UNIX processes
2. Communication between processes based on System V
3. Communication between POSIX processes (portable operating system interface)
Now the process of communication between Linux uses includes;
1. Pipelines and famous pipelines
2. Signal
3. Message Queuing
4. Shared Memory
5. Signal Volume
6. Socket SOCKET
Category: Nameless pipes and well-known pipelines: The former is used for communication between parent and child processes, which can be used to run on the same system
int pipe (int filedis[2]) function is created:
When a pipeline is established, he creates two file descriptors:
Filedis[0] for read pipelines, filedis[1] for writing pipelines.
Reading and writing of pipelines
Pipelines are used for communication between different processes. Typically, you create a pipe first by using the fork function
Creates a child process that inherits the pipeline created by the parent process.
Call the pipe () before calling fork (), otherwise the child process will not inherit the file descriptor.
Implementation of/****************** nameless pipe *******************************/
#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));
/* pipl*/before creating a pipe fork
if (pipe (PIPE_FD) <0)
{
printf ("Pipe Create error\n");
return-1;
}
/* Create Child process */
if ((Pid=fork ()) ==0)//child process or parent process?
{
printf ("\ n");
Close (pipe_fd[1]);
Sleep (2); /* Why sleep */
if ((R_num=read (pipe_fd[0],buf_r,100)) >0)
{
printf ("%d numbers read from the pipe is%s\n", r_num,buf_r);
}
Close (pipe_fd[0]);
Exit (0);
}
Else if (pid>0)
{
close (pipe_fd[0]);
if (Write (pipe_fd[1], "Hefrfro", 5)!=-1)
printf ("Parent write1 hello!\n");
if (Write (pipe_fd[1], " degree VVFVF ", 5)!=-1)
printf ("Parent Write2 Chang ge!\n ");
close (pipe_fd[1]);
sleep (3);
waitpid (pid,null,0); /* Wait for child process to end */
exit (0);
}
return 0;
}
Name (FIFO)
#include <sys/types.h>
#include <sys/stat.h>
int Mkfifo (const char *pathname,mode_t MoD)
Pathname:fifo file name
Mode: Once the FIFO is created, you can open it with open, and close read Write can
Non-blocking (O_nonblock) when FIFO is turned on
will have the following effects on future reads and writes
1, do not use O_nonblock: When the access process can not be satisfied, the process will block, or immediately error.
/****************** Famous Pipe reading operation ***************************/
#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 argc,char** argv)
{
Char buf_r[100];
int FD;
int nread;
/* Create pipeline */
if (Mkfifo (fifo,o_creat| O_EXCL) <0) && (errno!=eexist))//fifo pipeline Path
printf ("Cannot create fifoserver\n");
printf ("Preparing for Reading bytes...\n");
memset (buf_r,0,sizeof (buf_r)); Allocating memory for Buf_r
/* Open Pipe */
Fd=open (fifo,o_rdonly| o_nonblock,0); If no data is open, the anti-go home
if (fd==-1)
{
Perror ("open");
Exit (1);
}
while (1)
{
memset (buf_r,0,sizeof (buf_r));
if ((Nread=read (fd,buf_r,100)) ==-1) If the pipeline data is not data, print the No, data yet
{
if (Errno==eagain)
printf ("No Data yet\n");
}
printf ("Read%s from fifo\n", buf_r);
Sleep (1);
}
Pause (); /* Pause, wait for signal */
Unlink (FIFO); deleting files
}
/************** Famous Pipe write operation *************************/
#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 " //Note that both pipelines are the same address.
Main (int argc,char** argv)//if you dont understand this looking before articles
{
int FD;
Char w_buf[100]; Define the cpcity of Shu zu
int nwrite;
/* Open Pipe */
Fd=open (fifo_server,o_wronly| o_nonblock,0);
if (argc==1)
{
printf ("Please send something\n");
Exit (-1);
}
strcpy (w_buf,argv[1]); Put the input parameters into the BUF;
/* Write data to Pipe */
Nwrite=write (fd,w_buf,100)); Can be added without!me not using!
if ((Nwrite=write (fd,w_buf,100)) ==-1)//Here to write this function synthesis, if not accustomed to be able to create a separate row
{
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);
}
end!
A well-known pipeline of communication between processes, nameless pipe (pipe), notes