c - SimGrid. Create subprocess with argument -
i have process declared in main.c
:
msg_function_register("dispatcher", dispatcher);
the dispatcher
function gets arguments deployment.xml
file:
int num = xbt_str_parse_int(argv[1], "invalid argument %s"); int id = xbt_str_parse_int(argv[2], "invalid argument %s");
then dispatcher
creates subprocess:
msg_process_create_with_arguments("ts", subprocess, null, msg_host_self(), agrc, argv);
how can pass num
, id
subprocess function?
int subprocess(int argc, char* argv){ return 0; }
how can call argv inside subprocess function?
you have 2 ways pass arguments msg processes:
either via argv;
or via
data
argument.
pass data via argv
in first solution, have create new argv
:
// create argv child process char** newargv = xbt_new(char*, 3); newargv[1] = xbt_strdup("ts"); newargv[2] = xbt_strdup(argv[0]); newargv[3] = xbt_strdup(argv[0]); // create process new argv: msg_process_create_with_arguments("ts", subprocess, null, msg_host_self(), 3, newargv);
with:
int subprocess(int argc, char* argv) { int num = xbt_str_parse_int(argv[1], "invalid argument %s"); int id = xbt_str_parse_int(argv[2], "invalid argument %s"); return 0; }
pass data via data
another solution pass data using data
parameter:
struct ts_data { int num; int id; }; struct ts_data* data = xbt_new(ts_data, 1); data->num = xbt_str_parse_int(argv[1], "invalid argument %s"); data->id = xbt_str_parse_int(argv[2], "invalid argument %s"); msg_process_create_with_arguments("ts", subprocess, data, msg_host_self(), 0, null
with:
int subprocess(int argc, char* argv) { struct ts_data* data = msg_process_get_data(msg_process_self()); int num = data->num; int id = data->id; free(data); return 0; }