diff --git a/.gitignore b/.gitignore index 9d13ead..203f8c0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ /build +/bin *.so -*.exe \ No newline at end of file +*.exe +/.vscode +/.cache \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 19420ce..6b4c9aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,17 @@ cmake_minimum_required(VERSION 3.16) project(ProcessPool) +set(CMAKE_CXX_STANDARD 11) set(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/bin) -add_library(${PROJECT_NAME} SHARED main.cpp) +file(GLOB SRC src/*.cpp) + +if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux") + file(GLOB OSRC src/systemapi/unix/*.cpp) +else() + file(GLOB OSRC src/systemapi/win/*.cpp) +endif() + +add_library(${PROJECT_NAME} SHARED ${SRC} ${OSRC}) target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include diff --git a/header/systempool.hpp b/header/systempool.hpp new file mode 100644 index 0000000..a68af08 --- /dev/null +++ b/header/systempool.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "processpool.hpp" +#include "worker.hpp" + +#include + +struct ProcessPool::systemapi { + /* 子工作进程 */ + std::vector works; + systemapi(int num); + void start(const Worker&) const; +}; \ No newline at end of file diff --git a/header/worker.hpp b/header/worker.hpp new file mode 100644 index 0000000..1d21897 --- /dev/null +++ b/header/worker.hpp @@ -0,0 +1,12 @@ +#pragma once +#include "exportapi.hpp" + +class Worker { + /* 子进程的pid */ + pid_t m_pid; + bool m_busy; + +public: + + void handle(pid_t pid) const; +}; diff --git a/include/exportapi.hpp b/include/exportapi.hpp new file mode 100644 index 0000000..a9aafb2 --- /dev/null +++ b/include/exportapi.hpp @@ -0,0 +1,30 @@ +#pragma once + +#if defined(_WIN32) || defined(_WIN64) + #ifdef DYZEXPORT + #define POOLAPI __declspec(dllexport) + #else + #define POOLAPI __declspec(dllimport) + #endif +#else + #define POOLAPI __attribute__((visibility("default"))) +#endif + + +#if defined(_WIN32) || defined(_WIN64) + +#else +#include "sys/types.h" +#include "unistd.h" +#endif + + + + +#if defined(_WIN32) || defined(_WIN64) + + +#else +using pid_t = pid_t; + +#endif diff --git a/include/log.hpp b/include/log.hpp new file mode 100644 index 0000000..c40aeba --- /dev/null +++ b/include/log.hpp @@ -0,0 +1,10 @@ +#pragma once + +enum class Loglevel { + OFF, DEBUG, INFO, WARN, ERROR +}; + +template +void log(Loglevel level, Args... arg) { + +} \ No newline at end of file diff --git a/include/processpool.hpp b/include/processpool.hpp new file mode 100644 index 0000000..60920a7 --- /dev/null +++ b/include/processpool.hpp @@ -0,0 +1,16 @@ +#pragma once +#include "exportapi.hpp" + +#include + +class POOLAPI ProcessPool { + /* 进程数量 */ + int m_num; + + struct systemapi; + std::shared_ptr Impl; + +public: + ProcessPool(int num); + void exec() const; +}; \ No newline at end of file diff --git a/main.cpp b/main.cpp deleted file mode 100644 index c18d9a4..0000000 --- a/main.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include - -int main() { - std::cout << "hello ProcessPoll" << '\n'; - return 0; -} \ No newline at end of file diff --git a/src/processpool.cpp b/src/processpool.cpp new file mode 100644 index 0000000..079d602 --- /dev/null +++ b/src/processpool.cpp @@ -0,0 +1,17 @@ +#include "processpool.hpp" +#include "systempool.hpp" + +ProcessPool::ProcessPool(int num):m_num(num) +{ + Impl = std::make_shared(m_num); +} + + +void ProcessPool::exec() const { + + for (const auto& e : Impl->works) + { + /* 创建子工作进程 */ + Impl->start(e); + } +} \ No newline at end of file diff --git a/src/systemapi/unix/unixpool.cpp b/src/systemapi/unix/unixpool.cpp new file mode 100644 index 0000000..eecf836 --- /dev/null +++ b/src/systemapi/unix/unixpool.cpp @@ -0,0 +1,26 @@ +#include "processpool.hpp" +#include "systempool.hpp" + +ProcessPool::systemapi::systemapi(int num) { + for (int i = 0; i < num; ++i) + { + Worker work; + works.push_back(work); + } +} + +/* 创建进程任务 */ +void ProcessPool::systemapi::start(const Worker& child) const { + pid_t pid = fork(); + if (pid > 0) { + child.handle(pid); + } + else if (pid < 0) { + /* 创建失败 */ + + } + else { + + /* 父进程 */ + } +} diff --git a/src/systemapi/unixpool.cpp b/src/systemapi/unixpool.cpp new file mode 100644 index 0000000..0519ecb --- /dev/null +++ b/src/systemapi/unixpool.cpp @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/worker.cpp b/src/worker.cpp new file mode 100644 index 0000000..1891c4e --- /dev/null +++ b/src/worker.cpp @@ -0,0 +1,12 @@ +#include "worker.hpp" +#include "iostream" + +void Worker::handle(pid_t pid) const { + for (int i = 0; i < 5; ++i) + { + std::cerr << "子进程任务:" << pid << '\n'; + sleep(1); + } + + return; +} \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 447e356..275e0ac 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.16) - +set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../bin) # Add test executable add_executable(${PROJECT_NAME}_test test.cpp) @@ -7,3 +7,7 @@ add_executable(${PROJECT_NAME}_test test.cpp) target_link_libraries(${PROJECT_NAME}_test PRIVATE ${PROJECT_NAME} ) + +target_include_directories(${PROJECT_NAME}_test PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/../include +) diff --git a/test/test.cpp b/test/test.cpp index e69de29..bc35b04 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -0,0 +1,9 @@ +#include "processpool.hpp" + +int main() { + + ProcessPool pool(3); + pool.exec(); + + return 0; +} \ No newline at end of file