两个孩子从管道读 [英] two children reading from a pipe

查看:108
本文介绍了两个孩子从管道读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我想做什么:

我试图让这两个孩子创造父程序,父母创建无名管道,写入它和孩子都应该从中读取数据(每1个字节),然后输出两个不同的终端窗口的结果。我不知道如何将它们进行同步。

我得到了一个终端窗口是这样的:Nejke AA
而在第二:ADT
我想:Nejake数据

我试着在互联网上搜索,但我问反正。
任何帮助是极大的AP preciated。

 的#include<&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&fcntl.h GT;
#包括LT&; SYS / stat.h>/ *声明我们的程序* /
无效runchild1(INT PFD []);
无效runchild2(INT PFD []);/ *一些数据写入和从管道中读取* /
为const char some_data [] =Nejake数据;
INT主(INT ARGC,字符** argv的)
{
INT PID,状态; // PID调试
INT FD [2]; //对于管文件描述符/ *让创建一些管道* /
管(FD);/ *应该运行过程中的两个孩子* /
runchild1(FD);
runchild2(FD);/* 这个很重要!在管道关闭两个文件描述符* /
关闭(FD [0]);关闭(FD [1]);/ *拿起所有的死亡儿童* /
而((PID =等待(安培;!状态))= - 1)
    fprintf中(标准错误,进程%d个%d条\\ n退出,PID,WEXITSTATUS(状态));
出口(0);
}无效runchild1(INT PFD [])/ *运行的第一个孩子* /
{
INT PID; / *您可能需要打印调试* /
INT data_processed; / *存储数据* /
INT德; / *描述符打开的文件* /
字符缓冲区; / *缓冲器读取数据字节* /开关(PID =叉()){案件0:/ *孩子从管道中读取* /    关闭(PFD [1]); / *这个过程不需要的另一端* /
    而((data_processed =读(PFD [0],&放大器;缓冲器,1))大于0){        的printf(%PROCES D,数据citane婆bajte:%C \\ N,GETPID(),缓冲区);
        DES =打开(/开发/ ttys001,O_RDWR);
        写(DES,&放大器;缓冲器,1);
    }
    出口(0);默认值:/ *父写入管道* /    / *写一些数据给孩子读* /
    data_processed =写入(PFD [1],some_data,strlen的(some_data));
    的printf(了ZAPI%d个bytov CEZ nepomenovanu鲁鲁:\\ n,data_processed);
    的printf(Zapisane:%S \\ n,some_data);
    的printf(索姆罗迪奇dvoch德提数:%d \\ n,GETPID());    打破;情况1:
    PERROR(叉);
    出口(1);
}
}
无效runchild2(INT PFD [])/ *运行老二* /
{
INT PID;
INT data_processed;
INT德;
字符缓冲区;开关(PID =叉()){案件0:/ * *子/    关闭(PFD [1]); / *这个过程中不需要的另一端* /
    而((data_processed =读(PFD [0],&放大器;缓冲器,1))大于0){        的printf(%PROCES D,数据citane婆bajte:%C \\ N,GETPID(),缓冲区);
        DES =打开(/开发/ ttys002,O_RDWR);
        写(DES,&放大器;缓冲器,1);
    }
    出口(0);
默认值:/ *父不执行任何操作* /
    打破;情况1:
    PERROR(叉);
    出口(1);
}
}


解决方案

如果两个孩子都需要看到相同的数据,你需要两个管道,每个孩子一个,家长必须写两次,每封邮件,一旦每个管。

另外,你可以运行 T恤 进程替换,或者你可以看一下(试行仰视)程序撒尿三通的进程/管变异) - 或者你可以按照从这个堆栈溢出的答案链接。你的程序将有一个管道,但孩子们将各自结束了自己的管道。

Here's what I'd like to do:

I've tried to make a program that creates parent with two children, parent creates unnamed pipe, writes into it and the children are supposed to read from it (per 1 byte) and then output the results in two different terminal windows. What I do not know is how to synchronise them.

I get something like this in one terminal window: Nejke aa and in the second: adt I want: Nejake data

I tried searching on the internet, but I'm asking anyway. Any help is greatly appreciated.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>

/* declare our procedures */
void runchild1(int pfd[]);
void runchild2(int pfd[]);

/* some data to write and read from pipe */
const char some_data[] =  "Nejake data" ;




int main(int argc, char **argv)
{
int pid, status;                //PID for debugging
int fd[2];                      //file descriptors for the          pipe

/* let create some pipe */
pipe(fd);   

/* supposed to run two children of the process */
runchild1(fd);
runchild2(fd);

/* this is important! close both file descriptors on the pipe */
close(fd[0]); close(fd[1]);     

/* pick up all the dead children */
while ((pid = wait(&status)) != -1) 
    fprintf(stderr, "process %d exits with %d\n", pid, WEXITSTATUS(status));
exit(0);
}



void runchild1(int pfd[])   /* run the first child */
{
int pid;                /* you may want to print it for debugging */
int data_processed;     /* store data */
int des;                /* descriptor for open files  */
char buffer;            /* buffer for reading byte of data  */

switch (pid = fork()) {

case 0:                 /* child reads from the pipe */

    close(pfd[1]);      /* this process don't need the other end */
    while ((data_processed = read(pfd[0],&buffer,1)) > 0) {

        printf("Proces %d, data citane po bajte: %c\n",getpid(),buffer);
        des = open("/dev/ttys001",O_RDWR);
        write(des, &buffer,1);          
    }               
    exit(0);

default: /* parent writes to the pipe  */       

    /* write some data for children to read */
    data_processed = write(pfd[1], some_data, strlen(some_data));
    printf("Zapis %d bytov cez nepomenovanu ruru:\n", data_processed);
    printf("Zapisane: %s\n",some_data);
    printf("Som rodic dvoch deti: %d\n",getpid());

    break;

case -1:
    perror("fork");
    exit(1);
}
}


void runchild2(int pfd[])   /* run the second child */
{
int pid;
int data_processed;
int des;
char buffer;

switch (pid = fork()) {

case 0:                 /* child */

    close(pfd[1]);      /* this process doesn't need the other end */
    while ((data_processed = read(pfd[0],&buffer,1)) > 0) {

        printf("Proces %d, data citane po bajte: %c\n",getpid(),buffer);
        des = open("/dev/ttys002",O_RDWR);
        write(des, &buffer,1);          
    }
    exit(0);        


default: /* parent does nothing */
    break;

case -1:
    perror("fork");
    exit(1);
}
}

解决方案

If the two children both need to see the same data, you'll need two pipes, one per child, and the parent must write each message twice, once on each pipe.

Alternatively, you could run the tee command with process substitution, or you can look up (try looking up) the program pee (process/pipe variant of tee) — or you can follow links from this Stack Overflow answer. Your program will have one pipe, but the children will each end up with their own pipe.

这篇关于两个孩子从管道读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆