Linux下父进程通过管道发送文件名给子进程,子进程获取文件名后首先判断文件是否存在,不存在,通过管道返回错误信息,存在,将文件内容返回给父进程
来源:互联网 发布:宁夏网络干部培训学院 编辑:程序博客网 时间:2024/04/24 14:24
#include <unistd.h>
#include <string.h>
#include <stdio.h>
void pro_client(int rfd,int wfd){
char str[]="file.dat";
int n;
char buf[100];
n=strlen(str);
write(wfd,str,n);
read(rfd,buf,100);
printf("parent process:%s\n",buf);
}
void pro_server(int rfd,int wfd){
int n,fd;
char buf[1024];
n=read(rfd,buf,1024);
buf[n]='\0';
if((fd=open(buf,0))<0){
strcat(buf,"can't open.\n");
write(wfd,buf,strlen(buf));
}
else{
while((n=read(fd,buf,1024))>0)
write(wfd,buf,n);
}
}
void main(){
int pipe1[2];
int pipe2[2];
int pid;
pipe(pipe1);
pipe(pipe2);
pid=fork();
if(pid){
close(pipe1[0]);
close(pipe2[1]);
pro_client(pipe2[0],pipe1[1]);
while(wait()!=pid);
close(pipe1[1]);
close(pipe2[0]);
}
else{
close(pipe1[1]);
close(pipe2[0]);
pro_server(pipe1[0],pipe2[1]);
close(pipe1[0]);
close(pipe2[1]);
}
exit(0);
}
#include <string.h>
#include <stdio.h>
void pro_client(int rfd,int wfd){
char str[]="file.dat";
int n;
char buf[100];
n=strlen(str);
write(wfd,str,n);
read(rfd,buf,100);
printf("parent process:%s\n",buf);
}
void pro_server(int rfd,int wfd){
int n,fd;
char buf[1024];
n=read(rfd,buf,1024);
buf[n]='\0';
if((fd=open(buf,0))<0){
strcat(buf,"can't open.\n");
write(wfd,buf,strlen(buf));
}
else{
while((n=read(fd,buf,1024))>0)
write(wfd,buf,n);
}
}
void main(){
int pipe1[2];
int pipe2[2];
int pid;
pipe(pipe1);
pipe(pipe2);
pid=fork();
if(pid){
close(pipe1[0]);
close(pipe2[1]);
pro_client(pipe2[0],pipe1[1]);
while(wait()!=pid);
close(pipe1[1]);
close(pipe2[0]);
}
else{
close(pipe1[1]);
close(pipe2[0]);
pro_server(pipe1[0],pipe2[1]);
close(pipe1[0]);
close(pipe2[1]);
}
exit(0);
}
- Linux下父进程通过管道发送文件名给子进程,子进程获取文件名后首先判断文件是否存在,不存在,通过管道返回错误信息,存在,将文件内容返回给父进程
- 判断子进程是否存在
- Linux下父进程与两子进程管道通信
- 父进程下的两个子进程通过管道进行通信
- 通过管道与子进程通信
- 进程管理:通过父进程ID获取子进程ID
- 父子进程通过管道实现文件复制
- 父进程传递参数给子进程,子进程写文件存储
- [Linux] 由管道父进程向子进程发送数据 (父子间IPC)
- 管道 子进程
- 父进程给子进程传参数
- Linux 下父进程与子进程的通信(pipe管道)
- python 杀进程 判断文件是否存在 查看进程
- Linux c 匿名管道实例—创建两个子进程与父进程进行管道通信
- WIN通过子进程获取父进程ID
- Linux 获取子进程的返回值
- linux 子进程变量传递给父进程
- VC下通过进程ID获取进程镜像文件路径的方法及其存在的缺陷
- WinCE开发的编译问题
- 无线上网
- sudo 用法一二
- 堆(heap)和栈(stack),你明白了吗?
- android adb shell命令
- Linux下父进程通过管道发送文件名给子进程,子进程获取文件名后首先判断文件是否存在,不存在,通过管道返回错误信息,存在,将文件内容返回给父进程
- goagent搭建
- 八个html的小技巧
- LINUX LCD 屏幕关闭问题
- strcpy函数
- linux系统中(touch,date,find)命令总结
- 2012年最具潜力的手机应用领域
- 明天会更好
- linux课上小结