进程间的通信的管道的创建及使用
来源:互联网 发布:思迅软件v7 编辑:程序博客网 时间:2024/04/27 15:27
方式分为:
有名管道
创建方式及使用方法
#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;
}
- 进程间的通信的管道的创建及使用
- 进程间的通信 管道的使用
- 使用管道实现进程间的通信
- 进程间的通信:管道
- 进程间的通信:管道
- Linux进程间的通信--管道通信
- Perl 使用管道实现进程间的通信
- Linus进程间通信(一)管道、命名管道的原理及实现
- 父子进程间的单向管道通信
- 匿名管道--进程间的通信
- C#中进程间的通信:管道
- 2.进程间的管道通信
- 12----进程间的通信:管道
- 进程间的通信—管道
- linux进程间的通信-无名管道
- Linux的进程间通信 - 管道
- 进程间通信--管道的实现方式
- Linux的进程间通信 - 管道
- 玩转Google开源C++单元测试框架Google Test系列(gtest)之五 - 死亡测试
- 线程的作用
- 欢迎使用CSDN-markdown编辑器
- Object
- 玩转Google开源C++单元测试框架Google Test系列(gtest)之六 - 运行参数
- 进程间的通信的管道的创建及使用
- 顺时针打印矩阵
- hdu 3667 线段树区间合并
- 玩转Google开源C++单元测试框架Google Test系列(gtest)之七 - 深入解析gtest
- 一句话解释Java相关技术
- poj3376 字典树 + 扩展kmp
- 玩转Google开源C++单元测试框架Google Test系列(gtest)之八 - 打造自己的单元测试框架
- JDBC的工作原理
- 8. String to Integer (atoi)