关于 《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);
}
}



红色部分就是我想改的。

不知道谁能帮我解决我的疑问

0 0
原创粉丝点击