Operating system experiment and code (full), operating system experiment code
Operating System is an extremely important professional course for computer-related majors, but many people do not know how to write the code during the lab course. Here we post a part of what the blogger has used, which is feasible for testing.
Question 1: create and destroy a Windows Process
Last Updated:
Content and requirements:
① Master the call methods for creating and destroying Windows processes, program code, and create and destroy a Word process in the program;
② It can suspend and activate the master thread of the created process;
③ View the changes in the system process list in the Windows Process Manager.
Tutorial guide:
① Process Creation API
BOOL CreateProcess( LPCTSTR lpApplicationName, LPTSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, LPCTSTR lpCurrentDirectory, LPSTARTUPINFO lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation);
Routine:
1 void main( VOID ){ 2 STARTUPINFO si; 3 PROCESS_INFORMATION pi; 4 ZeroMemory( &si, sizeof(si) ); 5 si.cb = sizeof(si); 6 ZeroMemory( &pi, sizeof(pi) ); 7 // Start the child process. 8 if( !CreateProcess( NULL, // No module name (use command line). 9 "MyChildProcess", // Command line. 10 NULL, // Process handle not inheritable. 11 NULL, // Thread handle not inheritable. 12 FALSE, // Set handle inheritance to FALSE. 13 0, // No creation flags. 14 NULL, // Use parent's environment block. 15 NULL, // Use parent's starting directory. 16 &si, // Pointer to STARTUPINFO structure.17 &pi ) // Pointer to PROCESS_INFORMATION structure.18 ) {19 ErrorExit( "CreateProcess failed." );20 }21 // Wait until child process exits.22 WaitForSingleObject( pi.hProcess, INFINITE );23 // Close process and thread handles. 24 CloseHandle( pi.hProcess );25 CloseHandle( pi.hThread );26 }
②Destruction Process API
BOOL TerminateProcess( HANDLE hProcess, UINT uExitCode);
③ Main thread of the suspended ProcessAPI
DWORD SuspendThread( HANDLE hThread);
④ Activate the main thread of the processAPI
DWORD ResumeThread( HANDLE hThread);
⑤ Process viewer
1 # include <iostream> 2 # include <windows. h> 3 using namespace std; 4 void main (VOID) 5 {6 STARTUPINFO si; 7 PROCESS_INFORMATION pi; 8/** 9 * enter the path of the personal word executable file 10 * ps: "\" Escape 11 **/12 TCHAR szCommandLine [] = TEXT ("D: \ Microsoft Office \ Office14 \ WINWORD. EXE "); 13 ZeroMemory (& si, sizeof (si); 14 si. cb = sizeof (si); 15 ZeroMemory (& pi, sizeof (pi); 16 if (! CreateProcess (NULL, szCommandLine, NULL, NULL, FALSE, 0, NULL, NULL, & si, & pi) 17 {18 fprintf (stderr, "process creation failed! "); 19} 20 int x; 21 while (1) 22 {23 cout <" Enter the operation to be selected: \ n1: Create Process \ n2: destruction Process \ n3: Pending Process \ n4: Activating process \ n0: exiting \ n "; 24 cin> x; 25 switch (x) 26 {27 case :28 if (CreateProcess (NULL, szCommandLine, NULL, NULL, FALSE, 0, NULL, NULL, & si, & pi) 29 fprintf (stderr, "process created successfully! "); 30 else31 cout <" process creation failed! "<Endl; 32 break; 33 case 2: 34 if (TerminateProcess (pi. hProcess, 0) 35 cout <"destruction process succeeded" <endl; 36 else 37 cout <"destruction failed! "<Endl; 38 break; 39 case :40 if (SuspendThread (pi. hThread) 41 cout <"pending process succeeded" <endl; 42 else43 cout <"pending failed" <endl; 44 break; 45 case 4: 46 if (ResumeThread (pi. hThread) 47 cout <"successful activation process" <endl; 48 else49 cout <"failed activation" <endl; 50 break; 51 case 0: 52 exit (0 ); 53 default: 54 cout <"incorrect input" <endl; 55} 56} 57}
Updating continuously !!!
Author: xin, published in blog
Reprinted please indicate the source, welcome to mail exchange: zhuanxinxin@foxmail.com