Data exchange between processes must pass through the kernel, and a buffer is opened in the kernel. Process 1 copies the data space to the kernel buffer, and process 2 then reads the data from the kernel buffer. In this way, inter-process communication is realized. I. Principles of inter-process communication
Data exchange between processes must pass through the kernel, and a buffer is opened in the kernel. Process 1 copies the data space to the kernel buffer, and process 2 then reads the data from the kernel buffer. In this way, inter-process communication is realized.
2. pipeline (pipe)
The space opened by calling the pipe (int filedes [2]) function in the memory is called a pipe. it reads data from one end and writes data to the other end, and transmits the two descriptors to the user program through filedes, filedes [0] points to the read end, and filedes [1] points to the write end. Therefore, read (filedes [0); or write (filedes) is used to read or write files from the pipeline.
3. process communication using pipelines
(1) the parent process creates an MPs queue and obtains two file descriptors pointing to both ends of the MPs queue,
(2) when the parent process calls fork () to create a child process, the child process also has two file descriptors pointing to both ends of the pipeline.
(3) the parent process closes the write end, the sub-process closes the read end, the sub-process writes to the pipeline, and the parent process reads data. the pipeline is implemented using a circular queue, and data is written from the write end, read from the read end to implement process communication.
-- Code -----
#include
#include
int main() { int _pipe[2]; int ret=pipe(_pipe); if(ret < 0) { printf(" pipe()"); return 1; } pid_t id=fork(); if(id < 0) { printf("fork()"); return 2; } else if(id==0) { close(_pipe[0]); char buf[]="hello world"; int count=10; while(count) { sleep(1); ssize_t _size=write(_pipe[1],buf,sizeof(buf)); if(_size < 0) { printf("write()"); return 3; } count--; } }else { close(_pipe[1]); char buf[1024]; int i=0; while(i++<10) { ssize_t _size=read(_pipe[0],buf,sizeof(buf)); if(_size >0) { buf[_size]='\n'; printf("%s\n",buf); } else { printf("child is empty or exit") ; break; } } } return 0; }
Running result:
Client
#include
#include
#include
#include
#include
#include
#include
#include
#define _PATH_ "./.tem" int main() { int fd=open(_PATH_,O_RDONLY); if(fd<0) { printf("open error"); } char buf[1024]; memset(buf,'\0',sizeof(buf)); while(1) { int ret =read(fd,buf,sizeof(buf)); if(ret>0) { buf[ret]='\n'; printf("serve said:%s\n",buf); } } close(fd); return 0; }
Serve end
#include
#include
#include
#include
#include
#include
#include
#define _PATH_ "./.tem" int main() { umask(0); int ret = mkfifo(_PATH_,S_IFIFO |0666) ; if(ret<0) { printf("mkfifo error\n"); return 1; } int fd=open(_PATH_,O_WRONLY); if(fd<0) { printf("open error"); } char buf[1024]; memset(buf,'\0',sizeof(buf)); while(1) { printf("please iput\n"); scanf("%s",buf); int ret =write(fd,buf,strlen(buf)+1); if(ret<0) { printf("write error\n"); break; } } close(fd); return 0; }
Running result:
The above is the content of the Linux-inter-process communication-pipeline. For more information, see The PHP Chinese network (www.php1.cn )!