IPC---piping

Source: Internet
Author: User


1. Establishing a pipeline of data flows between two cities


2. Can be unidirectional or bidirectional


3. Similar to files, but when the data is read out, there is no information in the pipeline.


4. Anonymous half-duplex pipe:


ls | grep *, the output of LS is the input of grep, the anonymous half-duplex pipeline is just the resource of the system, but there is no real name, it is impossible to see the pipeline in the file system as any file


The process ends up being purged by the system


5.

  
 
  1. #include<unistd.h>

  2. #include<stdio.h>

  3. #include<stdlib.h>

  4. int main()

  5. {

  6. int fd[2];

  7. if(pipe(fd)==-1)

  8. {

  9. perror("create pipe fail");

  10. exit(-1);

  11. }

  12. char wbuf[] = "Hello,I am pipe!";

  13. if(write(fd[1],wbuf,sizeof(wbuf))==-1)

  14. {

  15. perror("write fail");

  16. }

  17. char rbuf[sizeof(wbuf)];

  18. if(read(fd[0],rbuf,sizeof(wbuf))==-1)

  19. {

  20. perror("read fail");

  21. }

  22. puts(rbuf);

  23. close(fd[0]);

  24. close(fd[1]);

  25. return 0;

  26. }


Note: Two file descriptors are not associated with any known file, there is no actual file


Pipe function Prototype:


 
   
  
  1. #inlcude<unistd.h>

  2. int pipe(int fd[2])

  3. 成功返回0,反之为-1


Anonymous pipe limit:


1. Single direction transmission information, fd[1] can only be write end, fd[0] can only be read end


2. Anonymous pipelines apply only to blood-related processes, such as parent-child processes and sibling processes


3. When the pipe read end is closed and the writing end is still written, the sigpipe signal is generated and the program is interrupted.


4. When the end of the pipeline is closed and the read is still read, the Read function returns the number of bytes read from the pipe and returns 0 if there is no data in the pipeline, instead returning the read word


Number of knots


6. Pipeline for inter-parent interprocess communication


The parent process sends information to the child process:


  
 
  1. #include<stdio.h>
  2. #include<unistd.h>
  3. #include<string.h>
  4. int main()
  5. {
  6. int fd[2];
  7. pipe(fd);
  8. pid_t pid = fork();
  9. char buf[6]= "Hello";
  10. char rbuf[6]={0};
  11. if(pid!=0)
  12. {
  13. close(fd[0]);
  14. write(fd[1],buf,6);
  15. close(fd[1]);
  16. }
  17. else if(pid==0)
  18. {
  19. close(fd[1]);
  20. read(fd[0],rbuf,6);
  21. puts(rbuf);
  22. close(fd[0]);
  23. }
  24. return 0;
  25. }

The child process sends information to the parent process:


  
 
  1. #include<stdio.h>
  2. #include<unistd.h>
  3. #include<string.h>
  4. int main()
  5. {
  6. int fd[2];
  7. pipe(fd);
  8. pid_t pid = fork();
  9. char buf[6]= "Hello";
  10. char rbuf[6]={0};
  11. if(pid==0)
  12. {
  13. close(fd[0]);
  14. write(fd[1],buf,6);
  15. close(fd[1]);
  16. }
  17. else if(pid!=0)
  18. {
  19. close(fd[1]);
  20. read(fd[0],rbuf,6);
  21. puts(rbuf);
  22. close(fd[0]);
  23. }
  24. return 0;
  25. }








From for notes (Wiz)

IPC---piping

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.