AMF-Placer  2.0
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
ExternalProcessFunc.h
Go to the documentation of this file.
1 
26 #ifndef _EXTERNALPROCESSFUNC
27 #define _EXTERNALPROCESSFUNC
28 
29 #include "strPrint.h"
30 #include <assert.h>
31 #include <fcntl.h>
32 #include <functional>
33 #include <iomanip>
34 #include <iostream>
35 #include <limits.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <string>
40 #include <sys/shm.h>
41 #include <sys/stat.h>
42 #include <sys/types.h>
43 #include <sys/wait.h>
44 #include <unistd.h>
45 #include <unordered_set>
46 
54 {
55  public:
63  ExternalProcessFunc(std::string executablePath, unsigned int shmSize, bool verbose)
65  {
67  }
68 
70  {
72  }
73 
79  {
80  sharedid = rand() + getpid();
81  shmid = shmget((key_t)sharedid, shmSize, 0666 | IPC_CREAT);
82 
83  if (shmid == -1)
84  {
85  fprintf(stderr, "shmget failed\n");
86  exit(EXIT_FAILURE);
87  }
88 
89  shm = shmat(shmid, (void *)0, 0);
90  if (shm == (void *)-1)
91  {
92  fprintf(stderr, "shmat failed\n");
93  exit(EXIT_FAILURE);
94  }
95  // printf("Memory attached at %llX\n", (unsigned long long)shm);
96  }
97 
99  {
100  if (shmdt(shm) == -1)
101  {
102  fprintf(stderr, "shmdt failed\n");
103  exit(EXIT_FAILURE);
104  }
105 
106  if (shmctl(shmid, IPC_RMID, 0) == -1)
107  {
108  fprintf(stderr, "shmctl(IPC_RMID) failed\n");
109  exit(EXIT_FAILURE);
110  }
111  }
112 
113  inline void *getSharedMemory()
114  {
115  return shm;
116  }
117 
118  void execute()
119  {
120  if (verbose)
121  print_cmd((executablePath + " " + std::to_string(sharedid) + " " + std::to_string(shmSize)));
122  int rc = system((executablePath + " " + std::to_string(sharedid) + " " + std::to_string(shmSize)).c_str());
123  assert(rc >= 0 && "the execution should be ended successfully but not.");
124  }
125 
126  private:
127  int shmid;
128  void *shm = NULL;
129  std::string executablePath;
130  unsigned int shmSize;
131  bool verbose;
132  key_t sharedid;
133 };
134 
135 #endif
ExternalProcessFunc::shmSize
unsigned int shmSize
Definition: ExternalProcessFunc.h:130
ExternalProcessFunc
ExternalProcessFunc is a wrapper of an external exectable for multi-process scenario with shared memo...
Definition: ExternalProcessFunc.h:54
ExternalProcessFunc::deleteShareMemory
void deleteShareMemory()
Definition: ExternalProcessFunc.h:98
ExternalProcessFunc::ExternalProcessFunc
ExternalProcessFunc(std::string executablePath, unsigned int shmSize, bool verbose)
Construct a new External Process Func object.
Definition: ExternalProcessFunc.h:63
ExternalProcessFunc::sharedid
key_t sharedid
Definition: ExternalProcessFunc.h:132
ExternalProcessFunc::verbose
bool verbose
Definition: ExternalProcessFunc.h:131
ExternalProcessFunc::shmid
int shmid
Definition: ExternalProcessFunc.h:127
ExternalProcessFunc::executablePath
std::string executablePath
Definition: ExternalProcessFunc.h:129
ExternalProcessFunc::~ExternalProcessFunc
~ExternalProcessFunc()
Definition: ExternalProcessFunc.h:69
strPrint.h
ExternalProcessFunc::getSharedMemory
void * getSharedMemory()
Definition: ExternalProcessFunc.h:113
ExternalProcessFunc::execute
void execute()
Definition: ExternalProcessFunc.h:118
print_cmd
void print_cmd(std::string tmp_string)
Definition: strPrint.cc:34
ExternalProcessFunc::shm
void * shm
Definition: ExternalProcessFunc.h:128
ExternalProcessFunc::createShareMemory
void createShareMemory()
Create a Share Memory object with random id.
Definition: ExternalProcessFunc.h:78