简体   繁体   English

用叉子和孩子创造的管道

[英]pipe with fork and child creationg

This is my code for a problem that I am trying to solve. 这是我要解决的问题的代码。 If anyone one have any idea how to solve this please help. 如果有人对解决此问题有任何想法,请提供帮助。 I am trying to create infinity children with as many pipe as the child's that we will create, but I am losing some children with their pipe and I don't know why. 我正在尝试使用与我们将要创建的子管道数量一样多的子管道来创建无限子管道,但是我正在失去一些用它们的管道的子管道,我不知道为什么。

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/wait.h>
void main() {
    int n;
    pid_t p;
    int pfd[2];
    int val=0;
    int i;

    printf("Enter the number of child you want:");
    scanf("%d",&n);

    pipe(pfd);

    while(1) {
        for(i=1;i<=n;i++) {

            p=fork();
            if(p>0){
                close(pfd[0]);
                sleep(5);
                val++;
                printf("Message %d: Process %d has process id %d and its child has process id %d \n",val,i,getppid(),getpid());
                write(pfd[0],&val,sizeof(val));
                close(pfd[1]);
                //wait(NULL);
                //exit(EXIT_SUCCESS);
            }
            else {
                close(pfd[1]);
                val++;
                //printf("Message %d: Process %d has process id %d and its child has process id %d \n",val,i,getpid(),getppid());
                //while(read(pfd[0],&val,sizeof(val))>0)
                read(pfd[0],&val,sizeof(val));
                //printf("Message %d: Process %d has process id %d and its child has process id %d \n",val,i,getpid(),getppid());
                write(pfd[0],&val,sizeof(val));
                close(pfd[0]);
                _exit(EXIT_SUCCESS);
            }
        }
    }
    printf("Message %d: last child's pid is %d and my father is : %d \n", n,getpid(),getppid());    
} 

OUTPUT IS: Enter the number of child you want: 3 输出是:输入您想要的孩子人数:3

Message 1: Process 1 has process id 678 and his child's id is : 15041 消息1:进程1的进程ID为678,他的孩子的ID为:15041

Message 2: Process 2 has process id 678 and his child's id is : 15041 消息2:进程2的进程ID为678,他的孩子的ID为:15041

Message 3: Process 3 has process id 678 and his child's id is : 15041 消息3:进程3的进程ID为678,他的孩子的ID为:15041

Message 4: Process 1 has process id 678 and his child's id is : 15041 消息4:进程1的进程ID为678,他的孩子的ID为:15041

Message 5: Process 2 has process id 678 and his child's id is : 15041 消息5:进程2的进程ID为678,他的孩子的ID为:15041

Message 6: Process 3 has process id 678 and his child's id is : 15041 消息6:进程3的进程ID为678,他的孩子的ID为:15041

Message 7: Process 1 has process id 678 and his child's id is : 15041 消息7:进程1的进程ID为678,他的孩子的ID为:15041

Message 8: Process 2 has process id 678 and his child's id is : 15041 消息8:进程2的进程ID为678,他的孩子的ID为:15041

Message 9: Process 3 has process id 678 and his child's id is : 15041 消息9:进程3的进程ID为678,他的孩子的ID为:15041

Message 10: Process 1 has process id 678 and his child's id is : 15041 消息10:进程1的进程ID为678,他的孩子的ID为:15041

Message 11: Process 2 has process id 678 and his child's id is : 15041 消息11:进程2的进程ID为678,他的孩子的ID为:15041

Message 12: Process 3 has process id 678 and his child's id is : 15041 消息12:进程3的进程ID为678,他的孩子的ID为:15041

So my problem is that the process id's are not changing and I can't find why 所以我的问题是process id's并没有改变,我找不到原因

So my problem is that the process id's are not changing and I can't find why 所以我的问题是process id's并没有改变,我找不到原因

because 因为

printf("Message %d: Process %d has process id %d and its child has process id %d \n",val,i,getppid(),getpid());

is executed by the parent, so getppid (the pid of the parent of the parent) is always the same, getpid also remains the same. 由父级执行,因此getppid (父级的父级的pid)始终相同, getpid也保持不变。 The only thing that changes are val and `i'. 唯一改变的是val和'i'。

You only create a pipe once and then use it for all forks, meanwhile you close the only created pipe and then fork again, this is not going to end well. 您只创建了一个管道,然后将其用于所有派生,同时关闭了唯一创建的管道,然后再次进行了派生,这将无法顺利完成。

You have to create the pipes before you do the fork. 在进行分叉之前,必须先创建管道。 Also you are closing the reading end of the pipe on the parent process, and the writing to the close reading end. 另外,您还要关闭父进程上管道的读取端,并写入关闭的读取端。 You have to write on the writing end. 您必须在写作端进行写作。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(void)
{
    pid_t p;
    int pfd[2];
    int i;
    int val = 0;
    int n = 3;

    for(i=1;i<=n;i++) {
        if(pipe(pfd) < 0)
        {
            perror("pipe");
            continue;
        }

        p = fork();

        if(p < 0)
        {
            perror("fork");
            continue;
        }

        if(p)
        {
            // PARENT PROC
            close(pfd[0]);
            val++;
            printf("Message %d: Parent process has process id %d and its child has process id %d\n", val, getpid(), p);
            write(pfd[1], &val, sizeof val);
            close(pfd[1]);
            int status;
            waitpid(p, &status, 0);

            if(WIFEXITED(status))
                printf("Child %d exit status: %d\n", i, WEXITSTATUS(status));
            else
                printf("Child %d did not exit normally\n", i);
        } else {
            // CHILD PROC

            close(pfd[1]);
            if(read(pfd[0], &val, sizeof val) < 0)
            {
                printf("Child %d: could not read from pipe\n", i);
                _exit(EXIT_FAILURE);
            }

            printf("Child %d, message: %d. Child pid: %d, parent pid: %d\n", i, val, getpid(), getppid());

            close(pfd[0]);
            _exit(EXIT_SUCCESS);
        }
    }

    return 0;
}

This prints 此打印

Message 1: Parent process has process id 9829 and its child has process id 9830
Child 1, message: 1. Child pid: 9830, parent pid: 9829
Child 1 exit status: 0
Message 2: Parent process has process id 9829 and its child has process id 9831
Child 2, message: 2. Child pid: 9831, parent pid: 9829
Child 2 exit status: 0
Message 3: Parent process has process id 9829 and its child has process id 9832
Child 3, message: 3. Child pid: 9832, parent pid: 9829
Child 3 exit status: 0

One last thing: the correct prototypes for main are: 最后一件事: main的正确原型是:

  • int main(void);
  • int main(int argc, char *argv[]);
  • int main(int argc, char **argv);

Your void main() is incorrect. void main()不正确。

When p>0 , that means you are in the parent process, not the child, and p is the pid of the child. p>0 ,这意味着您处于父进程中,而不是子进程中,并且p是子进程的pid。

this 这个

printf("Message %d: Process %d has process id %d and its child has process id %d \n",val,i,getppid(),getpid());

should be 应该

printf("Message %d: Process %d has process id %d and its child has process id %d \n",val,i,getpid(),p)

and if you want the child to display a similar message in the else block your first statement works, but just change the word child to parent. 如果希望孩子在else块中显示类似的消息,则您的第一个语句是有效的,只需将“孩子”一词更改为“父母”即可。

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

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