1. 程式人生 > >作業系統實驗課(一)程序的建立與銷燬

作業系統實驗課(一)程序的建立與銷燬

根據老師給的實驗指導書做的

# include <stdio.h>
# include <stdlib.h>
# include <Windows.h>

int main()
{
	STARTUPINFO si;
	PROCESS_INFORMATION pi;
	ZeroMemory(&si, sizeof(si));
	si.cb = sizeof(si);
	ZeroMemory(&pi, sizeof(pi));
	TCHAR szCommandLine[] = TEXT("first");
	if (!CreateProcess(NULL,
					   szCommandLine,
					   NULL,
					   NULL,
					   FALSE,
					   0,
					   NULL,
					   NULL,
					   &si,
					   &pi))
	{
		fprintf(stderr, "Createprocess Failed \n\n\n");
		system("pause");
		exit(0);
	}

	int x;
	while (true)
	{
		printf("請輸入要選擇的操作:\n0:銷燬程序\n1:掛起程序\n2:啟用程序\n3:退出\n");
		scanf("%d",&x);
		switch (x)
		{
			case 0:
				if (TerminateProcess(pi.hProcess, 0))
					printf("程序銷燬成功");
				break;
			case 1:
				if (SuspendThread(pi.hThread))
					printf("掛起程序成功");
				else
					printf("掛起失敗");
				break;
			case 2:
				if (ResumeThread(pi.hThread))
					printf("啟用程序成功");
				else
					printf("啟用失敗");
				break;
			case 3:
				exit(0);
			default:
				printf("選項不正確");
		}
	}
    return 0;
}

如果把szCommandLine字串的內容改成跟程式碼檔案的名字相同,會發生有意思的事情哦,比如檔名first.cpp哈程式碼中TCHAR szCommandLine[] = TEXT("first");這句,很有意思的哦,嘿嘿嘿……