250 likes | 403 Views
บทที่ 4. Processor Management ( single processor with single-threaded process). ประเด็นเกี่ยวกับ Single processing. มี processor เดียว ( single core) ทำงานได้ทีละคำสั่ง (ระดับภาษาเครื่อง) มีหลายงานที่รันอยู่พร้อมกัน ( multitasking ) การที่ OS รันอยู่ก็ต้องใช้ CPU เหมือนกัน
E N D
บทที่ 4 Processor Management (single processor with single-threaded process)
ประเด็นเกี่ยวกับ Single processing • มี processor เดียว (single core) • ทำงานได้ทีละคำสั่ง (ระดับภาษาเครื่อง) • มีหลายงานที่รันอยู่พร้อมกัน (multitasking) • การที่ OS รันอยู่ก็ต้องใช้ CPU เหมือนกัน • ให้ทุกกลุ่มปรึกษากัน และนำเสนอแนวคิดว่า ถ้ามีสิ่งของอยู่ชิ้นหนึ่ง ไม่สามารถแบ่งแยกได้ ไม่สามารถใช้ร่วมกันได้ แต่มีผู้ต้องการใช้หลายคน จะมีวิธีแบ่งอย่างไร จึงจะได้ใช้ทุกคน • ทุกกลุ่มเสนอแนวคิด • ปรับปรุงเป็นการจัดสรร CPU • เปรียบเทียบข้อแตกต่างกับการจัดสรร memory
Process state(การเรียกชื่อ state ต่างๆอาจต่างกันไปตามผู้แต่งหนังสือ น.ศ.ไม่จำต้องเรียกตามอาจารย์ แต่ต้องอธิบายความหมายได้ถูกต้อง) • เมื่อมีการเริ่มต้น process ใหม่ process ยังไม่พร้อมจะทำงานทันที ต้องผ่านกระบวนการบางอย่างเสียก่อน (เช่นเตรียมทรัพยากรและข้อมูล) • เมื่อ process พร้อมจะทำงาน ก็อาจไม่สามารถทำงานได้ทันที เพราะ CPU ไม่ว่าง (แล้วทำยังไงดี กลับไปเริ่มต้นใหม่ดีไหม ?กลุ่ม ... ตอบ) • เมื่อ process ได้เริ่มทำงาน (ได้ใช้ CPU แล้ว) ควรให้ทำงานไปจนจบหรือไม่ (กลุ่ม ... ตอบ) • ถ้า process ทำคำสั่ง I/O เช่นรับข้อมูลจาก keyboard ต้องรอจนกว่าจะได้ข้อมูล จึงจะทำคำสั่งต่อไปได้ process ดังกล่าวควรจะครอบครอง CPU ต่อหรือไม่ ถ้าไม่ควรทำอย่างไร (กลุ่ม ... ตอบ)
Hold Ready Running Waiting Job and Process Status Admitted Finished Interrupt Exit Scheduler dispatch I/O or event completion I/O or event wait Handled by Process Scheduler Handled by Job Scheduler
เมื่อ CPU ว่าง process ใดที่จะได้รัน • จากแผนภาพมีเพียงเส้นจาก ready เท่านั้นที่มายัง running • Process ที่อยู่ในสถานะ ready เท่านั้นจึงจะมีสิทธิได้รัน • Process ในสถานะ ready มีอยู่ process เดียวหรือเปล่า ? • จากแผนภาพเส้นที่มา ready มี 3 เส้น มีโอกาสที่จะมาจากทั้ง 3 ทาง • ในแต่ละเส้นทาง มีโอกาสที่จะมีหลาย process เข้ามาขณะ CPU ไม่ว่าง • เมื่อมีหลาย process อยู่ในสถานะ ready จะเลือก process ใดมารัน • ให้ทุกกลุ่มปรึกษาและเสนอความเห็น
เมื่อมีการเปลี่ยน process ทำงาน • ถ้า process ที่กำลังครอบครอง CPU ทำงานเสร็จสิ้น CPU ก็จะว่างให้ process อื่นมาทำงานได้โดยไม่มีปัญหาใดๆ • แต่ถ้า process ที่ครอบครอง CPU ยังทำงานไม่จบ จำเป็นต้องกลับมาใช้ CPU ใหม่ หากเอา process ดังกล่าวออกไปอยู่สถานะอื่นเฉยๆย่อมมีปัญหา เพราะเมื่อกลับมาทำงานต่อ จะต้องกลับมาทำงานต่อที่คำสั่งต่อเนื่องจากเดิม ข้อมูลที่อยู่ระหว่างประมวลผลโดย CPU ก็จะต้องเหมือนเดิม • ดังนั้นจำเป็นต้องเก็บข้อมูลไว้เพื่อที่จะกลับมาทำงานต่อได้ถูกต้อง • ข้อมูลสำคัญที่ต้องเก็บไว้ได้แก่ program counter, register content และอื่นๆ ซึ่งจะเก็บไว้ใน PCB ซึ่งจะถูกนำกลับมาไว้ในที่เดิมเมื่อ process กลับมาทำงานอีกครั้ง • กระบวนการนี้เรียกว่า Context Switching
Process Control Block (PCB) • Process Control Block (PCB) -- data structure that contains basic info about the job • Process identification • Process status (HOLD, READY, RUNNING, WAITING) • Process state (process status word, register contents, main memory info, resources, process priority) • Accounting (CPU time, total amount of time, I/O operations, number input records read, etc.)
PCBs and Queuing • PCB of job created when Job Scheduler accepts it • updated as job goes from beginning to termination. • Queues use PCBs to track jobs. • PCBs, not jobs, are linked to form queues. • E.g., PCBs for every ready job are linked on READY queue; all PCBs for jobs just entering system are linked on HOLD queue. • Queues must be managed by process scheduling policies and algorithms.
Process Scheduling Algorithms • First Come First Served (FCFS) • Shortest Job Next (SJN) • Priority Scheduling • Shortest Remaining Time (SRT) • Round Robin • Multiple Level Queues
First Come First Served (FCFS) • Non-preemptive. • Handles jobs according to their arrival time -- the earlier they arrive, the sooner they’re served. • Simple algorithm to implement -- uses a FIFO queue. • Good for batch systems; not so good for interactive ones. • Turnaround time is unpredictable.
FCFS Example Process CPU Burst (Turnaround Time) A 15 milliseconds B 2 milliseconds C 1 millisecond • If they arrive in order of A, B, and C. What does the time line look like? What’s the average turnaround time?
Shortest Job Next (SJN) • Non-preemptive. • Handles jobs based on length of their CPU cycle time. • Use lengths to schedule process with shortest time. • Optimal – gives minimum average waiting time for a given set of processes. • optimal only when all of jobs are available at same time and the CPU estimates are available and accurate. • Doesn’t work in interactive systems because users don’t estimate in advance CPU time required to run their jobs.
Priority Scheduling • Non-preemptive. • Gives preferential treatment to important jobs. • Programs with highest priority are processed first. • Aren’t interrupted until CPU cycles are completed or a natural wait occurs. • If 2+ jobs with equal priority are in READY queue, processor is allocated to one that arrived first (first come first served within priority). • Many different methods of assigning priorities by system administrator or by Processor Manager.
Shortest Remaining Time (SRT) • Preemptive version of the SJN algorithm. • Processor allocated to job closest to completion. • This job can be preempted if a newer job in READY queue has a “time to completion” that's shorter. • Can’t be implemented in interactive system -- requires advance knowledge of CPU time required to finish each job. • SRT involves more overhead than SJN. • OS monitors CPU time for all jobs in READY queue and performs “context switching”.
Round Robin • Preemptive. • Used extensively in interactive systems because it’s easy to implement. • Isn’t based on job characteristics but on a predetermined slice of time that’s given to each job. • Ensures CPU is equally shared among all active processes and isn’t monopolized by any one job. • Time slice is called a time quantum • size crucial to system performance (100 ms to 1-2 secs)
If Job’s CPU Cycle < Time Quantum • If job’s last CPU cycle & job is finished, then all resources allocated to it are released & completed job is returned to user. • If CPU cycle was interrupted by I/O request, then info about the job is saved in its PCB & it is linked at end of the appropriate I/O queue. • Later, when I/O request has been satisfied, it is returned to end of READY queue to await allocation of CPU.
Time Slices Should Be ... • Long enough to allow 80 % of CPU cycles to run to completion. • At least 100 times longer than time required to perform one context switch. • Flexible -- depends on the system.
Multiple Level Queues • Not a separate scheduling algorithm. • Works in conjunction with several other schemes where jobs can be grouped according to a common characteristic. • Examples: • Priority-based system with different queues for each priority level. • Put all CPU-bound jobs in 1 queue and all I/O-bound jobs in another. Alternately select jobs from each queue to keep system balanced. • Put batch jobs “background queue” & interactive jobs in a “foreground queue”; treat foreground queue more favorably than background queue.
Policies To Service Multi-level Queues • No movement between queues. • Move jobs from queue to queue. • Move jobs from queue to queue and increasing time quantums for “lower” queues. • Give special treatment to jobs that have been in system for a long time (aging).
ผลของแต่ละ algorithm • จะเห็นว่าแต่ละ algorithm ก็มีข้อดีข้อเสียแตกต่างกันไป ผลกระทบต่อ process แต่ละแบบก็แตกต่างกันด้วย • ดังนั้นการเลือกใช้ algorithm ใด จะเป็น preemptive หรือ non-preemptive การตั้งค่าเวลาของ time quantum ย่อมขึ้นอยู่กับนโยบายในการออกแบบ OS ว่าต้องการออกแบบ OS ให้เป็นแบบใด เหมาะกับงานแบบใดโดยเฉพาะหรือไม่ • CPU-bound jobs • I/O-bound jobs • Interactive jobs • Background jobs • Real time jobs • Transactions Processing jobs
Maximize throughput by running as many jobs as possible in a given amount of time. Maximize CPU efficiency by keeping CPU busy 100 % of time. Ensure fairness for all jobs by giving everyone an equal amount of CPU and I/O time. Minimize response time by quickly turning around interactive requests. Minimize turnaround time by moving entire jobs in/out of system quickly. Minimize waiting time by moving jobs out of READY queue as quickly as possible. Process Scheduling Policy
Interrupts • There are instances when a job claims CPU for a very long time before issuing an I/O request. • builds up READY queue & empties I/O queues. • Creates an unacceptable imbalance in the system. • Process Scheduler uses a timing mechanism to periodically interrupts running processes when a predetermined slice of time has expired. • suspends all activity on the currently running job and reschedules it into the READY queue.
Cache Memory • Cachememory -- quickly accessible memory that’s designed to alleviate speed differences between a very fast CPU and slower main memory. • Stores copy of frequently used data in an easily accessible memory area instead of main memory.
Types of Interrupts • Page interrupts to accommodate job requests. • Time quantum expiration. • I/O interrupts when issue READ or WRITE command. • Internal interrupts (synchronous interrupts) result from arithmetic operation or job instruction currently being processed. • Illegal arithmetic operations (e.g., divide by 0). • Illegal job instructions (e.g., attempts to access protected storage locations).
Interrupt Handler • Describe & store type of interrupt (passed to user as error message). • Save state of interrupted process (value of program counter, mode specification, and contents of all registers). • Process the interrupt • Send error message & state of interrupted process to user. • Halt program execution • Release any resources allocated to job are released • Job exits the system. • Processor resumes normal operation.