Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
introduce sg_actor_create and use it in C examples
[simgrid.git] / examples / c / actor-kill / actor-kill.c
index 2d450bbbc6066c41cabe8735fe4561100a67ea00..417efd7fb06e2e32c492975aef59449ce1999be9 100644 (file)
@@ -25,7 +25,7 @@ static void victimA_fun(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv
   XBT_INFO("Suspending myself");
   sg_actor_suspend(sg_actor_self()); /* - First suspend itself */
   XBT_INFO("OK, OK. Let's work");    /* - Then is resumed and start to execute a task */
-  sg_actor_self_execute(1e9);
+  sg_actor_execute(1e9);
   XBT_INFO("Bye!"); /* - But will never reach the end of it */
 }
 
@@ -37,10 +37,11 @@ static void victimB_fun(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv
 static void killer_fun(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
 {
   XBT_INFO("Hello!"); /* - First start a victim process */
-  sg_actor_t victimA = sg_actor_init("victim A", sg_host_by_name("Fafard"));
-  sg_actor_start(victimA, victimA_fun, 0, NULL);
-  sg_actor_t victimB = sg_actor_init("victim B", sg_host_by_name("Jupiter"));
-  sg_actor_start(victimB, victimB_fun, 0, NULL);
+  sg_actor_t victimA = sg_actor_create("victim A", sg_host_by_name("Fafard"), victimA_fun, 0, NULL);
+
+  sg_actor_t victimB = sg_actor_create("victim B", sg_host_by_name("Jupiter"), victimB_fun, 0, NULL);
+  sg_actor_ref(victimB); // We have to take that ref because victimB will end before we try to kill it
+
   sg_actor_sleep_for(10.0);
 
   XBT_INFO("Resume the victim A"); /* - Resume it from its suspended state */
@@ -53,11 +54,11 @@ static void killer_fun(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[
 
   XBT_INFO("Kill victimB, even if it's already dead"); /* that's a no-op, there is no zombies in SimGrid */
   sg_actor_kill(victimB); // the actor is automatically garbage-collected after this last reference
+  sg_actor_unref(victimB); // Release the ref taken on victimB to avoid to leak memory
   sg_actor_sleep_for(1.0);
 
   XBT_INFO("Start a new actor, and kill it right away");
-  sg_actor_t victimC = sg_actor_init("victim C", sg_host_by_name("Jupiter"));
-  sg_actor_start(victimC, victimA_fun, 0, NULL);
+  sg_actor_t victimC = sg_actor_create("victim C", sg_host_by_name("Jupiter"), victimA_fun, 0, NULL);
   sg_actor_kill(victimC);
   sg_actor_sleep_for(1.0);
 
@@ -78,8 +79,7 @@ int main(int argc, char* argv[])
   simgrid_load_platform(argv[1]);
 
   /* - Create and deploy killer process, that will create the victim process  */
-  sg_actor_t killer = sg_actor_init("killer", sg_host_by_name("Tremblay"));
-  sg_actor_start(killer, killer_fun, 0, NULL);
+  sg_actor_create("killer", sg_host_by_name("Tremblay"), killer_fun, 0, NULL);
 
   simgrid_run();