LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1274|回复: 11

读过Unix Network Programming volum 1, third edition 的请进来看一下(请教关于cha

[复制链接]
发表于 2006-10-24 23:07:52 | 显示全部楼层 |阅读模式

  1. 5.9  Handling SIGCHLD Signals
  2. ..............................
  3. ............................
  4. solaris % tcpserv02 &
  5. start server in background

  6. [2] 16939
  7.    
  8. solaris % tcpcli01 127.0.0.1
  9. then start client in foreground

  10. hi there
  11. we type this

  12. hi there
  13. and this is echoed

  14. ^D
  15. we type our EOF character

  16. child 16942 terminated
  17. output by printf in signal handler

  18. accept error: Interrupted system call
  19. main function aborts



  20. The sequence of steps is as follows:

  21. 1,We terminate the client by typing our EOF character. The client TCP sends a FIN to the server and the server responds with an ACK.

  22. [color="Red"]2,The receipt of the FIN delivers an EOF to the child's pending readline. The child terminates.[/color]

  23. 3,The parent is blocked in its call to accept when the SIGCHLD signal is delivered. The sig_chld function executes (our signal handler), wait fetches the child's PID and termination status, and printf is called from the signal handler. The signal handler returns.

  24. 4,Since the signal was caught by the parent while the parent was blocked in a slow system call (accept), the kernel causes the accept to return an error of EINTR (interrupted system call). The parent does not handle this error (Figure 5.2), so it aborts.


复制代码

在5.9节中有这样的一段话,特别是我用红色标出来的,我觉得有问题. 因为child调用的是str_echo, str_echo调用的是read的函数,根本没有调用readline,child怎么会pending在readline这个函数上?
我在http://www.unpbook.com/上拿了 ... 调用这个函数. 所以我很困惑!不知道看过这个的兄弟有没有这样的经历?难道是我看错了?

(没有看过的可能不知道我在讲什么)
 楼主| 发表于 2006-10-24 23:10:26 | 显示全部楼层
If we compile this program—Figure 5.2, with the call to Signal, with our sig_chld handler—under Solaris 9 and use the signal function from the system library (not our version from Figure 5.6), we have the following:

solaris % tcpserv02 &
start server in background

[2] 16939
   
solaris % tcpcli01 127.0.0.1
then start client in foreground

hi there
we type this

hi there
and this is echoed

^D
we type our EOF character

child 16942 terminated
output by printf in signal handler

accept error: Interrupted system call
main function aborts



The sequence of steps is as follows:

We terminate the client by typing our EOF character. The client TCP sends a FIN to the server and the server responds with an ACK.

[color="Red"]The receipt of the FIN delivers an EOF to the child's pending readline. The child terminates.

The parent is blocked in its call to accept when the SIGCHLD signal is delivered. The sig_chld function executes (our signal handler), wait fetches the child's PID and termination status, and printf is called from the signal handler. The signal handler returns.

Since the signal was caught by the parent while the parent was blocked in a slow system call (accept), the kernel causes the accept to return an error of EINTR (interrupted system call). The parent does not handle this error (Figure 5.2), so it aborts.


(第一回发这么长的, 格式上不会控制,请见谅!)
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-10-24 23:13:40 | 显示全部楼层
If we compile this program—Figure 5.2, with the call to Signal, with our sig_chld handler—under Solaris 9 and use the signal function from the system library (not our version from Figure 5.6), we have the following:

solaris % tcpserv02 &                                  start server in background

[2] 16939
   
solaris % tcpcli01 127.0.0.1                         then start client in foreground

hi there                                                       we type this
hi there                                                       and this is echoed

^D                                                               we type our EOF character
child 16942 terminated                             output by printf in signal handler

accept error: Interrupted system call          main function aborts



The sequence of steps is as follows:

We terminate the client by typing our EOF character. The client TCP sends a FIN to the server and the server responds with an ACK.

[color="DarkRed"]The receipt of the FIN delivers an EOF to the child's pending readline. The child terminates.

The parent is blocked in its call to accept when the SIGCHLD signal is delivered. The sig_chld function executes (our signal handler), wait fetches the child's PID and termination status, and printf is called from the signal handler. The signal handler returns.

Since the signal was caught by the parent while the parent was blocked in a slow system call (accept), the kernel causes the accept to return an error of EINTR (interrupted system call). The parent does not handle this error (Figure 5.2), so it aborts.


(这下应该好了,实在对不起啊!)
回复 支持 反对

使用道具 举报

发表于 2006-10-25 10:20:51 | 显示全部楼层
可能是版本不同,或者确实印刷错误。

不过,应该不影响这部分的理解吧。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-10-25 11:02:06 | 显示全部楼层
书上写的我可以理解,但跟这段说明前面运行的程序代码对不上,而且现象也不同,child收到FIN后好像并没有产生SIGCHLD的信号.因为sig_chld()函数没有执行.(用Ethereal看过包的收发)
  1. void
  2. sig_chld(int signo)
  3. {
  4.         pid_t        pid;
  5.         int                stat;

  6.         pid = wait(&stat);
  7.         printf("child %d terminated\n", pid);
  8.         return;
  9. }
复制代码


而且后面一节关于waitpid的也没有成功的显示出书上的现象.
我看的是电子版的,third edition,我跟第二版比过,相应的地方也是这样写的.
回复 支持 反对

使用道具 举报

发表于 2006-10-25 15:06:34 | 显示全部楼层

呵呵我在看中文版的

呵呵我在看中文版的,现在看到第十六章了,非常愿意和在看和看过这本书的朋友认识一下,互相学习和探讨心得,在这留个QQ 258425240
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-10-25 15:13:13 | 显示全部楼层
好,我也有这个想法. 呵呵
QQ:185259642
回复 支持 反对

使用道具 举报

发表于 2006-10-25 15:21:02 | 显示全部楼层
呵呵我加你了可是你的QQ好像不在线。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-10-25 15:27:09 | 显示全部楼层
现在是上班时间,不能上QQ, 晚上下班后我会上的.

不过我刚才看了一下中文版的(电子版),相应的章节也是这样写的. 你有注意过吗?
回复 支持 反对

使用道具 举报

发表于 2006-10-25 19:33:39 | 显示全部楼层
Post by future_god
书上写的我可以理解,但跟这段说明前面运行的程序代码对不上,而且现象也不同,child收到FIN后好像并没有产生SIGCHLD的信号.因为sig_chld()函数没有执行.(用Ethereal看过包的收发)


这里不明白,SIGCHLD和Ethereal有什么关系?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表