简体   繁体   English

从用户读取整数并写入子管道,然后从其读取父管道

[英]read integer from user and write into child pipe and then parent pipe read from it

i am trying to do simple read/write in pipe but it gives me error 我正在尝试在管道中执行简单的读/写操作,但这给了我错误

this is my code : 这是我的代码:

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<fcntl.h>
#define BUFSIZE 80

int main()  
{
    int fd[2],n,i,h;
    char buf[BUFSIZE];
    pipe(fd);   
    switch(fork())
    {   
        case -1 :printf("Fork Error");
            exit(0);
        case 0  :close(fd[0]);  
            printf("Enter N :");
            scanf("%d",&n);
            write(fd[1],&n,sizeof(n));
            close(fd[1]);
        case 1  :close(fd[1]);  
            n=read(fd[0],&h,sizeof(h));
            for(i=1;i<=h;i++)
            {
                if(i%2==1)
                {
                    write(1,&i,n);
                }
            }
            close(fd[0]);
    }
    exit(0);
}

in this code : child's fd[0] pointer is closed and write from child's fd[1] pointer and then parent reading from fd[1] pointer and stores it in h variable and then value of variable i is going in STDOUT_FILENO (1) and displays output on standard output 在此代码中:关闭孩子的fd [0]指针,然后从孩子的fd [1]指针写入,然后从fd [1]指针读取父对象,并将其存储在h变量中,然后变量i的值进入STDOUT_FILENO(1)并在标准输出上显示输出

output : 输出:

kartik@ubuntu:~/Desktop/isp$ 卡尔蒂克@ Ubuntu的:〜/桌面/ ISP $

Enter N :6 输入N:6

6: command not found 6:找不到命令

There are several issues here: 这里有几个问题:

  • You're prompting for N in the child process, however the parent process is the one that receives input from the terminal. 您在子进程中提示输入N ,但是父进程是从终端接收输入的进程。 So by the time you enter "6", the parent process has exited and you're entering that value to the shell. 因此,当您输入“ 6”时,父进程已退出,并且您正在将该值输入到Shell中。 Move the printf and scanf to before the fork and you should be able to read that value properly. printfscanf移至fork之前,您应该能够正确读取该值。
  • Your case 1 will not be entered for the case when fork() returns from the parent, which seems to be your intent. fork()从父级返回时,将不会输入case 1 ,这似乎是您的意图。 fork() returns the pid of the child to the parent. fork()将孩子的pid返回给父对象。 Because the special init process has pid 1, this will never be true. 因为特殊的init过程具有pid 1,所以永远不会如此。 Change case 1: to default to process the parent process. case 1:更改为default以处理父流程。
  • You're not using break at the end of your switch cases. 在切换案例结束时,您不会使用break In C, switch cases "fall through", meaning that once the statement for one case are complete, it will continue running the statements for the following case. 在C中,切换用例“失败”,这意味着一旦一个用例的语句完成,它将继续运行以下用例的语句。 The break statement at the end of each case prevents that from happening. 在每种情况下, break语句都会阻止这种情况的发生。

With these corrections, you now have this: 通过这些更正,您现在有了:

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<fcntl.h>
#define BUFSIZE 80

int main()
{
    int fd[2],n,i,h;
    pid_t pid;     // to capture the child pid
    char buf[BUFSIZE];
    // prompt for N before forking
    printf("Enter N :");
    scanf("%d",&n);
    pipe(fd);
    switch((pid=fork()))   // saving the child pid in case we want to use it later
    {
        case -1 :
            printf("Fork Error");
            exit(0);     // no break needed here because of exit
        case 0  :
            close(fd[0]);
            write(fd[1],&n,sizeof(n));
            close(fd[1]);
            break;       // end of case 0
        default :
            close(fd[1]);
            n=read(fd[0],&h,sizeof(h));
            printf("h=%d\n",h);
            for(i=1;i<=h;i++)
            {
                if(i%2==1)
                {
                    //write(1,&i,n);
                    printf("%d\n",i);
                }
            }
            close(fd[0]);
            break;      // end of default case
    }
    exit(0);
}

Your parent process needs to wait until its child theme finish its work and push data in pipe... 您的父进程需要等到其子主题完成工作并将数据推送到管道中后,才能执行此操作。

so add below line. 所以添加下面的行。

            wait(&status);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM