Windows core programming Note (3)--Jobs

Source: Internet
Author: User

/*1, if a process has been associated with a job, it is not possible to remove the current process and any of its child processes from the job, which guarantees that the/* process cannot get rid of the restrictions imposed on it. /*2, when you debug a program, the debugger starts from the explorer and the program inherits the job with the "PCA" prefix from the debugger. Therefore, the debugger/* Always shows that the process has joined the job. There is no problem with running the program using the command line. /*3, closes a Job object, and does not terminate all processes within the job. The Job object actually only adds a delete tag, and it is automatically destroyed only if all processes are terminated in the job. /*4, you can reference the following restrictions to the job: (Setinformationjobobject function to implement) 4.1 Basic and additional restrictions to prevent processes in the job from monopolizing system resources; 4.2 Basic UI restrictions to prevent processes within the job from changing the user interface; 4.3 A security limit that prevents processes within a job from accessing security resources (files, registry subkeys, and so on)/*5, once a process has joined a job, it can no longer be added to other jobs, and the process says that the child process created will also be within the limits of its job. /* You can add a JOB_OBJECT_LIMIT_BREAKAWAY_OK limit to the job and add create_breakaway_from_job *//knowledge when creating the subprocess so that the process is not constrained by the parent process job. /*6, the method of terminating all processes in the job, Terminatejobobject (__in HANDLE hjob, __in UINT uexitcode) function to terminate the job. */#include "stdafx.h" #include <Windows.h> #include <iostream>using std::cout;using Std::endl; Booladdthreadtojob (); int _tmain (int argc, _tchar* argv[]) {addthreadtojob (); return 0;} BOOL Addthreadtojob () {bool bresult = FALSE; HANDLE Hjob = Null;try{bool Binjob = False;//dword dwpid = GetCurrentProcessId ();//This is the real handle to the get process//handle hprocess = OpenPro Cess (Process_all_access, FALSE, dwpid);GetCurrentProcess () returns a pseudo handle of 0xffffffff, but is inherently error-free bool BRet = Isprocessinjob (GetCurrentProcess (), NULL, &binjob) ;//closehandle (hprocess); if (binjob) {jobobject_basic_process_id_list jbril;memset (&jbril, 0, sizeof (jbril));D WORD Dwlen = 0;bret = Queryinformationjobobject (NULL, Jobobjectbasicprocessidlist, (LPVOID) &jbril, sizeof ( jobobject_basic_process_id_list), &dwlen); throw L "The current process has been added to the job! ";} Hjob = Createjobobject (null, L "Testjob"); if (null = = Hjob) {Throw L ' Create job failed! ";} if (GetLastError () = = error_already_exists) {Throw L ' the same name job has been created! ";} cout<< "Add limit to Job" <<endl;//set job base limit jobobject_basic_limit_information Jbli = {0};jbli. priorityclass= idle_priority_class;//the system will automatically terminate its run Jbli for any processes that process takes longer than the allotted time. Perjobusertimelimit.quadpart= 10000;jbli. limitflags= job_object_limit_priority_class| Job_object_limit_job_time; Setinformationjobobject (Hjob, Jobobjectbasiclimitinformation, (LPVOID) &jbli, sizeof (jobobject_basic_limit_ information));//Set Job UI Limits cout<< "Set job UI Limits" <<endl; Jobobject_basic_ui_restrictions Jbur;jbur. uirestrictionsclass= job_object_uilimit_exitwindows;//cannot shut down the computer Jbur. Uirestrictionsclass|= job_object_uilimit_handles;//cannot access other object handles in the system Setinformationjobobject (Hjob, Jobobjectbasicuirestrictions, (LPVOID) &jbur, sizeof (jobobject_basic_ui_restrictions));//You must first create a process, Then suspend the process so that he cannot execute any code before joining the job (to ensure that the process does not have any confidence in the code before entering the job)//or join the job limit this process is meaningless. Startupinfo si;memset (&si, 0, sizeof ( Startupinfo)); si.cb= sizeof (STARTUPINFO); Process_information Pi;memset (&pi, 0, sizeof (process_information));cout<< "Create a new process" <<endl;bret = CreateProcess (L "Update.exe", NULL, NULL, NULL, FALSE, create_suspended| Create_new_console, NULL, NULL, &AMP;SI, &AMP;PI); if (!bret) throw L "Create process failed! ";cout<<" child process created "<<endl;//to add this process to the job Assignprocesstojobobject (Hjob, pi.hprocess);//can now resume this process, Let him start executing the code resumethread (Pi.hthread); CloseHandle (Pi.hthread);//wait for the process to terminate, or the job requests CPU time to run out of handle h[2];h[0]= pi.hprocess;h[1]= hjob;cout<< " Start waiting for the process to finish executing or the job CPU is finished using "<<endl;dword DWret = WaitForMultipleObjects (2, H, FALSE, INFINITE), switch (DWRET-WAIT_OBJECT_0) {case 0://process terminates {int a = 0;cout<< ' into End of Process "<<endl;; break;} Case 1://The CPU time of the job runs out {int a = 0;cout<< "The CPU time of the job runs out" <<endl;break;} CloseHandle (pi.hprocess);//Get Process Execution time information filetime createtime, Exittime, Kerneltime, Usedtime; Getprocesstimes (pi.hprocess, &createtime, &exittime, &kerneltime, &usedtime); WCHAR szbuffer[256] = {0};swprintf_s (szbuffer, N, L "createtime=%u,exittime=%u,kerneltime=%u,usedtime=%u", createtime.dwlowdatetime/1000, exittime.dwlowdatetime/10000, kerneltime.dwlowdatetime/10000, usedtime.dwlowdatetime/10000); MessageBox (NULL, Szbuffer, L "End:", MB_OK); bresult = TRUE;} catch (wchar* pMsg) {MessageBox (null, PMSG, NULL, 0);} if (hjob) CloseHandle (hjob); return bresult;} Finally, you need to run the instance from the command line, because the resource Manager startup defaults to joining the system job. (described above)





Windows core programming Note (3)--Jobs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.