为什么我的进程并不同时运行?

时间:2012-09-07 05:22:38

标签: c process fork pid

我的问题是:

1。)如何让父进程永远死?我知道这没有完成,因为父亲是第一个被运行的pid,但我不知道如何改变它。

2.。)如何让我的子进程像他一样同时执行?我甚至把这个数字提高到很高,看它是不是巧合,但似乎不是。

编辑:解决方案

1。)在内部默认
中添加了两次wait(NULL) 2.)正在发生。用睡眠(1)来证明它。

我的代码如下

#include <stdio.h>
int main() {
    int pid, i;
    pid = fork();
    switch(pid) {
        case -1:
            // error
            printf("Fork error");
            break;
        case 0:
            // child process
            printf("First child is born, my pid is %d\n", getpid());
            for(i=1; i<10; i++)
                printf("First child executes iteration %d\n", i);
            printf("First child dies quietly.\n");
            break;
        default:
            // parent process
            printf("Parent process is born, my pid is %d\n", getpid());
            pid = fork();
            switch(pid) {
                case -1:
                    // error
                    printf("Fork error");
                    break;
                case 0:
                    // child process
                    printf("Second child is born, my pid is %d\n", getpid());
                    for(i=1; i<10; i++)
                        printf("Second child executes iteration %d\n", i);
                    printf("Second child dies quietly.\n");
                    break;
                default:
                    // parent process
                    printf("Parent process dies quietly.");
        }
    }
    return 0;
}

我的输出总是如下:

Parent process is born, my pid is 7847  
First child is born, my pid is 7848  
First child executes iteration: 1  
First child executes iteration: 2  
First child executes iteration: 3  
First child executes iteration: 4  
First child executes iteration: 5  
First child executes iteration: 6  
First child executes iteration: 7  
First child executes iteration: 8  
First child executes iteration: 9  
First child executes iteration: 10  
First child dies quietly.  
Parent process dies quietly.  
Second child is born, my pid is 7849  
Second child executes iteration 1  
Second child executes iteration 2  
Second child executes iteration 3  
Second child executes iteration 4  
Second child executes iteration 5  
Second child executes iteration 6  
Second child executes iteration 7  
Second child executes iteration 8  
Second child executes iteration 9  
Second child executes iteration 10  
Second child dies quietly.  

我的任务是:

编写一个C程序(“procs.c”),它创建三个进程:一个创建两个子进程的父进程。

第一个孩子应该做到以下几点:

  • 显示“第一个孩子出生,我的pid是......”

  • 显示十次“第一个孩子执行迭代X”的消息,其中X是迭代次数

  • 显示“第一个孩子安静地死去。”

第二个孩子应该做到以下几点:

  • 显示“第二个孩子出生,我的pid是......”

  • 显示十次“第二个孩子执行迭代X”的消息,其中X是迭代次数

  • 显示“第二个孩子安静地死去。”

父进程应执行以下操作:

  • 显示“父进程诞生,我的pid是......”

  • 创建第一个孩子

  • 创建第二个孩子

  • 显示“父进程悄然死亡。”

使用gcc编译程序并命名可执行文件“procs”。多次执行该程序,并注意两个孩子的输出如何交织。

该程序的可能输出是:

nova> ./procs

Parent process is born, my pid is 7847  
First child is born, my pid is 7848  
First child executes iteration: 1  
First child executes iteration: 2  
First child executes iteration: 3  
First child executes iteration: 4  
First child executes iteration: 5  
Second child is born, my pid is 7849  
Second child executes iteration 1  
Second child executes iteration 2  
Second child executes iteration 3  
First child executes iteration: 6  
Second child executes iteration 4  
Second child executes iteration 5  
Second child executes iteration 6  
First child executes iteration: 7  
Second child executes iteration 7  
Second child executes iteration 8  
Second child executes iteration 9  
Second child executes iteration 10  
Second child dies quietly.  
First child executes iteration: 8  
First child executes iteration: 9  
First child executes iteration: 10  
First child dies quietly.  
Parent process dies quietly.  

2 个答案:

答案 0 :(得分:3)

  1. 在退出之前,相应的父进程应等待(使用wait() or waitpid()或特定于平台的变体)让其子进程死亡。

  2. 并发执行需要调度程序有机会运行。您可以通过适当的睡眠(微睡眠,nano-sleep)操作来强制解决问题。一些系统调用会有所帮助(因此fflush(0)可能有所帮助)。


  3. #include <stdio.h>
    #include <sys/wait.h>
    #include <time.h>
    #include <unistd.h>
    
    int main(void)
    {
        int pid, i;
        struct timespec tw = { .tv_sec = 0, .tv_nsec = 10000000 };
        pid = fork();
        switch(pid)
        {
            case -1:
                printf("Fork error");
                break;
            case 0:
                printf("First child is born, my pid is %d\n", getpid());
                for(i=1; i<10; i++)
                {
                    printf("First child executes iteration %d\n", i);
                    nanosleep(&tw, 0);
                }
                printf("First child dies quietly.\n");
                break;
            default:
                printf("Parent process is born, my pid is %d\n", getpid());
                pid = fork();
                switch(pid)
                {
                    case -1:
                        printf("Fork error");
                        break;
                    case 0:
                        printf("Second child is born, my pid is %d\n", getpid());
                        for(i=1; i<10; i++)
                        {
                            printf("Second child executes iteration %d\n", i);
                            nanosleep(&tw, 0);
                        }
                        printf("Second child dies quietly.\n");
                        break;
                    default:
                        printf("Parent process waiting for children.\n");
                        int corpse;
                        int status;
                        while ((corpse = waitpid(0, &status, 0)) > 0)
                            printf("Child %d died with exit status 0x%.4X\n", corpse, status);
                        printf("Parent process dies quietly.\n");
                        break;
                }
        }
        return 0;
    }
    

    示例输出:

    Parent process is born, my pid is 46624
    First child is born, my pid is 46625
    First child executes iteration 1
    Parent process waiting for children.
    Second child is born, my pid is 46626
    Second child executes iteration 1
    First child executes iteration 2
    Second child executes iteration 2
    First child executes iteration 3
    Second child executes iteration 3
    First child executes iteration 4
    Second child executes iteration 4
    Second child executes iteration 5
    First child executes iteration 5
    Second child executes iteration 6
    First child executes iteration 6
    Second child executes iteration 7
    First child executes iteration 7
    Second child executes iteration 8
    First child executes iteration 8
    Second child executes iteration 9
    First child executes iteration 9
    First child dies quietly.
    Second child dies quietly.
    Child 46625 died with exit status 0x0000
    Child 46626 died with exit status 0x0000
    Parent process dies quietly.
    

    请注意,10毫秒延迟几乎强制交替执行。如果没有类似的东西,你会遇到系统及其调度程序的特性,很多现代机器太快了!

答案 1 :(得分:1)

fork()创建一个新的子进程,该进程独立于父进程运行。

如果您希望父进程等待子进程完成,您可以使用系统调用wait,传递一个PID,它将阻塞直到进程完成。

请参阅文章wait(System Call) on Wikipedia