免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2406 | 回复: 5
打印 上一主题 下一主题

[函数] 管道小程序,time和wait函数实在不解,请赐教 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-07-19 19:59 |只看该作者 |倒序浏览
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
        int pipe1_fd[2] = { 0};
        int pipe2_fd[2] = { 0};

        char *parent_talks[] = {"hi,my baby",
                "can you tell daddy date and time?",
                "Daddy have to leave here,bye!",
                NULL
        };

        char *child_talks[] = { "hi,daddy",
                "sure,",
                "bye!",
                NULL
        };

        char parent_buf[256] = { '\0'};
        char child_buf[256] = { '\0'};
        char *parent_ptr = NULL;
        char *child_ptr = NULL;
        int i = 0;
        int j = 0;
        int len = 0;
        int child_status = 0;
        time_t curtime;
        
        if(pipe(pipe1_fd) < 0)
        {
                printf("pipel create erro\n" );
                return -1;
        }

        if(pipe(pipe2_fd) < 0)
        {
                printf("pipe2 create error\n" );
                return -1;
        }

        if(fork() == 0)
        {
                printf("\n" );
                close(pipe1_fd[1]);
                close(pipe2_fd[0]);

                i = 0;
                child_ptr = child_talks;
                while(child_ptr != NULL)
                {
                        len = read(pipe1_fd[0],child_buf, 255);
                        child_buf[len] = '\0';
                        printf("Parent: %s\n",child_buf);
                        if(i == 1)
                        {
                                time(&curtime);
                                len = sprintf(child_buf, "%s%s",child_ptr,ctime(&curtime));
                                child_buf[len-1] = '\0';
                                write(pipe2_fd[1],child_buf,strlen(child_buf));
                        }
                        else
                        {
                                write(pipe2_fd[1],child_ptr,strlen(child_ptr));
                        }
                        child_ptr = child_talks[++i];
                }
                close(pipe1_fd[0]);
                close(pipe2_fd[0]);
                exit(0);
        }
        else
        {
                close(pipe1_fd[0]);
                close(pipe2_fd[1]);

                j = 0;
                parent_ptr = parent_talks[j];
                while(parent_ptr != NULL)
                {
                        write(pipe1_fd[1],parent_ptr,strlen(parent_ptr));
                        len = read(pipe2_fd[0],parent_buf,255);
                        parent_buf[len] = '\0';
                        printf( "Child: %s\n",parent_buf);
                        parent_ptr = parent_talks[++j];
                }
               
                close(pipe1_fd[1]);
                close(pipe2_fd[0]);
                wait(&child_status);
                exit(0);
        }

        return 0;

}


重点不明,time函数和wait函数,网上查了,还是不太明白,请帮帮我,谢谢大伙。

[ 本帖最后由 zhanglupanda 于 2007-7-19 20:01 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2007-07-19 20:10 |只看该作者
time()函数是获得当前时间的,
wait()函数是等待当前进程的子进程结束, 回收该子进程的资源. 使其不成为孤儿进程.

论坛徽章:
0
3 [报告]
发表于 2007-07-19 20:21 |只看该作者
楼主还是把代码那种标签里吧

论坛徽章:
0
4 [报告]
发表于 2007-07-20 10:18 |只看该作者
先谢谢楼上两位大哥,
wait(&child_status)  中child_status只在定义时出现过,
是怎样回收子进程的呀!
二楼的大哥说的我不太明白,让我干什么呀

我现在安装bugzilla,sendmail还不会用,有会的吗?

论坛徽章:
0
5 [报告]
发表于 2007-07-20 10:32 |只看该作者
就是说如果你子进程退出了, 但是父进程还没有退出, 如果此时父进程不调用wait()这一系列的函数的话, 子进程就是孤儿进程, 此时仍然会占用一些系统资源. 所以说要调用wait()这一系列的函数来回收这些资源.
不过如果你的父进程比子进程提前退出的话, 那么子进程的父进程就变成了进程1了, 而进程1会不断的调用wait()系列函数来回收这些资源.

论坛徽章:
0
6 [报告]
发表于 2007-07-20 11:15 |只看该作者
你不用管它怎么收的,会收就得了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP