C/C++ 进程通信----管道
2023-09-11 14:14:44 时间
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int fd[2];
char str[256];
if (pipe(fd) < 0)
{
printf("create pipe failed!\n");
exit(1);
}
write(fd[1], "create the pipe successfuilly!\n", 31);
read(fd[0], str, sizeof(str));
printf("%s\n", str);
printf("pipe file descriptors are %d , %d \n", fd[0], fd[1]);
close(fd[0]);
close(fd[1]);
return 0;
}
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <limits.h>
#define BUFSIZE PIPE_BUF
void err_quit(char* msg)
{
printf("%s\n", msg);
exit(1);
}
int main()
{
int pid;
int fd[2];
char buf[BUFSIZE] = "hello my child, i am zhaoyun that is your perent\n";
int len;
if (pipe(fd) < 0)
{
err_quit("pipe failed\n");
}
//create child
if ((pid = fork() < 0))
{
err_quit("fork failed\n");
}
else if (pid > 0)
{
close(fd[0]);
write(fd[1], buf, strlen(buf));
exit(0);
}
else
{
close(fd[1]);
len = read(fd[0], buf, BUFSIZE);
if (len < 0)
{
err_quit("process failed when read a pipe\n");
}
else
{
// printf("%s\n", buf);
write(STDOUT_FILENO, buf, len);
}
exit(0);
}
return 0;
}
相关文章
- C++ HOOK 指定进程的指定 API(MessageBoxA 为例)(最简单)
- C++ 获取某进程中某模块的入口地址(也是 PE文件头 地址)
- 托管C++线程锁实现 c++11线程池
- 【转】C++中substr的用法
- 29 C++ - 友元
- 19 C++ - 类和对象的基本概念
- 11 C++ - “新增”bool类型关键字(比较C语言)
- C++程序设计:原理与实践(进阶篇)15.10 容器概览
- [C++]:万字超详细讲解多态以及多态的实现原理(面试的必考的c++考点)
- 【c++】深入学习c++中的模板
- 使用c++filt命令还原C++编译后的函数名
- C++ 无锁数据结构
- Google C++ Style Guide在C++11普及后的变化
- 《C++多线程编程实战》——2.3 解释进程模型
- 《C++多线程编程实战》——2.5 进程间通信(IPC)
- SwiftUI Swift 5.7 新功能大全,告诉什么时候Swift直接调用C++
- C与C++之间相互调用
- C++读写文本文件
- 30、【栈和队列】完全平方数(C++版)——BFS和动态规划解法
- 解决Eclipse中C++代码显示Symbol 'std' could not be resolved的问题
- C++ 线程池的使用——备忘
- C/C++教程 第十九章 —— 数据库的理解与使用