有没有办法改变用fork()创建的子进程的执行顺序? [英] Is there a good way to alter execution order of child processes created with fork()?

查看:168
本文介绍了有没有办法改变用fork()创建的子进程的执行顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找创建可以控制其处理顺序的子进程.

I am looking for creating child processes for which I can control their order of processing.

简单的例子:

  • 父母用叉子创造了两个孩子
  • 儿童
  1. 第一个孩子打印消息2"
  2. 第二个孩子打印消息1"

  • 完成后,父级会打印结束"
  • 由于我们无法确定将首先执行哪个进程,因此最终结果很有可能是:

    Because of the fact that we can't know for sure which process will be executed first, there are high chances that the final result would be:

    消息2

    消息1

    结束

    我正在尝试确保第二个孩子在第一个孩子之前执行打印,而父母在所有孩子之后执行打印.

    I am trying to make sure that the second child executes the print before the first child and that the parent executes its print after all the children.

    对于父母来说,使用wait()/waitpid()函数非常容易.但是,带孩子们似乎更困难.

    For the parent it's quite easy with the wait()/waitpid() functions. However it seems harder with the children.

    这是我为实现目标而执行的想法:

    Here is an implementation of my ideas to achieve the objective:

    (注意:我对创建子进程还很陌生,在此实现中我可能会误解了事情)

    (note: I'm still quite new to the creation of child processes and I may have misunderstood things in this implementation)

    #include <stdlib.h>
    #include <stdio.h>
    
    #include <sys/types.h>
    #include <signal.h>
    #include <unistd.h>
    
    static int init = 0;
    
    void setInitFinished(int sig)
    {
        if (sig == SIGUSR1)
            init = 1;
    }
    
    int main()
    {
        signal(SIGUSR1, setInitFinished);
        
        pid_t pid1, pid2;
        int status1, status2;
        
        // CHILD 1
        if (!(pid1 = fork()))
        {
            while (!init); // Waiting all children to be initiated
    
            // Once all children created, we wait for child 2 to print its message
            int pidOfChild2 = getpid()+1; // I checked, the PID is correct
            waitpid(pidOfChild2, &status1, 0);
            
            printf("MESSAGE 2\n");
            exit(0);
        }
        
        // CHILD 2
        if (!(pid2 = fork()))
        {
            while (!init); // Waiting all children to be initiated
            
            // No need to wait since it's the first message to be printed
            printf("MESSAGE 1\n");
            exit(0);
        }
        
        // PARENT
    
        // All children have been created, tell it to all the children
        kill(pid2,SIGUSR1);
        kill(pid1,SIGUSR1);
        
        // When every child has finished its work, continue parent process
        waitpid(pid1, &status1, 0);
        waitpid(pid2, &status2, 0);
        
        printf("Parent end\n");
        
        return 0;
    }
    

    在孩子1中,我试图通过 waitpid(pidOfChild2,...)等待孩子2;但这似乎不起作用.

    In the child 1 I am trying to wait for the Child 2 with waitpid(pidOfChild2, ...); but it doesn't seem to work.

    我仍然在发现fork的功能,所以我很确定会误会这里的很多事情.

    I'm still discovering the fork functionalities so I'm pretty sure to misunderstand a lot of things here.

    注意:我想避免使用sleep(),它可以工作,但效果不佳

    NB: I want to avoid using sleep(), it could work but it's not pretty

    推荐答案

    您需要使用实际的进程间通信来实现此目的.

    You need to use actual inter-process communication to achieve this.

    您似乎认为waitpid()函数与等待进程打印输出有关,但这根本不起作用.

    You seem to think that the waitpid() function has something to do with waiting for a process to print output, but that's not at all what it does.

    在父级中创建信号量,并将其传递给两个孩子,并让他们一个孩子在打印之前等待信号量,另一个孩子在完成打印后向信号量发送消息.

    Create a semaphore in the parent, pass it to both children, and have one child wait on the semaphore before printing and the other one messaging the semaphore after it's done printing.

    这篇关于有没有办法改变用fork()创建的子进程的执行顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆