SimGrid
Lesson 3: Passing arguments to the processes (in SG)

Table of Contents


The most problematic issue with the code of previous lesson is that it does not work in RL since we hardcoded the server hostname in the client code. We will thus learn you how to pass arguments to your processes to overcome this situation.

Using command line arguments from user code

In RL, the situation is quite simple: we just have to use the command line arguments as we would do in a usual C program. In the server, only change concern the opennong of the master socket:

  mysock = gras_socket_server(atoi(argv[1]));

In the client, we only need to change the way we open the client socket:

  toserver = gras_socket_client(argv[1], atoi(argv[2]));

The rest of the program remains inchanged.

Passing command line arguments in deployment files

At this point, the problem is to pass arguments to the processes in SG. Fortunately, it is quite simple. You just have to edit your deployment file so that it reads:

<?xml version='1.0'?>
<!DOCTYPE platform SYSTEM "http://simgrid.gforge.inria.fr/simgrid.dtd">
<platform version="3">
  <process host="Jacquelin" function="server">
    <argument value="12345"/>
  </process>
  <process host="Boivin" function="client">
    <argument value="Jacquelin"/>
    <argument value="12345"/>
  </process>
</platform>

The syntax should be self-explanatory at this point.

Recaping everything together

The whole program now reads:

/* Copyright (c) 2006, 2007, 2010. The SimGrid Team.
 * All rights reserved.                                                     */

/* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */

#include <stdio.h>
#include <gras.h>

int server(int argc, char *argv[])
{
  gras_socket_t mysock;         /* socket on which I listen */
  gras_socket_t toclient;       /* socket used to write to the client */

  gras_init(&argc, argv);

  gras_msgtype_declare("hello", NULL);
  mysock = gras_socket_server(atoi(argv[1]));

  gras_msg_wait(60, "hello", &toclient, NULL /* no payload */ );

  fprintf(stderr, "Cool, we received the message from %s:%d.\n",
          gras_socket_peer_name(toclient),
          gras_socket_peer_port(toclient));

  gras_exit();
  return 0;
}

int client(int argc, char *argv[])
{
  gras_socket_t mysock;         /* socket on which I listen */
  gras_socket_t toserver;       /* socket used to write to the server */

  gras_init(&argc, argv);

  gras_msgtype_declare("hello", NULL);
  mysock = gras_socket_server_range(1024, 10000, 0, 0);

  fprintf(stderr, "Client ready; listening on %d\n",
          gras_socket_my_port(mysock));

  gras_os_sleep(1.5);           /* sleep 1 second and half */
  toserver = gras_socket_client(argv[1], atoi(argv[2]));

  gras_msg_send(toserver, "hello", NULL);
  fprintf(stderr, "That's it, we sent the data to the server on %s\n",
          gras_socket_peer_name(toserver));

  gras_exit();
  return 0;
}

And here is the output:

$ ./test_server 12345 & ./test_client 127.0.0.1 12345
Client ready; listening on 1024
That's it, we sent the data to the server on 127.0.0.1
[arthur:client:(27443) 0.000018] [gras/INFO] Exiting GRAS
Cool, we received the message from 127.0.0.1:1024.
[arthur:server:(27441) 0.000012] [gras/INFO] Exiting GRAS
$
$ ./test_simulator platform.xml test.xml
Client ready; listening on 1024
That's it, we sent the data to the server on Jacquelin
[Boivin:client:(2) 0.000000] [gras/INFO] Exiting GRAS
Cool, we received the message from Boivin:1024.
[Jacquelin:server:(1) 0.000000] [gras/INFO] Exiting GRAS
$

Go to Lesson 4: Attaching callbacks to messages


Back to the main Simgrid Documentation page The version of Simgrid documented here is v3.6.1.
Documentation of other versions can be found in their respective archive files (directory doc/html).
Generated for SimGridAPI by doxygen