简体   繁体   English

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

[英]Why aren't my processes running concurrently?

My questions are: 我的问题是:

1.) How can I get the parent process to always die last? 1.)如何让父进程永远死? I get that this isn't being accomplished because the parent is the first pid that gets ran, but I don't know how to change it. 我知道这没有完成,因为父亲是第一个运行的pid,但我不知道如何改变它。

2.) How do I get my child processes to execute at the same time like his? 2.)如何让我的子进程像他一样同时执行? I've even bumped the number up really high to see if it was just a coincidence, but it appears to not be. 我甚至把这个数字提高到很高,看它是不是巧合,但似乎不是。

EDIT: SOLUTIONS 编辑:解决方案

1.) added wait(NULL) twice in inner Default 1.)在内部默认值中添加两次wait(NULL)
2.) was happening. 2.)正在发生。 used sleep(1) to prove it. 用睡眠(1)来证明它。

My code is as follows 我的代码如下

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

My output always looks like this: 我的输出总是如下所示:

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.

My assignment is: 我的任务是:

Write a C program ("procs.c") that creates three processes: a parent process that creates two child processes. 编写一个C程序(“procs.c”),它创建三个进程:一个创建两个子进程的父进程。

The first child should do the following: 第一个孩子应该做以下事情:

  • display "First child is born, my pid is ..." 显示“第一个孩子出生,我的pid是......”

  • display ten times the message "First child executes iteration X", where X is the number of the iteration 显示十次“第一个子进行迭代X”的消息,其中X是迭代次数

  • display "First child dies quietly." 显示“第一个孩子安静地死去。”

The second child should do the following: 第二个孩子应该做以下事情:

  • display "Second child is born, my pid is ..." 显示“第二个孩子出生,我的pid是......”

  • display ten times the message "Second child executes iteration X", where X is the number of the iteration 显示十次消息“第二个子进行迭代X”,其中X是迭代次数

  • display "Second child dies quietly." 显示“第二个孩子安静地死去。”

The parent process should do the following: 父进程应执行以下操作:

  • display "Parent process is born, my pid is ..." 显示“父进程诞生,我的pid是......”

  • create the first child 创造第一个孩子

  • create the second child 创造第二个孩子

  • display "Parent process dies quietly." 显示“父进程悄然死亡”。

Compile the program using gcc and name the executable "procs". 使用gcc编译程序并命名可执行文件“procs”。 Execute the program several times and notice how the output of the two children interlace. 多次执行程序并注意两个孩子的输出如何交错。

A possible output of this program is: 该程序的可能输出是:

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.
  1. The appropriate parent process should wait (using wait() or waitpid() or a platform-specific variant) for its children to die before exiting. 相应的父进程应该等待(使用wait()waitpid()或特定于平台的变体),以使其子进程在退出之前死亡。

  2. Concurrent execution requires the scheduler to get a chance to run. 并发执行要求调度程序有机会运行。 You can force the issue with appropriate sleep (micro-sleep, nano-sleep ) operations. 您可以通过适当的睡眠(微睡眠, 纳米睡眠 )操作来强制解决问题。 Some system calls will help (so fflush(0) might help). 一些系统调用会有所帮助(所以fflush(0)可能有帮助)。


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

Example output: 示例输出:

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.

Note that the 10 millisecond delays almost force alternating execution. 请注意,10毫秒延迟几乎强制交替执行。 Without something similar, you are stuck with the idiosyncrasies of your system and its scheduler, and many a modern machine is just too fast! 如果没有类似的东西,你会遇到系统及其调度程序的特性,而且许多现代机器太快了!

fork() creates a new child process, which run's independently from the parent. fork()创建一个新的子进程,它独立于父进程运行。

If you want the parent process to wait for the children to finish, you can use the system call wait , passing a PID, which will block until the process has finished. 如果您希望父进程等待子进程完成,您可以使用系统调用wait ,传递一个PID,它将阻塞直到进程完成。

See the article wait(System Call) on Wikipedia 请参阅Wikipedia上的文章wait(系统调用)

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

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