linux下的C语言开发(管道通信)
来源:互联网 发布:战旗人气协议软件 编辑:程序博客网 时间:2024/06/05 18:54
Linux系统本身为进程间通信提供了很多的方式,比如说管道、共享内存、socket通信等。管道的使用十分简单,在创建了匿名管道之后,我们只需要从一个管道发送数据,再从另外一个管道接受数据即可。 下面我们就可以开始编译运行了,老规矩分成两步骤进行:(1)输入gcc pipe.c -o pipe;(2)然后输入./pipe,过一会儿你就可以看到下面的打印了。
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- int pipe_default[2];
- int main()
- {
- pid_t pid;
- char buffer[32];
- memset(buffer, 0, 32);
- if(pipe(pipe_default) < 0)
- {
- printf("Failed to create pipe!\n");
- return 0;
- }
- if(0 == (pid = fork()))
- {
- close(pipe_default[1]);
- sleep(5);
- if(read(pipe_default[0], buffer, 32) > 0)
- {
- printf("Receive data from server, %s!\n", buffer);
- }
- close(pipe_default[0]);
- }
- else
- {
- close(pipe_default[0]);
- if(-1 != write(pipe_default[1], "hello", strlen("hello")))
- {
- printf("Send data to client, hello!\n");
- }
- close(pipe_default[1]);
- waitpid(pid, NULL, 0);
- }
- return 1;
- }
- [test@localhost pipe]$ ./pipe
- Send data to client, hello!
- Receive data from server, hello!
0 0
- linux下的C语言开发(管道通信)
- linux下的C语言开发(管道通信)
- linux下的C语言开发(管道通信)
- linux下的C语言开发(管道通信)
- linux下的C语言开发(管道通信)
- linux下的C语言开发(管道通信)
- linux下的C语言开发(管道通信)
- Linux下的C语言开发(管道通信)
- Linux下的管道通信
- Linux下的管道通信
- linux下管道通信
- Linux C 进程间的管道通信
- linux下的管道通信程序
- linux下的管道通信程序
- 一个LINUX下管道通信的例子
- Lunix下用C语言实现管道间通信
- linux C使用管道通信(一)
- Linux下进程的通信方式: 有名管道(命名管道)
- struts2 if标签和form的关系
- VNC和远程桌面的区别
- Android菜鸟的成长笔记——初识Activity
- 将指定文件夹下的所有单个文件 打包
- 源文件中有一组整数,排序后输出到另一个文件
- linux下的C语言开发(管道通信)
- android studio 安装
- 浏览器检测
- 数据结构和算法学习(7)-高级排序
- word2vec中关于词向量的阐述
- 精妙的SQL语句
- android手机源码联系人页面添加紧急电话模块
- dubbo_URL分析
- JetBrains KegGen