有一个启动100个进程的进程:
for (int i = 0; i < 100; ++i) {
MSG_process_create("w", executor, NULL, MSG_host_self());
}executor创建示例任务并执行它。因此,有100个进程同时开始执行任务,并同时完成任务。为了监控进程的数量,我使用了void plusOneActiveProcess()和void minusOneActiveProcess()
由于所有executor进程都是同时启动的,所以一切正常:
[ 0.000000] (2:w@Worker) Active process amount is 1
[ 0.000000] (3:w@Worker) Active process amount is 2
[ 0.000000] (4:w@Worker) Active process amount is 3
....................................................
[ 0.000000] (101:w@Worker) Active process amount is 100当executor完成执行任务时,每个进程都应该(正如我所预期的)逐步减少活动进程的数量。但这并没有发生:
[100.000000] (101:w@Worker) Active process amount is 99
[100.000000] (2:w@Worker) Active process amount is 99
[100.000000] (3:w@Worker) Active process amount is 99
....................................................
[100.000000] (100:w@Worker) Active process amount is 99如何正确地做到这一点?
这是函数plusOneActiveProcess()、minusOneActiveProcess、executor()的代码
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);
}发布于 2016-08-02 23:34:05
啊哈,这很有趣。问题是set_property()是一个SimGrid simcall,所以您可以保证对set_property()的所有调用都将始终以相同的顺序线性化。但是get_property()是一个常规的函数调用,它返回调度循环开始时的值。由于所有进程都在同一调度轮次中询问该值,因此它们都获得相同的值(即100),递减该值,然后执行simcall以推送新值(所有进程都希望推送99)。
您希望使get+set成为原子的,即确保任何进程在其他进程执行get_property()之前推送更新后的值。为此,我建议使用SimGrid Mutexes或其他工具。
https://stackoverflow.com/questions/38722948
复制相似问题