Search code examples
csimgrid

SimGrid. Create subprocess with argument


I have process declared in main.c:

MSG_function_register("dispatcher", dispatcher);

The dispatcher function gets arguments from 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 a subprocess:

MSG_process_create_with_arguments("TS", subprocess, NULL, MSG_host_self(), agrc, argv);

How can I pass num and id to subprocess function?

int subprocess(int argc, char* argv){
    return 0;
}

How can I call this argv inside from subprocess function?


Solution

  • You have 2 ways to pass arguments to MSG processes:

    • either via the argv;

    • or via the data argument.

    Pass the data via argv

    In the first solution, you have to create a new argv:

    // Create argv for 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 with 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 the data via data

    Another solution is to pass the data using the 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;
    }