linux 下父子进程通过管道传输数据
2017-05-12 15:48
204 查看
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <fcntl.h>
int main(int argc,char* argv[])
{
int status;
int fd[2];
int pi=pipe(fd);
char *buf=(char*)malloc(sizeof(char)*20);
memset(buf,0,sizeof(char)*20);
if(pi==-1)
perror("pipe");
pid_t pid=fork();
if(pid==-1)
perror("fork");
if(pid==0){
printf("this is a child\n");
close(fd[0]);
write(fd[1],"write something",20*sizeof(char));
}else{
sleep(1);
waitpid(pid,&status,0);
printf("this is father\n");
close(fd[1]);
read(fd[0],buf,20*sizeof(char));
printf("%s\n",buf);
free(buf);
}
printf("main \n");
}
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <fcntl.h>
int main(int argc,char* argv[])
{
int status;
int fd[2];
int pi=pipe(fd);
char *buf=(char*)malloc(sizeof(char)*20);
memset(buf,0,sizeof(char)*20);
if(pi==-1)
perror("pipe");
pid_t pid=fork();
if(pid==-1)
perror("fork");
if(pid==0){
printf("this is a child\n");
close(fd[0]);
write(fd[1],"write something",20*sizeof(char));
}else{
sleep(1);
waitpid(pid,&status,0);
printf("this is father\n");
close(fd[1]);
read(fd[0],buf,20*sizeof(char));
printf("%s\n",buf);
free(buf);
}
printf("main \n");
}
相关文章推荐
- linux中IPC管道实现进程间传输数据
- [Linux] 由管道父进程向子进程发送数据 (父子间IPC)
- [Linux] 由管道父进程向子进程发送数据 (父子间IPC)
- 父子进程通过管道传递数据
- Linux 利用管道父子进程间传递数据
- Linux 利用管道父子进程间传递数据
- 进程之间如何通过消息队列传输大量数据
- 父子进程通过管道通讯------命名管道
- Linux下父进程通过管道发送文件名给子进程,子进程获取文件名后首先判断文件是否存在,不存在,通过管道返回错误信息,存在,将文件内容返回给父进程
- linux c之通过管道父子进程实现同步通信
- pipe,父子进程实现读写交换数据(linux)
- C通过管道获取LINUX进程内存占用情况
- 在 AIX 上通过数据管道实现进程间通讯
- linux下父子进程间的通信——管道
- linux进程通信之使用匿名管道进行父子进程通信
- 使用pipe()与fork()函数通过管道实现父子进程之间的通信
- linux程序通过消息队列传输数据
- UNIX环境高级编程学习之第十五章进程间通信 - 通过匿名管道实现父子进程同步
- linux系统间通过cron和ssh实现数据的自动定时传输
- UNIX环境高级编程学习之第十五章进程间通信 - 通过半双工匿名管道实现父子进程通信