Windows에서 프로세스를 일시 중단 / 재개하는 방법은 무엇입니까?
Unix에서는 프로세스 실행을 일시적으로 중단하고 신호 SIGSTOP
및 SIGCONT
. 프로그래밍없이 Windows에서 단일 스레드 프로세스를 어떻게 일시 중단 할 수 있습니까?
명령 줄에서는 할 수 없으며 코드를 작성해야합니다 (유틸리티를 찾는 것만이 아니라고 가정합니다. 그렇지 않으면 수퍼 유저가 더 나은 곳일 수 있습니다). 또한 애플리케이션에이를 수행하는 데 필요한 모든 권한이 있다고 가정합니다 (예제에는 오류 검사가 없음).
어려운 방법
먼저 주어진 프로세스의 모든 스레드를 가져온 다음 SuspendThread
함수를 호출하여 각 스레드 를 중지 ResumeThread
하고 다시 시작합니다. 작동하지만 일부 응용 프로그램은 스레드가 어느 지점에서든 중지 될 수 있고 일시 중단 / 재개 순서를 예측할 수 없기 때문에 충돌하거나 중단 될 수 있습니다 (예 : 데드락이 발생할 수 있음). 단일 스레드 응용 프로그램의 경우 이는 문제가되지 않을 수 있습니다.
void suspend(DWORD processId)
{
HANDLE hThreadSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
THREADENTRY32 threadEntry;
threadEntry.dwSize = sizeof(THREADENTRY32);
Thread32First(hThreadSnapshot, &threadEntry);
do
{
if (threadEntry.th32OwnerProcessID == processId)
{
HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE,
threadEntry.th32ThreadID);
SuspendThread(hThread);
CloseHandle(hThread);
}
} while (Thread32Next(hThreadSnapshot, &threadEntry));
CloseHandle(hThreadSnapshot);
}
이 기능은 너무 순진하기 때문에 스레드를 재개하려면 일시 중지 된 스레드를 건너 뛰어야하며 일시 중지 / 재개 순서로 인해 교착 상태가 발생하기 쉽습니다. 단일 스레드 응용 프로그램의 경우 prolix이지만 작동합니다.
문서화되지 않은 방법
Windows XP부터는 NtSuspendProcess
있지만 문서화되지 않았습니다 . 코드 예제를 보려면 이 게시물 을 읽으십시오 (문서화되지 않은 함수에 대한 참조 : news : //comp.os.ms-windows.programmer.win32).
typedef LONG (NTAPI *NtSuspendProcess)(IN HANDLE ProcessHandle);
void suspend(DWORD processId)
{
HANDLE processHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processId));
NtSuspendProcess pfnNtSuspendProcess = (NtSuspendProcess)GetProcAddress(
GetModuleHandle("ntdll"), "NtSuspendProcess");
pfnNtSuspendProcess(processHandle);
CloseHandle(processHandle);
}
"디버거"방식
프로그램을 일시 중지하는 것은 일반적으로 디버거가하는 일이며,이를 수행하려면 DebugActiveProcess
함수를 사용할 수 있습니다 . 프로세스 실행을 일시 중단합니다 (모든 스레드가 모두 함께). 재개하려면 DebugActiveProcessStop
.
이 함수를 사용하면 프로세스를 중지 할 수 있습니다 (프로세스 ID가 제공됨). 구문은 매우 간단합니다. 중지하려는 프로세스의 ID 만 전달하면됩니다. 명령 줄 응용 프로그램을 만들려면 인스턴스를 계속 실행하여 프로세스를 일시 중단 (또는 종료)해야합니다. 자세한 내용은 MSDN 의 설명 섹션을 참조 하십시오.
void suspend(DWORD processId)
{
DebugActiveProcess(processId);
}
명령 줄에서
내가 말했듯이 Windows 명령 줄에는이를 수행하는 유틸리티가 없지만 PowerShell에서 Windows API 함수를 호출 할 수 있습니다. 먼저 Invoke-WindowsApi 스크립트를 설치 한 다음 다음과 같이 작성할 수 있습니다.
Invoke-WindowsApi "kernel32" ([bool]) "DebugActiveProcess" @([int]) @(process_id_here)
물론 자주 필요한 경우 만들 수 있습니다 alias
.
Without any external tool you can simply accomplish this on Windows 7 or 8, by opening up the Resource monitor and on the CPU or Overview tab right clicking on the process and selecting Suspend Process. The Resource monitor can be started from the Performance tab of the Task manager.
I use (a very old) process explorer from SysInternals (procexp.exe). It is a replacement / addition to the standard Task manager, you can suspend a process from there.
Edit: Microsoft has bought over SysInternals, url: procExp.exe
Other than that you can set the process priority to low so that it does not get in the way of other processes, but this will not suspend the process.
PsSuspend command line utility from SysInternals
suite. It suspends / resumes a process by its id.
Well, Process Explorer has a suspend option. You can right click a process in the process column and select suspend. Once you are ready to resume it again right click and this time select resume. Process Explorer can be obtained from here:
http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx
PsSuspend, as mentioned by Vadzim, even suspends/resumes a process by its name, not only by pid.
I use both PsSuspend and PsList (another tool from the PsTools suite) in a simple toggle script for the OneDrive process: if I need more bandwidth, I suspend the OneDrive sync, afterwards I resume the process by issuing the same mini script:
PsList -d onedrive|find/i "suspend" && PsSuspend -r onedrive || PsSuspend onedrive
PsSuspend command line utility from SysInternals
suite. It suspends / resumes a process by its id.
#pragma comment(lib,"ntdll.lib")
EXTERN_C NTSTATUS NTAPI NtSuspendProcess(IN HANDLE ProcessHandle);
void SuspendSelf(){
NtSuspendProcess(GetCurrentProcess());
}
ntdll contains the exported function NtSuspendProcess, pass the handle to a process to do the trick.
참고URL : https://stackoverflow.com/questions/11010165/how-to-suspend-resume-a-process-in-windows
'development' 카테고리의 다른 글
SQL 스키마 비교 오류 "대상을 사용할 수 없습니다" (0) | 2020.11.09 |
---|---|
Java 프로세스에서 스레드 수를 얻는 방법 (0) | 2020.11.09 |
ng-click의 if 문 (0) | 2020.11.09 |
싱글 톤 : 좋은 디자인인가 아니면 버팀목인가? (0) | 2020.11.09 |
ExpandableListView의 groupIndicator를 완전히 숨기려면 어떻게해야합니까? (0) | 2020.11.09 |