Linux下父进程通过管道发送文件名给子进程,子进程获取文件名后首先判断文件是否存在,不存在,通过管道返回错误信息,存在,将文件内容返回给父进程
2011-12-20 18:56
696 查看
#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
='\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
='\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 获取文件时间信息 判断文件是否存在
- 通过文件锁实现,程序开始运行时,先判断文件是否存在,若存在则表明该程序已经在运行了,如果不存在就用open函数创建该文件,程序退出时关闭文件并删除文件
- 根据文件获取文件夹,判断文件夹是否存在,不存在则创建
- 【转】通过文件锁实现,程序开始运行时,先判断文件是否存在,若存在则表明该程序已经在运行了,如果不存在就用open函数创建该文件,程序退出时关闭文件并删除文件
- JAVA之旅(二十八)——File概述,创建,删除,判断文件存在,创建文件夹,判断是否为文件/文件夹,获取信息,文件列表,文件过滤
- JAVA之旅(二十八)——File概述,创建,删除,判断文件存在,创建文件夹,判断是否为文件/文件夹,获取信息,文件列表,文件过滤
- C# 通过HttpWebRequest 创建连接获取文件大小 判断Http类型的文件是否存在
- QT cmd判断进程是否存在 获取进程信息 关闭进程
- C++ 获取文件夹内的文件名,判断文件(夹)是否存在的两个函数
- 通过文件锁实现,程序开始运行时,先判断文件是否存在,若存在则表明该程序已经在运行了,如果不存在就用open函数创建该文件,程序退出时关闭文件并删除文件。
- linux 判断某个进程是否存在,存在则重启,不存在则启动。
- php 在linux 用file_exists() 函数判断 另外一台服务器映射过来的文件是否存在 总是返回false
- QT cmd判断进程是否存在 获取进程信息 关闭进程
- 判断XML文件是否存在,不存在就创建一个XMl文件,存在就在里面添加一条数据
- linux判断文件是否存在
- Python:判断文本中的用户名在数据库中是否存在,存在返回1,不存在返回0
- Java 简单解决springmvc获取properties文件里面中文内容出现论码问题(我遇到的问题是数据提交检验时返回的错误提示出现乱码)
- 在主函数中提示用户输入用户名和密码。另写一方法来判断用户输入是否正确。该方法分别返回一个bool类型的登录结果和和一个string类型的登录信息。如登录成功,返回true及“登录成功”,若登录失败则返回false及“用户名错误”或“密码错误”(使用out参数)
- Linux中用C++通过管道来获取命令行输出的内容
- IOS开发中判断文件是否存在,不存在则拷贝