简体   繁体   English

fork 和 execve 应用其中一些与 condition_variable 和其他同时进行

[英]fork and execve applications some of them in order with condition_variable and the others simultaneously

I am trying to execute multiple applications with fork() and execve().我正在尝试使用 fork() 和 execve() 执行多个应用程序。 and some of them has a dependency to each other.他们中的一些人相互依赖。 So for example, I want to make it execute A, B, C at the same time and make it wait until B is in "Running" status before execute C when the scenario is like below.因此,例如,当场景如下所示时,我想让它同时执行 A、B、C,并让它等到 B 处于“正在运行”状态,然后再执行 C。

applications
A
B-C   (C depends on B)
D

at the very first time.在第一时间。 I came up with very primitive solution like below.我想出了非常原始的解决方案,如下所示。 fork and execute app and wait until its status changed to running state. fork 并执行 app 并等待其状态变为运行状态。 (the application has Initial state and everything are ready it sends message that it is on the running status to state_mgr) (应用程序具有初始状态并且一切准备就绪,它向 state_mgr 发送处于运行状态的消息)

// app_starter.cpp
CreateAppProcess(...) {
   ...
   child = fork();
   ...
   if (child == 0) {
      execve(name, argv, argc);
      ...
   }
   ...
}


StartApplication(AppInstance& app) {
...
    CreateAppProcess(app->name, app->argv, app->argc);
...
}

Use the function "StartApplication()" in this way.以这种方式使用函数“StartApplication()”。

//app_starter.cpp

// 1. declare AppInstance array.
AppInstance app_array[10];  
// 2. set AppInstances in the array.
...
// 3. start apps
for (int i = 0 ; i < 10; i++) {
   StartApplication(app_array[i]);
}

// 4. wait until "execve"d app is on Running state.
for (int i = 0; i < 1000000000; i++) 
{
    return_value = app_ins.GetAppState(app->name);
    if(return_value == State::Running) break;
}

The solution above has a three problems.上述解决方案存在三个问题。

  1. it can only execute one application at a time.它一次只能执行一个应用程序。
  2. should wait until previous application is on "Running" state, even though it is not is dependency.应该等到前一个应用程序处于“运行”状态,即使它不是依赖项。
  3. for loop keep consuming the resources executing GetAppState() repeatedly. for 循环不断消耗重复执行 GetAppState() 的资源。

So I am trying to fix problems one by one but I stuck at the very first soulution for 3. - condition_variable.所以我试图一个一个地解决问题,但我坚持第一个解决方案 3。-condition_variable。

// app_instance.cpp
std::condition_variable g_app_cv;
std::mutex g_app_cv_mtx;
...
ReceiveAppStateMsg() {
    while(_quit) {
        ret_ = mq_receive(rcv_handler, buffer, size_, 0);
        ...
        if (msg_rcvd == State::Running) {
            ...
            g_app_cv.notify_one();
        }
    }
}

//app_starter.cpp
extern std::condition_variable g_app_cv;
extern std::mutex g_app_cv_mtx;

// 1. declare AppInstance array.
AppInstance app_array[10];  
// 2. set AppInstances in the array.
...
// 3. start apps  - change for loop to condition_variable.
for (int i = 0 ; i < 10; i++) {
   StartApplication(app_array[i]);
}

// 4. wait until "execve"d app is on Running state.
{
    std::unique_lock<std::mutex> lk(g_app_cv_mtx);
    g_app_cv.wait_for(lk, std::chrono::milliseconds(1000));
}

in conclusion I need some insights about...总之,我需要一些关于……的见解

  1. How to use condition_variable across the multiple cpp files.如何在多个 cpp 文件中使用 condition_variable。

  2. The right way fork() and exec() with std::condition_variable.使用 std::condition_variable 的正确方法 fork() 和 exec()。

    • want to know my solution is the right one.想知道我的解决方案是正确的。
  3. How to fork() and exec() in simultaneously or with not much delay.如何同时或没有太多延迟地 fork() 和 exec() 。

execution order timeline I want
start --- |end
--------------
 A----|
  B----|C----|
   D----|
--------------

Thank you for read this long question.感谢您阅读这么长的问题。

You need to allocated the mutex as well as the condition variable in a shared memory region between the processes.您需要在进程之间的共享内存区域中分配互斥锁和条件变量。 You also need to set the attribute of your mutex and cond vars to be PTHREAD_PROCESS_SHARED您还需要将互斥锁和 cond 变量的属性设置为 PTHREAD_PROCESS_SHARED

Howto Share memory: link如何共享内存: 链接

However, for the purpose similar to yours, i would prefer to write a small process life cycle module (built entirely on top of IPC using domain sockets ).但是,出于与您类似的目的,我更愿意编写一个小型进程生命周期模块(使用域套接字完全构建在 IPC 之上)。 You can divide the life cycle of your process into : INIT, RUNNING, ALIVE, STOPPED, DEAD ( based on what you wanna do, i am just giving you clues ).您可以将流程的生命周期划分为:INIT、RUNNING、ALIVE、STOPPED、DEAD(基于您想要做什么,我只是给您提供线索)。 As soon as as your process reaches a stage in it's lifecycle you can publish out an event notifying the same.一旦您的流程达到其生命周期的某个阶段,您就可以发布一个通知相同的事件。 The other process can just sit block waiting for that state to be realized in the co-operating process.另一个进程可以只是坐着等待在协作进程中实现该状态。 A simple heart beat mechanism can determine whether the other process is ALIVE or dead.一个简单的心跳机制可以确定另一个进程是活着还是死了。 The other stages can be notified via publishing out over IPC ( preferable domain sockets )其他阶段可以通过在 IPC 上发布来通知(首选域套接字)

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

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