File: ffmaster.c

package info (click to toggle)
freefem%2B%2B 4.11%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 57,196 kB
  • sloc: cpp: 225,276; ansic: 29,162; makefile: 4,046; sh: 2,422; fortran: 1,115; perl: 865; pascal: 452; awk: 295; f90: 32; exp: 16
file content (136 lines) | stat: -rw-r--r-- 3,400 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * Example of coupling c program and freemfem++ script
 * with mmap and semaphore
 *
 * the c code is    :   ffmaster.c
 * the ff++ code is : ffslave.edp
 * and here FreeFem++ is a slave process
 * the compile step is
 *
 * cc -c libff-mmap-semaphore.c
 # on linux, unix, 
 * cc ffmaster.c -o ffmaster  libff-mmap-semaphore.o -g
 # on win32 
 * cc ffmaster.c -o ffmaster  libff-mmap-semaphore.o -g  ../../3rdparty/lib/libpthread-google.a

 #build the freefem++ plugin
 * ff-c++ -auto ff-mmap-semaphore.cpp
 # launch
 # ./ffmaster
 #
 #
 # F. Hecht Feb. 2018   Frederic.Hecht@upmc.fr
 */

/*
 *
 * This file is part of Freefem++
 *
 * Freefem++ is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * Freefem++  is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Freefem++; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "libff-mmap-semaphore.h"
#if defined ( _WIN32 )
#  include <windows.h>
#  include <process.h>
#  define NO_STDIO_REDIRECT
#else
# include <unistd.h>
#endif
#include <stdlib.h>
#include <stdio.h>


ff_Psem sem_ff, sem_c;    // the semaphore for mutex
/*
 * Psemaphore smff("ff-slave");
 * Psemaphore smc("ff-master");
 * Pmmap sharedata("shared-data");
 *
 */
int main(int argc, const char **argv) {
  int debug = 0;
  ff_Pmmap shd;
  double cff, rff;
  long status;
  int i, ret;


  if (argc > 1) {
    debug = atoi(argv[1]);
  }

  ff_mmap_sem_verb = debug;

  sem_ff = ffsem_malloc( );
  sem_c = ffsem_malloc( );
  shd = ffmmap_malloc( );

  ffsem_init(sem_ff, "ff-slave1", 1);
  ffsem_init(sem_c, "ff-master1", 1);
  ffmmap_init(shd, "shared-data", 1024);

  status = 1;
  ffmmap_write(shd, &status, sizeof(status), 8);
  ffmmap_msync(shd, 0, 32);

  char ff[1024];
#ifdef _WIN32
  sprintf( ff, "%d", debug );
  ret = spawnl( P_NOWAIT,
                "..\\FreeFem++.exe",
                "..\\FreeFem++.exe",
                "..\\examples\\plugin\\ffslave.edp",
                "-nw", "-ns", "-v", ff, NULL );
#else
  sprintf(ff, "FreeFem++ /usr/share/doc/freefem++/examples/plugin/ffslave.edp -nw -ns -v %d&", debug);
  ret = system(ff);    // Lauch FF++ in batch no graphique
#endif
  if (ret == -1) printf("system function error\n");
  if (debug) {
    printf(" cc: before wait\n");
  }

  if (debug) {
    printf(" cc: before wait 0 ff\n");
  }

  ffsem_wait(sem_ff);

  for (i = 0; i < 10; ++i) {
    printf(" iter : %d \n", i);
    cff = 10 + i;
    ffmmap_write(shd, &cff, sizeof(cff), 0);
    ffsem_post(sem_c);

    if (debug) {
      printf(" cc: before wait 2\n");
    }

    ffsem_wait(sem_ff);
    ffmmap_read(shd, &rff, sizeof(rff), 16);
    printf(" iter = %d rff= %f\n", i, rff);
  }

  status = 0;    // Fin
  ffmmap_write(shd, &status, sizeof(status), 8);
  ffsem_post(sem_c);
  printf("Fin Master \n");
  ffsem_wait(sem_ff);
  ffsem_del(sem_ff);
  ffsem_del(sem_c);
  ffmmap_del(shd);
  return 0;
}