import worker code
This commit is contained in:
parent
61654a06e8
commit
8831040043
64 changed files with 20853 additions and 1 deletions
128
src/worker/include/process_manager.h
Normal file
128
src/worker/include/process_manager.h
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
#ifndef PROCESS_MANAGER_H
|
||||
#define PROCESS_MANAGER_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
typedef HANDLE process_handle_t;
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
typedef pid_t process_handle_t;
|
||||
#endif
|
||||
#include "sqlite3.h"
|
||||
|
||||
|
||||
typedef struct {
|
||||
process_handle_t handle;
|
||||
char* command; // command line
|
||||
char* work_dir; // working directory
|
||||
int status; // process status (0: stopped, 1: running)
|
||||
int exit_code; // exit code
|
||||
int output_fd; // output file descriptor
|
||||
char* log_file; // log file path
|
||||
#ifdef _WIN32
|
||||
HANDLE pipe_read; // pipe read handle
|
||||
HANDLE monitor_thread; // monitor thread handle
|
||||
bool monitor_active; // monitor thread active flag
|
||||
#endif
|
||||
} process_info_t;
|
||||
|
||||
typedef struct process_manager {
|
||||
process_info_t* processes;
|
||||
size_t process_count;
|
||||
int update_interval;
|
||||
bool running;
|
||||
bool initialized;
|
||||
#ifdef _WIN32
|
||||
HANDLE thread;
|
||||
CRITICAL_SECTION lock;
|
||||
#else
|
||||
pthread_t thread;
|
||||
pthread_mutex_t lock;
|
||||
#endif
|
||||
} process_manager_t;
|
||||
|
||||
/**
|
||||
* @brief initialize process info structure
|
||||
* @param proc process info structure
|
||||
* @return void
|
||||
*/
|
||||
void process_info_init(process_info_t* proc);
|
||||
|
||||
/**
|
||||
* @brief initialize process info structure
|
||||
* @return process_manager_t* if success, NULL if failed
|
||||
*/
|
||||
process_manager_t* process_manager_create(void);
|
||||
|
||||
/**
|
||||
* @brief start a new process
|
||||
* @param manager process manager without resource limits
|
||||
* @param command command with arguments
|
||||
* @return 0 if success, -1 if failed
|
||||
*/
|
||||
int process_manager_start_process(process_manager_t* manager, const char* command);
|
||||
|
||||
/**
|
||||
* @brief get the number of processes
|
||||
* @param manager process manager
|
||||
* @return the number of processes
|
||||
*/
|
||||
int get_process_count(process_manager_t* manager);
|
||||
|
||||
/**
|
||||
* @brief get the current process pid
|
||||
* @param manager process manager
|
||||
* @return the current process pid
|
||||
*/
|
||||
int get_current_process_pid(process_manager_t* manager);
|
||||
|
||||
/**
|
||||
* @brief update the process status in the list
|
||||
* @param manager process manager
|
||||
* @return true if success, false if failed
|
||||
*/
|
||||
bool process_manager_update(process_manager_t* manager);
|
||||
|
||||
/**
|
||||
* @brief get process info by handle
|
||||
* @param manager process manager
|
||||
* @param handle process handle
|
||||
* @return process_info_t* if success, NULL if failed
|
||||
*/
|
||||
process_info_t* process_manager_get_process_info(process_manager_t* manager, process_handle_t handle);
|
||||
|
||||
/**
|
||||
* @brief terminate a process
|
||||
* @param manager process manager
|
||||
* @param handle process handle
|
||||
* @return 0 if success, -1 if failed
|
||||
*/
|
||||
int process_manager_terminate_process(process_manager_t* manager, process_handle_t handle);
|
||||
|
||||
/**
|
||||
* @brief destroy process manager
|
||||
* @param manager process manager
|
||||
* @return void
|
||||
*/
|
||||
void process_manager_destroy(process_manager_t* manager);
|
||||
|
||||
/**
|
||||
* @brief remove a process from the list
|
||||
* @param manager process manager
|
||||
* @param handle process handle
|
||||
* @return 0 if success, -1 if failed
|
||||
*/
|
||||
int process_manager_remove_process(process_manager_t* manager, process_handle_t handle);
|
||||
|
||||
/**
|
||||
* @brief free process manager
|
||||
* @param manager process manager
|
||||
* @return void
|
||||
*/
|
||||
void process_manager_free(process_manager_t* manager);
|
||||
|
||||
#endif // PROCESS_MANAGER_H
|
||||
Loading…
Add table
Add a link
Reference in a new issue