File: synchro-semaphore.c

package info (click to toggle)
simgrid 3.21%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 21,972 kB
  • sloc: cpp: 88,193; ansic: 69,244; fortran: 6,089; f90: 5,162; xml: 4,861; java: 4,250; perl: 2,056; python: 1,193; sh: 1,159; makefile: 57; sed: 6
file content (73 lines) | stat: -rw-r--r-- 2,085 bytes parent folder | download
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
/* Copyright (c) 2013-2018. 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/msg.h"

XBT_LOG_NEW_DEFAULT_CATEGORY(msg_semaphore_example, "Messages specific for this msg example");

msg_sem_t sem;

static int peer(int argc, char* argv[]){
  int i = 0;
  while(i < argc) {
    double wait_time = xbt_str_parse_double(argv[i],"Invalid wait time: %s");
    i++;
    MSG_process_sleep(wait_time);
    XBT_INFO("Trying to acquire %d", i);
    MSG_sem_acquire(sem);
    XBT_INFO("Acquired %d", i);

    wait_time = xbt_str_parse_double(argv[i], "Invalid wait time: %s");
    i++;
    MSG_process_sleep(wait_time);
    XBT_INFO("Releasing %d", i);
    MSG_sem_release(sem);
    XBT_INFO("Released %d", i);
  }
  MSG_process_sleep(50);
  XBT_INFO("Done");

  return 0;
}

int main(int argc, char* argv[])
{
  MSG_init(&argc, argv);
  MSG_create_environment(argv[1]);

  msg_host_t h = MSG_host_by_name("Fafard");

  sem = MSG_sem_init(1);
  char** aliceTimes = xbt_new(char*, 9);
  aliceTimes[0] = xbt_strdup("0");
  aliceTimes[1] = xbt_strdup("1");
  aliceTimes[2] = xbt_strdup("3");
  aliceTimes[3] = xbt_strdup("5");
  aliceTimes[4] = xbt_strdup("1");
  aliceTimes[5] = xbt_strdup("2");
  aliceTimes[6] = xbt_strdup("5");
  aliceTimes[7] = xbt_strdup("0");
  aliceTimes[8] = NULL;

  char** bobTimes = xbt_new(char*, 9);
  bobTimes[0] = xbt_strdup("0.9");
  bobTimes[1] = xbt_strdup("1");
  bobTimes[2] = xbt_strdup("1");
  bobTimes[3] = xbt_strdup("2");
  bobTimes[4] = xbt_strdup("2");
  bobTimes[5] = xbt_strdup("0");
  bobTimes[6] = xbt_strdup("0");
  bobTimes[7] = xbt_strdup("5");
  bobTimes[8] = NULL;

  MSG_process_create_with_arguments("Alice", peer, NULL, h, 8, aliceTimes);
  MSG_process_create_with_arguments("Bob", peer, NULL, h, 8, bobTimes);

  msg_error_t res = MSG_main();
  MSG_sem_destroy(sem);
  XBT_INFO("Finished\n");
  return (res != MSG_OK);
}