进程通信 管道通信
来源:互联网 发布:空白头像软件下载 编辑:程序博客网 时间:2024/05/01 22:23
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<unistd.h>
void read_from_pipe(int fd)
{
char buf[100];
read(fd,buf,100);
printf("read from pipe: %s\n",buf);
}
void write_to_pipe(int fd)
{
char *buf="hello pipe!";
write(fd,buf,strlen(buf)+1);
printf("write into pipe: %s\n",buf);
}
int main()
{
pid_t pid,pr;
int fd[2];
int stat;
if(pipe(fd)){
printf("pipe failed!\n");
exit(0);
}
pid=fork();
switch(pid){
case -1:
printf("fork failed\n");
exit(0);
case 0:
close(fd[1]);
read_from_pipe(fd[0]);
exit(0);
default:
close(fd[0]);
write_to_pipe(fd[1]);
pr = wait(&stat);
exit(0);
}
return 0;
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<unistd.h>
void read_from_pipe(int fd)
{
char buf[100];
read(fd,buf,100);
printf("read from pipe: %s\n",buf);
}
void write_to_pipe(int fd)
{
char *buf="hello pipe!";
write(fd,buf,strlen(buf)+1);
printf("write into pipe: %s\n",buf);
}
int main()
{
pid_t pid,pr;
int fd[2];
int stat;
if(pipe(fd)){
printf("pipe failed!\n");
exit(0);
}
pid=fork();
switch(pid){
case -1:
printf("fork failed\n");
exit(0);
case 0:
close(fd[1]);
read_from_pipe(fd[0]);
exit(0);
default:
close(fd[0]);
write_to_pipe(fd[1]);
pr = wait(&stat);
exit(0);
}
return 0;
}
运行结果:
0 0
- 进程通信 管道通信
- 进程通信---管道通信
- 进程通信之管道通信
- 进程通信之管道通信
- 进程通信,管道通信 1.0
- 进程通信之管道通信
- 进程间通信-管道通信
- 【进程通信】之管道通信
- 进程间通信--管道通信
- 进程间管道通信
- 进程间通信: 管道
- 进程通信之管道
- Linux进程通信:管道
- 进程间通信--管道
- 进程间通信----管道
- Linux进程通信-管道
- 进程通信(管道)
- 进程通信-匿名管道
- 正则 2
- Spring Security hello world example
- Spring定时任务的几种实现
- linux 7z命令交叉编译移植到arm linux平台
- python 实现PCA
- 进程通信 管道通信
- QT TabBar:全面增强资源管理器(转)
- oracle 安装后需要调整内容
- hdu1013-简单模拟
- Building a Space Station 1718 (三维的 最小生成树)
- c++ 设计模式之简单的工厂模式
- C源码@数据结构与算法->LeftistHeap
- HDU 3729-I'm Telling the Truth-周赛4补题-二分图最大匹配-邻接表
- HDOJ 题目 4749 Parade Show(KMP)