2016-08-02 10 views
0

100個のプロセスを起動するプロセスがあります:SimGridでプロパティを同時に正しく設定する方法は?

for (int i = 0; i < 100; ++i) { 
     MSG_process_create("w", executor, NULL, MSG_host_self()); 
    } 

executorがサンプルタスクを作成し、それを実行します。したがって、同時に100タスクが実行され、同時にタスクが完了します。アクティブのステップ量によって(私は予想通り)

[ 0.000000] (2:[email protected]) Active process amount is 1 
[ 0.000000] (3:[email protected]) Active process amount is 2 
[ 0.000000] (4:[email protected]) Active process amount is 3 
.................................................... 
[ 0.000000] (101:[email protected]) Active process amount is 100 

の各プロセスが減少するはずであるステップ:すべてのexecutorのプロセスが同時に開始のすべてがOKですので

:プロセスの数を監視するために私はvoid plusOneActiveProcess()void minusOneActiveProcess()を持っていますタスクを実行するためにexecutorが完了したときのプロセス。しかし、それは起こらなかった:

[100.000000] (101:[email protected]) Active process amount is 99 
[100.000000] (2:[email protected]) Active process amount is 99 
[100.000000] (3:[email protected]) Active process amount is 99 
.................................................... 
[100.000000] (100:[email protected]) Active process amount is 99 

それを正しく行うには?

これは機能plusOneActiveProcess()のコード、minusOneActiveProcessexecutor()である:それは面白い、

int executor(){ 
    plusOneActiveProcess(); 
    msg_error_t a = MSG_task_execute(MSG_task_create("", 1e9, 0, NULL)); 
    minusOneActiveProcess(); 
    MSG_process_kill(MSG_process_self()); 
    return 0; 
} 

void plusOneActiveProcess(){ 
    char kot[50]; 
    long number; 
    number = xbt_str_parse_int(MSG_host_get_property_value(MSG_host_self(), "activeProcess"), "error"); 
    number++; 
    sprintf(kot, "%ld", number); 
    MSG_host_set_property_value(MSG_host_self(), "activeProcess", xbt_strdup(kot), NULL); 
    XBT_INFO("Active process amount is %s", MSG_host_get_property_value(MSG_host_self(), "activeProcess")); 
    memset(kot, 0, 50); 
} 

void minusOneActiveProcess(){ 
    char kot[50]; 
    long number; 
    number = xbt_str_parse_int(MSG_host_get_property_value(MSG_host_self(), "activeProcess"), "error"); 
    number--; 
    sprintf(kot, "%ld", number); 
    MSG_host_set_property_value(MSG_host_self(), "activeProcess", xbt_strdup(kot), NULL); 
    //XBT_INFO("Active process amount is %s", MSG_host_get_property_value(MSG_host_self(), "activeProcess")); 
    memset(kot, 0, 50); 
} 

答えて

1

なるほど。つまり、set_property()はSimGrid simcallなので、set_property()へのすべての呼び出しが常に同じ順序で線形化されるという保証があります。しかし、get_property()はスケジューリングラウンドの始めに値を返す通常の関数呼び出しです。すべてのプロセスが同じスケジューリングラウンドで値を要求するので、それらはすべて同じ値(100)を取得し、デクリメントしてからsimcallを実行して新しい値をプッシュします(すべてのプロセスは99をプッシュします)。

get + setをアトミックにしたい、つまり、他の誰かがget_property()を実行する前に、プロセスが更新された値をプッシュするようにします。そのために、SimGrid Mutexesなどを使用することをお勧めします。

関連する問題