经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C » 查看文章
c/c++ linux 进程间通信系列3,使用socketpair,pipe
来源:cnblogs  作者:小石王  时间:2018/10/20 15:33:50  对本文有异议

linux 进程间通信系列3,使用socketpair,pipe

1,使用socketpair,实现进程间通信,是双向的。

2,使用pipe,实现进程间通信

使用pipe关键点:fd[0]只能用于接收,fd[1]只能用于发送,是单向的。

3,使用pipe,用标准输入往里写。

疑问:在代码2里不写wait函数的话,父进程不能结束,但是在代码3里也没有写wait函数,父进程却可以结束???

1,使用socketpair:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/socket.h>
  6. #include <wait.h>
  7. int main(){
  8. int sv[2];
  9. pid_t pid;
  10. char buf[128];
  11. memset(buf, 0, sizeof(buf));
  12. if(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) != 0){
  13. perror("socketpair");
  14. return 1;
  15. }
  16. pid = fork();
  17. if(pid < 0){
  18. perror("fork");
  19. return 1;
  20. }
  21. if(pid == 0){
  22. close(sv[0]);
  23. read(sv[1], buf, sizeof(buf));
  24. printf("child process : data from parant process [%s]\n", buf);
  25. exit(0);
  26. }
  27. else {
  28. int status;
  29. close(sv[1]);
  30. write(sv[0], "HELLO", 5);
  31. printf("parent process : child process id %d\n", pid);
  32. wait(&status);
  33. }
  34. return 0;
  35. }

github源代码

2,使用pipe:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <wait.h>
  6. int main(){
  7. int p[2];
  8. pid_t pid;
  9. char buf[128];
  10. memset(buf, 0, sizeof(buf));
  11. if(pipe(p) != 0){
  12. perror("pipe");
  13. return 1;
  14. }
  15. pid = fork();
  16. if(pid < 0){
  17. perror("fork");
  18. return 1;
  19. }
  20. if(pid == 0){
  21. close(p[1]);
  22. read(p[0], buf, sizeof(buf));
  23. printf("child process : data form parent process [%s]\n", buf);
  24. exit(0);
  25. }
  26. else{
  27. close(p[0]);
  28. write(p[1], "aaaa", 4);
  29. printf("parent process : child process is %d\n", pid);
  30. int status;
  31. wait(&status);
  32. }
  33. return 0;
  34. }

github源代码

3,使用pipe,用标准输入往里写。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <wait.h>
  6. int main(){
  7. int p[2];
  8. pid_t pid;
  9. char buf[1024];
  10. memset(buf, 0, sizeof(buf));
  11. if(pipe(p) != 0){
  12. perror("pipe");
  13. return 1;
  14. }
  15. pid = fork();
  16. if(pid < 0){
  17. perror("fork");
  18. return 1;
  19. }
  20. if(pid == 0){
  21. printf("child process : my_id=%d\n",getpid());
  22. close(p[0]);
  23. //把标准输出给管道1了
  24. dup2(p[1], fileno(stdout));
  25. char *argv[ ]={"ls", "/home/ys/cpp/network"};
  26. //利用ls命令,往标准输出里,输入文件夹里文件的的名字,标准输出又连接到了上面开的管道1里。
  27. if(execve("/bin/ls", argv, NULL) < 0){
  28. perror("exec");
  29. return 1;
  30. }
  31. exit(0);
  32. }else{
  33. int n;
  34. FILE* filep;
  35. close(p[1]);
  36. printf("parent process : child process id=%d\n", pid);
  37. //先打开管道1
  38. filep = fdopen(p[0], "r");
  39. if(filep == NULL){
  40. perror("fdopen");
  41. return 1;
  42. }
  43. //再从管道1里读取数据
  44. while(fgets(buf, sizeof(buf), filep) != NULL){
  45. printf("get:%s\n", buf);
  46. }
  47. int status;
  48. wait(&status);
  49. }
  50. return 0;
  51. }

github源代码

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号