fork with child creationg

时间:2018-02-22 00:59:48

标签: c pipe fork

这是我正在尝试解决的问题的代码。 如果有人知道如何解决这个问题,请帮忙。 我正在尝试用我们将创造的孩子的管道来创建无限儿童,但我正在丢失一些带管子的孩子,我不知道为什么。

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

输出是: 输入您想要的孩子数量:3

  

消息1:进程1的进程ID为678,其子进程为:15041

     

消息2:进程2的进程ID为678,其子进程为:15041

     

消息3:进程3的进程ID为678,其子进程为:15041

     

消息4:进程1的进程ID为678,其子进程为:15041

     

消息5:进程2的进程ID为678,他的孩子的id为:15041

     

消息6:进程3的进程ID为678,其子进程为:15041

     

消息7:进程1的进程号为678,他的孩子的ID为:15041

     

消息8:进程2的进程ID为678,其子进程为:15041

     

消息9:进程3的进程号为678,他的孩子的ID为:15041

     

消息10:进程1的进程号为678,其子进程号为:15041

     

消息11:进程2的进程号为678,其子进程号为:15041

     

消息12:进程3的进程号为678,其子进程号为:15041

所以我的问题是process id's没有改变,我找不到原因

2 个答案:

答案 0 :(得分:1)

  

所以我的问题是process id's没有改变,我无法找到原因

,因为

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

由父项执行,因此getppid(父项的父项的pid)是 始终相同,getpid也保持不变。唯一改变的是val和`i&#39;。

您只需创建一次管道,然后将其用于所有叉子,同时关闭 唯一创建的管道,然后再次分叉,这不会很好。

您必须在执行fork之前创建管道。你也正在关闭 读取父进程上管道的末尾,并写入关闭 读完了。你必须写在写作结束。

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

打印

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

最后一件事:main的正确原型是:

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

您的void main()不正确。

答案 1 :(得分:0)

p>0时,这意味着你在父进程中,而不是孩子,而p是孩子的pid。

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

应该是

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

如果您希望孩子在else块中显示类似的消息,则您的第一个语句可以正常工作,但只需将单词child更改为parent。