简体   繁体   English

如何在 c 程序中使用管道修复数据写入或读取给出错误输出?

[英]How to fix data write or read using pipe in c program is giving wrong output?

I am trying to get an integer input in the child process and send it to the parent process using pipe()我正在尝试在子进程中获取整数输入并使用 pipe() 将其发送到父进程

but I receive garbage values every time in the parent process.但我每次在父进程中都会收到垃圾值。

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include<sys/wait.h>
int main(int argc, char *argv[])
{
    pid_t pid;
    int fd[2];
    char *args[] = {"", NULL};
    int cnum,pnum;
    
    pid = fork();
    if (pid < 0)
    {
        perror("fork");
        exit(1);
    }
    if(pipe(fd) == -1)//fd[0] for read fd[1] for write
    {
        perror("pipe");
        exit(1);
    }
    if(pid == 0)
    {
        close(fd[0]);
        printf("\n**In the child process**\n");
        printf("Enter Number : ");
        scanf("%d",&cnum);
        write(fd[1],&cnum,sizeof(int));
        close(fd[1]);

    }
    else
    {
        wait(NULL);
        close(fd[1]);
        printf("\n**In the parent precess**\n");
        read(fd[0],&pnum,sizeof(int));
        close(fd[0]);
        
        printf("Number recieved = %d\n",pnum);
        printf("PID = %d\n", getpid());
        execv("./sayHello", args);
        printf("Error");
    }
}

Output of the above code上述代码的输出

**In the child process**
Enter Number : 212
**In the parent precess**
Number recieved = 1036468968
PID = 22528
Hillo Amol
PID = 22528

I give input of 212 but in parent 1036468968 received.我提供了 212 的输入,但在父级中收到了 1036468968。

You call fork before you create the pipe FDs.在创建管道 FD之前调用fork After you call fork , the parent and the child both create their own pair of pipe FDs, and there's no shared pipe between them.调用fork后,父子节点都创建了自己的一对管道 FD,并且它们之间没有共享管道。

Create the pipe before you fork and it could work.在分叉之前创建管道,它可以工作。

As drorfromthenegev suggest problem is arising due to I am calling pipe() after fork().正如 drorfromthenegev 建议的那样,由于我在 fork() 之后调用 pipe() 而出现问题。

So I call pipe() first and the i call fork() and it works..所以我先调用 pipe() 然后我调用 fork() 并且它有效..

Workable solution可行的解决方案

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include<sys/wait.h>
int main(int argc, char *argv[])
{
    pid_t pid;
    int fd[2];
    char *args[] = {"", NULL};
    int cnum,pnum;
    
    if(pipe(fd) == -1)//fd[0] for read fd[1] for write
    {
        perror("pipe");
        exit(1);
    }
    pid = fork();
    if (pid < 0)
    {
        perror("fork");
        exit(1);
    }
    
    if(pid == 0)
    {
        close(fd[0]);
        printf("\n**In the child process**\n");
        printf("Enter Number : ");
        scanf("%d",&cnum);
        write(fd[1],&cnum,sizeof(int));
        close(fd[1]);

    }
    else
    {
        wait(NULL);
        close(fd[1]);
        printf("\n**In the parent precess**\n");
        read(fd[0],&pnum,sizeof(int));
        close(fd[0]);
        
        printf("Number recieved = %d\n",pnum);
        printf("PID = %d\n", getpid());
        execv("./sayHello", args);
        printf("Error");
    }
}

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

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