进程间的通信的管道的创建及使用
2016-02-22 17:16
197 查看
方式分为:
有名管道
创建方式及使用方法
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#define FIFO "/home/farsight/aaa"
char *buf = "hello b.c!\n";
int main()
{
int ret;
int fd;
ret = mkfifo(FIFO, 0777);
if (-1 == ret) {
if (EEXIST == errno) {
if ((fd = open(FIFO, O_WRONLY)) < 0) {
perror("open fifo failed!\n");
return -1;
}
printf("fd : %d\n", fd);
}
else {
perror("mkfifo failed!");
return -1;
}
}
if ((fd = open(FIFO, O_WRONLY)) < 0) {
perror("open fifo failed!\n");
return -1;
}
if (-1 == write(fd, buf, strlen(buf) + 1)) {
perror("write: ");
return -1;
}
exit(0);
}
——————————————————————————————————————
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#define FIFO "/home/farsight/aaa" //绝对路径
int main()
{
int ret;
int fd;
char buf[128] = {0};
ret = mkfifo(FIFO, 0777); //创建管道
if (-1 == ret) { //出现错误时判断是否是EEXIST=errno,如果是就打开文件;
if (EEXIST == errno) {
if ((fd = open(FIFO, O_RDONLY)) < 0) {
perror("open fifo failed!\n");
return -1;
}
printf("fd : %d\n", fd);
}
else {
perror("mkfifo failed!");
return -1;
}
}else {
if ((fd = open(FIFO, O_RDONLY)) < 0) { //没出现错误就打开文件。
perror("open fifo failed!\n");
return -1;
}
}
if (-1 == read(fd, buf, 128)) { // 读入文件。
perror("read: ");
return -1;
}
printf("b.c : %s\n", buf);
exit(0);
}
——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
无名管道
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int main()
{
int fd[2];
int fd1[2];
pid_t pid;
if (pipe(fd) == -1) {
perror("pipe ");
return -1;
}
if (pipe(fd1) == -1) {
perror("pipe ");
return -1;
}
pid = fork();
if (-1 == pid) {
perror("fork ");
return -1;
} else if (0 == pid) {//child
close(fd[0]);
close(fd1[1]);
char buf[128] = {0};
if (-1 == write(fd[1], "aaaa", strlen("aaaa")+1)){
perror("child process write failed!\n");
}
if (-1 == read(fd1[0], buf, 128)) {
perror("child process read failed!\n");
exit(-1);
}
printf("child process: %s\n", buf);
exit(0);
} else {//father
close(fd[1]);
close(fd1[0]);
char buf[128] = {0};
if (-1 == read(fd[0], buf, 128)) {
perror("child process read failed!\n");
}
printf("father process: %s\n", buf);
if (-1 == write(fd1[1], "11111111", strlen("11111111")+1)){
perror("child process write failed!\n");
exit(-1);
}
exit(0);
}
printf("%d, %d\n", fd[0], fd[1]);
return 0;
}
有名管道
创建方式及使用方法
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#define FIFO "/home/farsight/aaa"
char *buf = "hello b.c!\n";
int main()
{
int ret;
int fd;
ret = mkfifo(FIFO, 0777);
if (-1 == ret) {
if (EEXIST == errno) {
if ((fd = open(FIFO, O_WRONLY)) < 0) {
perror("open fifo failed!\n");
return -1;
}
printf("fd : %d\n", fd);
}
else {
perror("mkfifo failed!");
return -1;
}
}
if ((fd = open(FIFO, O_WRONLY)) < 0) {
perror("open fifo failed!\n");
return -1;
}
if (-1 == write(fd, buf, strlen(buf) + 1)) {
perror("write: ");
return -1;
}
exit(0);
}
——————————————————————————————————————
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#define FIFO "/home/farsight/aaa" //绝对路径
int main()
{
int ret;
int fd;
char buf[128] = {0};
ret = mkfifo(FIFO, 0777); //创建管道
if (-1 == ret) { //出现错误时判断是否是EEXIST=errno,如果是就打开文件;
if (EEXIST == errno) {
if ((fd = open(FIFO, O_RDONLY)) < 0) {
perror("open fifo failed!\n");
return -1;
}
printf("fd : %d\n", fd);
}
else {
perror("mkfifo failed!");
return -1;
}
}else {
if ((fd = open(FIFO, O_RDONLY)) < 0) { //没出现错误就打开文件。
perror("open fifo failed!\n");
return -1;
}
}
if (-1 == read(fd, buf, 128)) { // 读入文件。
perror("read: ");
return -1;
}
printf("b.c : %s\n", buf);
exit(0);
}
——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
无名管道
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int main()
{
int fd[2];
int fd1[2];
pid_t pid;
if (pipe(fd) == -1) {
perror("pipe ");
return -1;
}
if (pipe(fd1) == -1) {
perror("pipe ");
return -1;
}
pid = fork();
if (-1 == pid) {
perror("fork ");
return -1;
} else if (0 == pid) {//child
close(fd[0]);
close(fd1[1]);
char buf[128] = {0};
if (-1 == write(fd[1], "aaaa", strlen("aaaa")+1)){
perror("child process write failed!\n");
}
if (-1 == read(fd1[0], buf, 128)) {
perror("child process read failed!\n");
exit(-1);
}
printf("child process: %s\n", buf);
exit(0);
} else {//father
close(fd[1]);
close(fd1[0]);
char buf[128] = {0};
if (-1 == read(fd[0], buf, 128)) {
perror("child process read failed!\n");
}
printf("father process: %s\n", buf);
if (-1 == write(fd1[1], "11111111", strlen("11111111")+1)){
perror("child process write failed!\n");
exit(-1);
}
exit(0);
}
printf("%d, %d\n", fd[0], fd[1]);
return 0;
}
相关文章推荐
- log4j.properties配置详解与实例
- 腾讯股票行情获取源代码
- Android应用开发经常使用知识
- javascript 性能检测
- Android之进程通讯——广播
- Android 6.0 开发者对系统权限的使用与练习(Permissions Best Practices)
- nagios 日常维护
- 读取nagios数据库至MySQL
- Eclipse 调试Hadoop java.lang.ClassNotFoundException问题解决
- 顺时针打印矩阵
- linux实战(八)----定时运行----实例运用
- 8. String to Integer (atoi)
- linux实战(八)----定时运行----实例运用
- Cocos2dx-lua 播放音循环播放问题
- MVC,MVP 和 MVVM 的图示
- GDKOI2016 游记
- MVC————前台中,冒号与等号的区别
- 玩转Google开源C++单元测试框架Google Test系列(gtest)之八 - 打造自己的单元测试框架
- poj3376 字典树 + 扩展kmp
- 玩转Google开源C++单元测试框架Google Test系列(gtest)之七 - 深入解析gtest