为何 printf无法正常 工作 ?

Ashnu 2004-02-25 04:02:45
我的系统是Fedora Core Test1,程序代码如下:

1 #include <sys/un.h> /* for sturct sockaddr_un */
2 #include <sys/socket.h>
3 #include <netinet/in.h> /* for htonl() */
4 #include <sys/types.h> /* for umask() */
5 #include <sys/stat.h> /* for umask() */
6 #include <string.h>
7 #include <stdio.h>
8 #include <errno.h>
9 #include "unigate.h"
10
11 main()
12 {
13 int php_listenfd, php_connfd;
14 pid_t childpid;
15 socklen_t clilen;
16 struct sockaddr_un php_cliaddr, php_servaddr;
17
18 printf("hello world");
19 if ((php_listenfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0)
20 perror("socket");
21
22 unlink(UNIGATE_SOCK_PATH);
23 bzero(&php_servaddr, sizeof(php_servaddr));
24 php_servaddr.sun_family = AF_LOCAL;
25 strcpy(php_servaddr.sun_path, UNIGATE_SOCK_PATH);
26
27 umask(0);
28 bind(php_listenfd, (struct sockaddr*)&php_servaddr, sizeof(php_servaddr));
29 if (listen(php_listenfd, 5)<0)
30 perror("listen");
31
32 for (;;)
33 {
34 clilen = sizeof(php_cliaddr);
35 if((php_connfd = accept(php_listenfd, (struct sockaddr*)&php_cliaddr, &clilen))<0 )
36 {
37 if (errno == EINTR)
38 continue;
39 else
40 perror("accept");
41 }
42 if((childpid = fork()) == 0)
43 {
44 char buf[24]={'\0'};
45 close(php_listenfd);
46 while (buf != "###")
47 {
48 if (read(php_connfd, buf, 23)<0)
49 perror("read");
50 printf("read: %s", buf);
51 write(php_connfd, buf, 23);
52 }
53 exit(0);
54 }
55 close(php_connfd);
56 }
57 }

大家只需要看一下第18行的printf语句, 我用gcc编译后执行,我认为应该会打印出"hello world",但实际情况是什么也没有打印出来.而更奇怪的是,当我把第18行修改成:

18 printf("hello world\n");

重新编译后执行,正常的打印除了"hello world".

不解,大惑不解!!!希望行家解惑!
我遇到过好几次这种类似的问题,都是printf,在我认为不应该出错的地方出错了.看来是我对这个函数还不够了解.谁能给我gcc中printf的实现?谢谢!!
...全文
40 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复

23,110

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧