如何使用C程序中的管道修复数据写或读取的数据是错误的输出?

发布于 2025-02-02 18:35:50 字数 1330 浏览 3 评论 0原文

我正在尝试在子进程中获取整数输入,并使用管道()将其发送到父进程,

但是我每次都会在父进程中收到垃圾值。

#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");
    }
}

上述代码的输出

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

我给出的输入为212,但在父1036468968中收到。

I am trying to get an integer input in the child process and send it to the parent process using 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲影 2025-02-09 18:35:50

您在之前调用fork 您创建管道FD。调用fork后,父母和孩子都创建自己的一对管道FD,并且之间没有共享的管道。

在分叉之前创建管道,它可以起作用。

You call fork before you create the pipe FDs. 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.

Create the pipe before you fork and it could work.

雄赳赳气昂昂 2025-02-09 18:35:50

由于drorfromthenegev建议问题出现是由于我在fork()之后致电Pipe(

#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");
    }
}

As drorfromthenegev suggest problem is arising due to I am calling pipe() after fork().

So I call pipe() first and the i call fork() and it works..

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");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文