关于 《UNIX网络编程--进程间通信》第四章的一个例子的疑问
来源:互联网 发布:android判断网络类型 编辑:程序博客网 时间:2024/04/30 07:05
下面是书本的源码,还有是我自己觉得需要修改的地方。我知道这书是大师写的应该没错,但是心里就是有疑问。。。。
书本源码:
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
void client(int, int), server(int, int);
void server(int readfd, int writefd);
#define MAXLINE 2048
int
main(int argc, char **argv)
{
int pipe1[2], pipe2[2];
pid_t childpid;
pipe(pipe1); /* create two pipes */
pipe(pipe2);
if ( (childpid = fork()) == 0) {/* child */
close(pipe1[1]);
close(pipe2[0]);
server(pipe1[0], pipe2[1]);
exit(0);
}
/* 4parent */
close(pipe1[0]);
close(pipe2[1]);
client(pipe2[0], pipe1[1]);
waitpid(childpid, NULL, 0);/* wait for child to terminate */
exit(0);
}
void
client(int readfd, int writefd)
{
size_t len;
ssize_t n;
char buff[MAXLINE];
/* 4read pathname */
fgets(buff, MAXLINE, stdin);
len = strlen(buff);/* fgets() guarantees null byte at end */
if (buff[len-1] == '\n')
len--; /* delete newline from fgets() */
/* 4write pathname to IPC channel */
write(writefd, buff, len);
/* 4read from IPC, write to standard output */
while ( (n = read(readfd, buff, MAXLINE)) > 0)
write(STDOUT_FILENO, buff, n);
}
void
server(int readfd, int writefd)
{
int fd;
ssize_t n;
char buff[MAXLINE+1];
/* 4read pathname from IPC channel */
if ( (n = read(readfd, buff, MAXLINE)) == 0)
//err_quit("end-of-file while reading pathname");
buff[n] = '\0';/* null terminate pathname */
if ( (fd = open(buff, O_RDONLY)) < 0) {
/* 4error: must tell client */
snprintf(buff + n, sizeof(buff) - n, ": can't open, %s\n",
strerror(errno));
n = strlen(buff);
write(writefd, buff, n);
} else {
/* 4open succeeded: copy file to IPC channel */
while ( (n = read(fd, buff, MAXLINE)) > 0)
write(writefd, buff, n);
close(fd);
}
}
接下来是我自己想修改的地方,和自己的一些理解
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#define MAXLINE 2048
//这个例子里面有一个很大的疑问,就是waitpid函数。我把waitpid函数用wait()代替放在client()函数里面,
//这样我觉得更合理,因为当父进程往管道里面写完路径名后,应该先挂起,等待子进程(服务器)把数据
//全部写到管道,接着子进程运行结束,父进程接着运行,此时管道里面已经有需要的全部数据,
//便可以读出来。编译也能正确运行。还有单步调试的时候在client函数里面的
// write(writefd, buff, len);函数运行完后,就直接跳转到子进程运行,这是什么原因
//子进程作为服务器,父进程作为客户;父进程从管道1的写入端写入数据,子进程从
//管道1的读入端读取数据;子进程从管道2的写入端写入数据,父进程从管道2的读如端
//读取数据;
void client(int, int);
void server(int, int);
int main(int argc, char **arg)
{
int pipe1[2],pipe2[2];
pid_t childpid;
pipe(pipe1);
pipe(pipe2);
if ((childpid = fork()) == 0)//child process,server
{
printf("child process");
close(pipe1[1]);
close(pipe2[0]);
server(pipe1[0], pipe2[1]);
exit(0);
}
else
{
printf("parent process\n");
close(pipe1[0]);
close(pipe2[1]);
client(pipe2[0], pipe1[1]);
exit(0);
}
}
void server(int readfd, int writefd)
{
int fd;
ssize_t n;
char buff[MAXLINE+1];
if ((n = read(readfd, buff, MAXLINE)) == 0)
{
perror("fail to read");
exit(1);
}
buff[n] == '0';
if ((fd = open(buff, O_RDONLY)) < 0)
{
snprintf(buff+n, sizeof(buff)-n, "can't open ,%s", strerror(errno));
n = strlen(buff);
write(writefd, buff, n);
}
else
{
while((n = read(fd, buff, MAXLINE)) > 0)
{
write(writefd, buff, n);
}
close(fd);
}
printf("child process");
}
void client(int readfd, int writefd)
{
size_t len;
ssize_t n;
char buff[MAXLINE];
fgets(buff, MAXLINE, stdin);
len = strlen(buff);
if (buff[len-1] == '\n')
{
len--;
}
write(writefd, buff, len);
// waitpid(childpid, NULL, 0);
wait(NULL);
while((n = read(readfd, buff, MAXLINE)) > 0)
{
write(STDOUT_FILENO, buff, n);
}
}
红色部分就是我想改的。
不知道谁能帮我解决我的疑问
- 关于 《UNIX网络编程--进程间通信》第四章的一个例子的疑问
- unix网络编程的一个多进程的例子
- unix网络编程的一个多进程的例子
- unix网络编程-的疑问
- 关于编译unix网络编程第二卷--进程间通信源代码的问题及解决
- 进程间通信的例子: socket unix
- UNIX网络编程 进程间通信概述
- 进程间通信的一个例子
- unix网络编程的一个简单例子总结
- 《unix网络编程 卷2 进程间的通信》中遇到的代码问题(ubuntu)
- unix环境编程学习笔记-----有关进程的一些疑问???
- Unix网络编程--进程间通信--管道通信
- UNIX网络编程卷2进程间通信读书笔记汇总
- UNIX网络编程卷2进程间通信读书笔记汇总
- UNIX网络编程——进程间通信概述
- 【UNIX网络编程】进程间通信之管道
- UNIX网络编程卷2进程间通信读书笔记-管道
- UNIX网络编程 卷2:进程间通信
- thoughtworksd一道编程题
- 【职场经验】程序员:伤不起的三十岁
- 岁月过歌,从一路走来
- 蓝桥杯 基础练习 数的读法
- [C++基础]位运算 游戏开发中的应用
- 关于 《UNIX网络编程--进程间通信》第四章的一个例子的疑问
- 跟我一起学extjs5(37--单个模块的设计[5取得模块列表数据])
- Android 更新升级下载 自定义Updates 兼容版
- 2014年湖南省赛赛后总结
- 理解css盒模型以及三种定位的区别
- Codeforces 112A-Petya and Strings(实现)
- .net中的索引器
- PHP开发常规安全问题总结
- android自动化单元测试