Skip to content

Commit

Permalink
Added signal processing function but still has bugs..
Browse files Browse the repository at this point in the history
  • Loading branch information
9527567 committed Apr 10, 2024
1 parent 23e8c76 commit cb13052
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
2 changes: 2 additions & 0 deletions AmberMDrun/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ add_subdirectory(extern/pybind11)
aux_source_directory(src SOURCE)
#add_library(ambermd SHARED ${SOURCE})
add_library(ambermd STATIC ${SOURCE})
target_link_libraries(ambermd PUBLIC pybind11::module)
target_link_libraries(ambermd PUBLIC pybind11::embed)
add_executable(amberMd
main.cpp
)
Expand Down
2 changes: 2 additions & 0 deletions AmberMDrun/include/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ inline void trim(std::string &s)
s.erase(s.find_last_not_of(' ') + 1);
}
[[maybe_unused]] std::string executeCMD2(const std::vector<std::string>& args, const bool inc_stderr = false);
void signalHandler(int signum);
void raiseHandler(int signum);
#endif//AMBERMD_COMMON_HPP
5 changes: 4 additions & 1 deletion AmberMDrun/src/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// Created by jack on 2022/9/19.
//
#include "base.hpp"
#include "common.hpp"
#include "fmt/core.h"
#include "fmt/os.h"
#include <signal.h>
#include <utility>
#define f fmt::format
Base::Base(const std::string &name, SystemInfo systemInfo, const std::string &rst7, const std::string &refc, bool irest,
Expand All @@ -27,6 +29,7 @@ void Base::writeInput()

void Base::Run()
{
signal(SIGINT, raiseHandler);
if (this->done_ == true) this->done_ = false;
writeInput();
charmmWater();
Expand Down Expand Up @@ -157,7 +160,7 @@ void Base::progress()
while (!this->done_)
{
fswatcher_event_handler handler = {[&](fswatcher_event_handler *handler, fswatcher_event_type evtype, const char *src, const char *dst) -> bool {
if (src =="./" + this->name_ + ".out")
if (src == "./" + this->name_ + ".out")
{
(void) handler;
(void) dst;
Expand Down
21 changes: 17 additions & 4 deletions AmberMDrun/src/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@
#include "common.hpp"
#include "fmt/format.h"

#include "pybind11/pybind11.h"
#include <atomic>
#include <cstring>
#include <filesystem>
#include <optional>
#include <signal.h>
#include <sys/wait.h>
#include <vector>


namespace py = pybind11;
std::vector<std::string> executeCMD(const std::string &strCmd)
{

signal(SIGINT, signalHandler);
char buf[1024] = {0};
FILE *pf = nullptr;

if ((pf = popen(strCmd.c_str(), "r")) == nullptr)
{
throw std::runtime_error(fmt::format("{} run failed!\n", strCmd));
Expand Down Expand Up @@ -44,8 +47,9 @@ std::vector<std::string> executeCMD(const std::string &strCmd)

return result;
}

[[maybe_unused]] std::string executeCMD2(const std::vector<std::string> &args,
const bool inc_stderr)
const bool inc_stderr)
{
int stdout_fds[2];
pipe(stdout_fds);
Expand Down Expand Up @@ -125,4 +129,13 @@ std::vector<std::string> executeCMD(const std::string &strCmd)
} while (r == -1 && errno == EINTR);

return out;
}
void signalHandler(int signum)
{
fmt::print("Interrupt signal{} received.\n", signum);
exit(signum);
}
void raiseHandler(int signum)
{
raise(signum);
}

0 comments on commit cb13052

Please sign in to comment.