no message

master
zcy 2021-11-17 16:02:58 +08:00
parent 6b0e58726e
commit bb433e232d
2 changed files with 38 additions and 153 deletions

View File

@ -1,7 +1,7 @@
/* /*
* @Author: caiyuzheng * @Author: caiyuzheng
* @Date: 2021-10-06 23:05:26 * @Date: 2021-10-06 23:05:26
* @LastEditTime: 2021-10-08 00:30:49 * @LastEditTime: 2021-11-17 15:58:26
* @LastEditors: Please set LastEditors * @LastEditors: Please set LastEditors
* @Description: In User Settings Edit * @Description: In User Settings Edit
* @FilePath: \generallib\general\src\threadsafe\thread_safe_list.h * @FilePath: \generallib\general\src\threadsafe\thread_safe_list.h
@ -10,6 +10,7 @@
#include <list> #include <list>
#include <mutex> #include <mutex>
#include <condition_variable>
using namespace std; using namespace std;
template <class T> template <class T>
@ -82,6 +83,25 @@ public:
return ret1; return ret1;
} }
} }
T PopFrontAndWait()
{
if (!m_data.empty())
{
m_mux.lock();
auto ret = m_data.begin();
auto ret1 = *ret;
m_data.erase(ret);
m_mux.unlock();
return ret1;
}else{
m_cv.wait(&m_mux);
auto ret = m_data.begin();
auto ret1 = *ret;
m_data.erase(ret);
m_mux.unlock();
return ret1;
}
}
T PopEnd() T PopEnd()
{ {
@ -114,5 +134,6 @@ public:
private: private:
std::mutex m_mux; std::mutex m_mux;
std::condition_variable m_cv;
std::list<T> m_data; std::list<T> m_data;
}; };

View File

@ -1,7 +1,7 @@
/* /*
* @Author: your name * @Author: your name
* @Date: 2021-03-15 23:07:25 * @Date: 2021-03-15 23:07:25
* @LastEditTime: 2021-07-25 23:47:46 * @LastEditTime: 2021-11-17 15:58:56
* @LastEditors: Please set LastEditors * @LastEditors: Please set LastEditors
* @Description: In User Settings Edit * @Description: In User Settings Edit
* @FilePath: \cpp11\cpp11_test.cpp * @FilePath: \cpp11\cpp11_test.cpp
@ -13,9 +13,9 @@
#include<iostream> #include<iostream>
#include <algorithm> #include <algorithm>
using namespace std; using namespace std;
#include "thread_usage.h" // #include "thread_usage.h"
#include "threadpool.h" #include "threadpool.h"
#include "json.h" // #include "json.h"
#include "template.h" #include "template.h"
extern "C"{ extern "C"{
#include<stdio.h> #include<stdio.h>
@ -26,154 +26,19 @@ extern "C"{
#include "loger.h" #include "loger.h"
#include <psapi.h> #include <psapi.h>
#include "function/daemon.h" #include "function/daemon.h"
#include "threadsafe/thread_safe_list.hpp"
// [Added by thinkhy 09/12/20]
// Description: Kill process(es) by PID.
// Reference: http://www.vckbase.com/document/viewdoc/?id=1882
// RETVALUE: SUCCESS TRUE
// FAILED FALSE
BOOL KillProcess(DWORD dwPid)
{
HANDLE hPrc;
if (0 == dwPid)
return FALSE;
hPrc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPid); // Opens handle to the process.
if (!TerminateProcess(hPrc, 0)) // Terminates a process.
{
CloseHandle(hPrc);
return FALSE;
}
else
WaitForSingleObject(hPrc, 2000); // At most ,waite 2000 millisecond.
CloseHandle(hPrc);
return TRUE;
}
BOOL KillProcessByName(const TCHAR *lpszProcessName)
{
unsigned int pid = -1;
BOOL retval = TRUE;
if (lpszProcessName == NULL)
return -1;
DWORD dwRet = 0;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 processInfo;
processInfo.dwSize = sizeof(PROCESSENTRY32);
int flag = Process32First(hSnapshot, &processInfo);
// Find the process with name as same as lpszProcessName
while (flag != 0)
{
printf("kill process find %s\r\n",processInfo.szExeFile);
if (strcmp(processInfo.szExeFile, lpszProcessName) == 0)
{
// Terminate the process.
pid = processInfo.th32ProcessID;
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid);
printf("kill process pid %d\r\n",pid);
if (TerminateProcess(hProcess, 0) != TRUE)
{ // Failed to terminate it.
retval = FALSE;
break;
}
}
flag = Process32Next(hSnapshot, &processInfo);
} // while (flag != 0)
CloseHandle(hSnapshot);
if (pid == -1)
return FALSE;
return retval;
}
BOOL SetProcessPrivilege(char *lpName, BOOL opt)
{
HANDLE tokenhandle;
TOKEN_PRIVILEGES NewState;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &tokenhandle))
{
LookupPrivilegeValue(NULL, lpName, &NewState.Privileges[0].Luid);
NewState.PrivilegeCount = 1;
NewState.Privileges[0].Attributes = opt != 0 ? 2 : 0;
AdjustTokenPrivileges(tokenhandle, FALSE, &NewState, sizeof(NewState), NULL, NULL);
CloseHandle(tokenhandle);
return 1;
}
else
{
return 0;
}
}
int RangeProcess()
{
DWORD Proc_pid[1024], Retn_bytes, Proc_count, Retn_bytes2;
unsigned int i;
HMODULE hMod[1024];
HANDLE hProcess;
char szModName[MAX_PATH];
if (EnumProcesses(Proc_pid, sizeof(Proc_pid), &Retn_bytes))
{
Proc_count = Retn_bytes / sizeof(DWORD);
SetProcessPrivilege("SeDebugPrivilege", 1);
for (i = 0; i < Proc_count; i++)
{
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, Proc_pid[i]);
if (hProcess != NULL)
{
EnumProcessModules(hProcess, hMod, sizeof(hMod), &Retn_bytes2);
GetModuleFileNameEx(hProcess, hMod[0], szModName, sizeof(szModName));
printf("PID=%d Path=%s\n", Proc_pid[i], szModName);
}
CloseHandle(hProcess);
}
SetProcessPrivilege("SeDebugPrivilege", 0);
}
return 0;
}
int test_fork(char *szCommandLine)
{
if(nullptr == szCommandLine)
{
return -1;
}
STARTUPINFO si = {sizeof(si)};
PROCESS_INFORMATION pi;
si.dwFlags = STARTF_USESHOWWINDOW; //指定wShowWindow成员有效
si.wShowWindow = TRUE; //此成员设为TRUE的话则显示新建进程的主窗口
BOOL bRet = CreateProcess(
NULL, //不在此指定可执行文件的文件名
szCommandLine, //命令行参数
NULL, //默认进程安全性
NULL, //默认进程安全性
FALSE, //指定当前进程内句柄不可以被子进程继承
CREATE_NEW_CONSOLE, //为新进程创建一个新的控制台窗口
NULL, //使用本进程的环境变量
NULL, //使用本进程的驱动器和目录
&si,
&pi);
if (bRet)
{
//不使用的句柄最好关掉
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
printf("new process id %d\n", pi.dwProcessId);
printf("new thread id %d\n", pi.dwThreadId);
}
return 0;
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
ThreadSafeList<int> ip;
ip.PushFront(102);
ip.PushFront(111);
std::cout<<ip.PopFrontAndWait()<<"\r\n";
std::cout<<ip.PopFrontAndWait()<<"\r\n";
std::cout<<ip.PopFrontAndWait()<<"\r\n";
// std::cout<<"test start"<<endl; // std::cout<<"test start"<<endl;
// try{ // try{
// std::cout<<"cpu count is "<<CoreCount()<<std::endl; // std::cout<<"cpu count is "<<CoreCount()<<std::endl;
@ -190,10 +55,9 @@ int main(int argc, char **argv)
// KillProcessByName(szCommandLine); // KillProcessByName(szCommandLine);
// } // }
// DaemonizeMonitor mi("Notepad.exe");
DaemonizeMonitor mi("Notepad.exe"); // mi.AddNewProcess("Notepad.exe");
mi.AddNewProcess("Notepad.exe"); // mi.StartMonitor();
mi.StartMonitor();
// std::list<int> // std::list<int>
// i; // i;