File: actor-kill.c

package info (click to toggle)
simgrid 4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 39,192 kB
  • sloc: cpp: 124,913; ansic: 66,744; python: 8,560; java: 6,773; fortran: 6,079; f90: 5,123; xml: 4,587; sh: 2,194; perl: 1,436; makefile: 111; lisp: 49; javascript: 7; sed: 6
file content (86 lines) | stat: -rw-r--r-- 2,887 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* Copyright (c) 2007-2025. 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 "simgrid/actor.h"
#include "simgrid/engine.h"
#include "simgrid/host.h"

#include "xbt/log.h"
#include "xbt/sysdep.h"

XBT_LOG_NEW_DEFAULT_CATEGORY(actor_kill, "Messages specific for this example");

static void victim_on_exit(int ignored1, void* ignored2)
{
  XBT_INFO("I have been killed!");
}

static void victimA_fun(int argc, char* argv[])
{
  sg_actor_on_exit(&victim_on_exit, NULL);
  XBT_INFO("Hello!");
  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_execute(1e9);
  XBT_INFO("Bye!"); /* - But will never reach the end of it */
}

static void victimB_fun(int argc, char* argv[])
{
  XBT_INFO("Terminate before being killed");
}

static void killer_fun(int argc, char* argv[])
{
  XBT_INFO("Hello!"); /* - First start a victim actor */
  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 */
  sg_actor_resume(victimA);
  sg_actor_sleep_for(2.0);

  XBT_INFO("Kill the victim A"); /* - and then kill it */
  sg_actor_kill(victimA);
  sg_actor_sleep_for(1.0);

  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_create("victim C", sg_host_by_name("Jupiter"), &victimA_fun, 0, NULL);
  sg_actor_kill(victimC);
  sg_actor_sleep_for(1.0);

  XBT_INFO("Killing everybody but myself");
  sg_actor_kill_all();

  XBT_INFO("OK, goodbye now. I commit a suicide.");
  sg_actor_exit();

  XBT_INFO("This line will never get displayed: I'm already dead since the previous line.");
}

int main(int argc, char* argv[])
{
  simgrid_init(&argc, argv);
  xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s platform.xml\n", argv[0], argv[0]);

  simgrid_load_platform(argv[1]);

  /* - Create and deploy killer actor, that will create the victim actor  */
  sg_actor_create("killer", sg_host_by_name("Tremblay"), &killer_fun, 0, NULL);

  simgrid_run();

  return 0;
}