Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ระบบ >> Windows Server

การจัดการ Priority ของ Process ใน Windows

มาพูดถึง Priority ของกระบวนการ Windows ในกรณีส่วนใหญ่ ไม่จำเป็นต้องคิดถึงการตั้งค่า Custom Process Priority แต่บางครั้ง System Administrator ที่มีคุณสมบัติสามารถช่วยให้ระบบจัดสรรเวลา Processor ระหว่างงานที่กำลังทำงานได้ดีขึ้น เมื่อจำเป็น? ตัวอย่างเช่นบน Application Server คุณสามารถให้ CPU Time มากขึ้นแก่ Application และ SQL Server เนื่องจากเป็นกระบวนการที่ Critical ที่สุดด้านทรัพยากร

โดยทั่วไป คุณสามารถดูและเปลี่ยน Priority ของ Process โดยใช้ Task Manager (someprocess.exe -> Set Priority)

Windows NT/2000/7/2008

การจัดการ Priority ของ Process ใน Windows

ใน Windows Server 2012 การตั้งค่านี้อยู่ที่แท็บ Details

การจัดการ Priority ของ Process ใน Windows

ตามที่เห็นจากภาพถ่ายเหล่านี้ มีเพียง 6 ประเภท Priority (ตามที่จะอธิบายต่อไป นี่คือ Priority Classes) ที่มีอยู่ Microsoft คิดว่ามันเพียงพอ แต่ลองจำคำพูดอันยิ่งใหญ่ของ Bill Gates ที่บอกว่า "640 KB of RAM will be enough for everybody" แต่เวลาก็พิสูจน์แล้วว่ามันไกลจากความจริงมาก : )

ตอนนี้มา看ว่าจริงๆ แล้วมันเป็นอย่างไร

ในความเป็นจริง มี 32 Priority Levels ใน Windows ตั้งแต่ 0 ถึง 31

พวกเขาถูกจัดกลุ่มดังนี้:

  1. 31 — 16 เป็น Real-time Levels
  2. 15 — 1 เป็น Dynamic Levels
  3. 0 เป็น System Level สำหรับ Zero-page Thread

เมื่อ Process ถูกสร้าง มันจะได้รับการกำหนดหนึ่งในหก Priority Classes:

  1. Real-time Class (ค่า 24),
  2. High Class (ค่า 13),
  3. Above Normal Class (ค่า 10),
  4. Normal Class (ค่า 8),
  5. Below Normal Class (ค่า 6),
  6. หรือ Idle Class (ค่า 4).

ตามที่กล่าวมา คุณสามารถดู Process Priority โดยใช้ Task Manager

หมายเหตุ Above Normal และ Below Normal Priorities ปรากฏครั้งแรกใน Windows 2000

Priority ของแต่ละ Thread (Basic Thread Priority) ประกอบด้วย Priority ของ Process ของมันและ Relative Priority ของ Thread เอง มี 7 ประเภทของ Relative Priorities ของ Threads:

  • Normal: เหมือนกับ Process
  • Above Normal: +1 จาก Priority ของ Process
  • Below Normal: -1 จาก Priority ของ Process
  • Highest: +2;
  • Lowest: -2;
  • Time Critical: ตั้งค่า Current Priority ของ Thread เป็น 31 สำหรับ Real-time Class และ 15 สำหรับ Class อื่นๆ
  • Idle: ตั้งค่า Current Priority ของ Thread เป็น 16 สำหรับ Real-time Class และ 1 สำหรับ Class อื่นๆ

ตารางต่อไปนี้แสดง Process Priorities, Relative และ Current Priorities ของ Thread

Thread Priority Process Class Process Class
Idle class Below normal class Normal class Above normal class High class Real time class
1 Idle Idle Idle Idle Idle
2 Lowest
3 Below …
4 Idle class Normal Lowest
5 Above … Below …
6 Below normal class Highest Normal Lowest
7 Above … Below …
8 Normal class Highest Normal Lowest
9 Above … Below …
10 Above normal class Highest Normal
11 Above … Lowest
12 Highest Below …
13 High class Normal
14 Above …
15 Highest
15 Time critical Time critical Time critical Time critical Time critical
16 Idle
17
18
19
20
21
22 Lowest
23 Below …
24 Real time class Normal
25 Above …
26 Highest
27
28
29
30
31 Time critical

วิธีเริ่ม Process ด้วย Priority ที่ไม่มาตรฐานหรือเปลี่ยนมัน?

วิธี 1. เริ่ม Task/Process และเปลี่ยน Priority โดยใช้ Task Manager

ข้อเสียของวิธี:

  • มีเพียง 6 ประเภท Priority เท่านั้น
  • Priority ถูกเปลี่ยนด้วย Mouse และคุณไม่สามารถทำให้มันอัตโนมัติได้

วิธี 2. คุณสามารถใช้คำสั่ง START พร้อมคีย์ที่เกี่ยวข้อง

คีย์ที่มีอยู่ซึ่งรับผิดชอบ Priority มีดังนี้ (ผมข้ามคีย์ของคำสั่ง START ที่ไม่เกี่ยวกับการตั้งค่า Priority ที่อธิบายอยู่):

C:\>start /?
Starts a separate window to run a specified program or command.
START ["title"] [/D path] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
[/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]
[/NODE ] [/AFFINITY ] [/WAIT] [/B]
[command/program] [parameters]
LOW Start application in the IDLE priority class.
NORMAL Start application in the NORMAL priority class.
HIGH Start application in the HIGH priority class.
REALTIME Start application in the REALTIME priority class.
ABOVENORMAL Start application in the ABOVENORMAL priority class.
BELOWNORMAL Start application in the BELOWNORMAL priority class.

ตามที่เห็น คำสั่ง START อนุญาตให้รัน Process ด้วย Priority 6 แบบเดียวกับที่มีใน Task Manager

ข้อเสียของวิธี:

  • มีเพียง 6 ประเภท Priority เท่านั้น

วิธี 3. การใช้ Utility wmic.exe

ตามที่แสดงด้านบน Task Manager และคำสั่ง START ค่อนข้างไม่สะดวกในการกำหนด Priorities มา看看วิธีทำได้ยืดหยุ่นกว่า เราจะใช้ wmic.exe

Command Prompt:

wmic process where name="AppName" CALL setpriority ProcessIDLevel

ตัวอย่าง:

wmic process where name="calc.exe" CALL setpriority 32768

หรือ

wmic process where name="calc.exe" CALL setpriority "above normal"

Priorities (Preset):

  • idle: 64
  • below normal: 16384
  • normal: 32
  • above normal: 32768
  • high priority: 128
  • real time: 256

หมายเหตุ ทำอย่างไรหากมีหลาย Process ชื่อเดียวกัน? Priority ของ Process สามารถเปลี่ยนได้ทั้งด้วยชื่อหรือ PID (Process ID)

นี่คือตัวอย่างสั้นของการรัน wmic.exe เพื่อรับข้อมูลที่จำเป็น

ใช้คำสั่งนี้:

wmic process list brief

หมายเหตุ ผมจะไม่ให้ตัวอย่างการรันคำสั่งนี้เพราะรายการ Process ยาวเกินไป ทำเองถ้าอยากลอง

ตอนนี้รันคำสั่งนี้:

wmic process list brief | find "cmd.exe"

นี่คือผลลัพธ์:

การจัดการ Priority ของ Process ใน Windows

ผมเริ่มต้น Copy ของ cmd.exe หลายตัวโดยเจตนา เพื่อให้เป็นตัวอย่างที่ชัดเจนขึ้น

ตอนนี้รายการ Process จำกัดเฉพาะที่มี "cmd.exe" ในชื่อของพวกเขา โปรดสังเกต PID ของ Process(es)

ตอนนี้ลองเลือก Process ที่จำเป็นโดยใช้ WMI เพื่อทำเช่นนั้น ให้ป้อน:

wmic process where description='cmd.exe' list brief

นี่คือผลลัพธ์:

การจัดการ Priority ของ Process ใน Windows

เปรียบเทียบผลลัพธ์ จำ PID ของ CMD.EXE

Command String สำหรับเริ่ม wmic.exe

wmic process where processid='XXXX' CALL setpriority ProcessIDLevel

ตอนนี้เราสามารถเปลี่ยน Priority ของ Process เฉพาะ (เช่น PID=8476):

wmic process where processid='8476' CALL setpriority 32768

หรือ

wmic process where processid='8476' CALL setpriority "above normal"

ต่อไปคืออะไร? คิด ลอง เลือก และจัดการ Priorities ของคุณเพื่อปรับปรุงประสิทธิภาพของ Services และ Processes พร้อมกับประสบการณ์ของ End User