【问题标题】:Sequential pipes between two children and a parent in C/UNIXC/UNIX 中两个子级和父级之间的顺序管道
【发布时间】:2016-06-02 10:36:52
【问题描述】:

我试图解决操作系统书籍中关于进程间通信和管道的一个例子,但我遇到了一些困难。它要求我找到父母的时间,然后用孩子 no1 打印出来。经过一些睡觉的孩子没有 2 应该打印在 1 号孩子中找到的时间信息。在一些睡觉的父母应该打印在 2 号孩子中找到的时间信息之后。所以我想我应该创建 3 个管道,因为我需要传递时间信息 3次。我试图在孩子一和孩子二之间设计其中一个,但我不确定我是否做得正确。另一个问题是我不知道如何打印时间。当我尝试用 printf %d 打印它时它给了我 0。任何人都可以帮助我将时间从父母传递到孩子 number1 并打印出来以便我可以测试我的程序吗?这是我的代码。

#include<stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/time.h>
#include<signal.h>



int main(int argc, char** argv)
{
    struct timeval t1;
    struct timeval t2;
    struct timeval t3;
    int firstchild,secondchild;
    firstchild=fork();
        int mypipe[2];
    int mypipe2[2];
    int mypipe3[2];

        if(pipe(mypipe) == -1) {
          perror("Pipe failed");
          exit(1);
        }

        if(firstchild == 0)            //first child
        {

       close(STDOUT_FILENO);  //closing stdout
            dup(mypipe[1]);         //pipewrite is replaced.
        sleep(3);               
            close(mypipe[0]);       //pipe read is closed.
            close(mypipe[1]);      //pipe write is closed   

        }else{
    secondchild=fork();        //Creating second child

        if(secondchild == 0)            //2nd child
        {
            close(STDIN_FILENO);   //closing stdin
            dup(mypipe[0]);         //pipe read
        sleep(6);
            close(mypipe[1]);       //pipe write    is closed.
            close(mypipe[0]);      //pipe read is closed.   

        }else{            //Parent

    gettimeofday(&t1,NULL);
    printf("\n Time is %d ",gettimeofday(&t1,NULL));
    sleep(9);
    printf("\n Parent:sent a kill signal to child one with id %d ",secondchild-1);
    printf("\n Parent:sent a kill signal to child two with id %d ",secondchild);
    kill(secondchild-1,SIGKILL);    
    kill(secondchild,SIGKILL);
    exit(1);}}
        close(mypipe[0]);
        close(mypipe[1]);
        return 0;
    }

让问题更清楚一点:我认为 dup 可以正常工作。我需要的是父母和第一个孩子之间的工作管道以及打印第一个孩子的时间(在父母中计算)的方法,以便我可以测试他们两个工作并继续编码。我愿意使用不同风格的管道,只要它有效。使用 dup 是我在书中看到的东西,所以这就是我使用的东西

【问题讨论】:

  • 您是否阅读过您正在使用的任何函数的手册页?您希望从dup(mypipe[0]); 获得什么?你不觉得返回值可能有什么意义吗?
  • 只是一个评论:在标准英语写作中,在. , ? ! ; :之类的标点符号后放置一个空格。您的帖子没有做到这一点,而且阅读起来非常困难。
  • @EOF 我同意这个问题有点不清楚。但我认为dup 可以,因为它正在用mypipe[0] 替换标准输入。这是因为STDIN_FILENOdup 之前关闭。它依赖于 dup “对新描述符使用编号最小的未使用描述符”这一事实(来自手册页的引用)。我也不知道这种技术,最近在 SO 上了解了它。
  • 我认为 dup 工作正常。我需要的是父母和第一个孩子之间的工作管道以及打印第一个孩子的时间(在父母中计算)的方法,以便我可以测试他们两个工作并继续编码。
  • @kaylum:这太疯狂了。 dup() 返回文件描述符以及引入dup2() 的原因有一个原因。您期望的编号最小的文件描述符可能已被另一个线程甚至信号处理程序重用。这种编程是可怕不好的做法。

标签: c linux unix pipe parent-child


【解决方案1】:

我应该创建 3 个管道,因为我需要传递时间信息 3 次。我试图在孩子一和孩子二之间设计一个 但我不确定我是否做得正确。

    firstchild=fork();
        int mypipe[2];
    …
        if(pipe(mypipe) == -1) …

你没有。您必须在fork() 之前创建管道,否则子无法访问(读取端)父管道。

将时间从父母传递给孩子编号1

家长:

    gettimeofday(&t1, NULL);
    write(mypipe[1], &t1, sizeof t1);

第一个孩子:

        read(mypipe[0], &t1, sizeof t1);

我不知道如何打印时间。当我尝试使用 printf 打印时 %d 它给了我 0。

    printf("\n Time is %d ",gettimeofday(&t1,NULL));

你没有打印时间,而是gettimeofday()函数返回的,是0。正确:

        printf(" Time is %ld\n", (long)t1.tv_sec);

【讨论】:

    猜你喜欢
    • 2015-01-18
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    • 1970-01-01
    • 2022-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多