1 / 60

CS134: Week 3

CS134: Week 3. Process control and scheduling Process Control Block Resource Control Block Scheduling. Context. The kernel is a set of primitive operations and processes A “primitive operation” is a subroutine that is part of the calling process (often a critical section)

aolani
Télécharger la présentation

CS134: Week 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS134: Week 3 • Process control and scheduling • Process Control Block • Resource Control Block • Scheduling Computing Systems

  2. Context • The kernel is a set of primitive operations and processes • A “primitive operation” is a subroutine that is part of the calling process (often a critical section) • A process requesting a service from another sends a request message, and (usually) blocks until the service is performed • The service process (usually) blocks until it receives a request Computing Systems

  3. Kernel operation classes • Primitives for process creation, destruction, and basic interprocess communication • Primitives for allocating and releasing resources (such as memory, storage, IO devices, files...) • Input and output primitives • Operations to handle interrupts Computing Systems

  4. Process Creation Hierarchy Computing Systems

  5. Process state diagram Computing Systems

  6. Process Control Block Computing Systems

  7. Identification • Identification: each process is uniquely identified by a pointer to its process control block; it also has some other description (a string or number) that identifies it. The kernel provides a function ID -> *PCB Computing Systems

  8. State Vector • State: information required by the processor to run the process. Modified by execution of the process, or other processes that share the state vector • CPU state: process capabilities and protection info (and PC and register contents when blocked). Usually defined by processor architecture • Processor: set to processor number when executing (undefined otherwise) • Memory: storage map (page or segment table with VM), shared or owned • Resources: allocated resources (resource class + unit description) Computing Systems

  9. Status Information • Status: • running: the process in running on the processor Processor • ready: the process is ready to run, waiting for a processor • blocked: the process can’t proceed until it receives a resource or a message • In addition, the status may allow a suspended state (^Z) • The status data is a pointer to the a job queue (ready or waiting) Computing Systems

  10. Other • Process hierarchy • Parent pointer (the root process will be null) • Linked list of children • Other • Priority • Policy info • ... Computing Systems

  11. Linux PCB (struct task_struct) Computing Systems

  12. Linux PCB Computing Systems

  13. Linux ID Computing Systems

  14. Linux State Vector Computing Systems

  15. Linux Other Computing Systems

  16. Resource Descriptors • Resources include hardware components, as well as software components that satisfy the definitions of resources: • An inventory: listing the number and id of available units • A waiting list of blocked process with unsatisfied requests for the resource • An allocator responsible for deciding which requests should be honored and when • Hardware resources: drives, I/O devices, ... • Software resources: message queues, buffers, IRQs, ... Computing Systems

  17. Resource descriptor Computing Systems

  18. Resource components • Inventory list avail: a list of units (hardware devices, buffers, etc) • Waiting process list waiters: linked list of waiting processes with details about the type of request and allocation (result) area • Allocator: matches available resources with requests of waiting processes Computing Systems

  19. The processor resource • Processors are a special kind of resource: • Processes are not “blocked” when they are waiting for a processor; they are “ready” • Processes wait on a “ready list” RL: which is the Waiters for the processor resource • The ready-list is a priority queue Computing Systems

  20. Queues • A queue supports two operations • insert(q, p): adds process p to queue q • remove(q): deletes and returns an element of the q • FIFO: insertion is always onto the end of the queue, removal is from the front Computing Systems

  21. Priority queues • A priority queue takes an additional priority argument • insert(q, i, p): inserts p with priority i • remove(q): removes the lowest-priority process • Can be implemented as a tree structure • insert/remove take time O(log n), where n is the total number of processes • Fixed number of priorities implemented as array Queue queue[N] • insert is O(1), remove is O(N) Computing Systems

  22. Operations on processes • Create: create a new process • Destroy: remove a process • Suspend: change process state to suspended • Activate: change process state to active • Change_priority: set a new priority for the process Computing Systems

  23. Create • Allocate PCB • Set process ID • Initialize CPU state • Initial register values • Set program counter to initial address (_start function) • Initialize resources (in Unix, the child inherits resources of parent) • Initialize process accounting • Set status to ready; add to ready-queue RL Computing Systems

  24. Suspend(p) • Set the process status to Suspended • If the process was Running • Stop it • Resume by invoking the scheduler process Computing Systems

  25. Suspend(p) • enum Status s = p->status;if(s == Running) Stop(p);if(s == Blocked) p->status = BlockedSuspended;else p->status = ReadySuspended;if(s == Running) Scheduler(); /* invoke scheduler */ Computing Systems

  26. Stop(p) • To stop a process • capture its context by issuing an interrupt • save the context in p->CPU_state • set the CPU to idle Computing Systems

  27. Stop(p) • int c = p->processor;/* Issue interrupt to get processor context */Interrupt(c);StoreState(c, &p->CPU_state);/* No process running on processor c */Process[c] = 0; Computing Systems

  28. Activate(p) • To activate a process, remove Suspended state • If process is Ready, resume by invoking scheduler • Otherwise, leave process blocked Computing Systems

  29. Activate(p) • if(p->status == ReadySuspended) { p->status = Ready; Scheduler();}else p->status = Blocked; Computing Systems

  30. Destroy(p) • If p is Running, Stop it • Delete it from its job queue (ready or blocked) • Possibly delete all children • Release all resources • Free the PCB • If process was Running, resume by invoking the scheduler Computing Systems

  31. Destroy(p) • bool sched = false;if(p->status = Running) { Stop(p); sched = true;}delete(p->status_data, p);optionally kill children;for each resource (r, r _unit) (including memory) { if(owner(p, r_unit)) insert(r->avail, r_unit);}free(p);if(sched) Scheduler(); Computing Systems

  32. Resource primitives • Create_RC: create the descriptor for a new resource class • Destroy_RC: delete the descriptor for a resource class • Request: request some units of a resource class • Release: release some units of a resource class Computing Systems

  33. Create_RC/Destroy_RC • Resources are usually created a boot time • Create_RC: • Create units for avail • Initialize queues • Destroy_RC (for instance, on PCMCIA eject) • Delete units • Unblocks waiters: return an error message Computing Systems

  34. Request(r, ...) • Insert the current process into the waiters • Poll the waiters for satisfiable requests • Insert each released process into the ready queue • If this request was not satisfied, set status to Blocked Computing Systems

  35. Request(r, req_details, alloc_details) • insert (p, req_details, alloc_details) into r.waitersAllocate(r, &l); /* returns list of satisfied requests in l */bool satisfied = false;while(l != nil) {if(l->proc != p) {Process p2 = l->proc; insert(ready, p2); /* insert into ready queue */ p->status_data = ready; p->status = Ready; }else satisfied = true; l = l->next;}if(!satisfied) { p->status = Blocked; insert(r->waiters, p); process[p->processor] = NULL; delete(readyq, p);}Scheduler(); Computing Systems

  36. Release(r, ...) • Release the unit into r->avail • Poll the resource for satisfiable requests • Insert each released process into the ready queue Computing Systems

  37. Release(r, req_details) • insert(r->avail, r_unit(req_details));Allocator(r, &l);if(l != NULL) { while(l != NULL) { Process p = l->proc; insert(ready, p); p->status_data = ready; p->status = Ready; l = l->next; }Schedule(); Computing Systems

  38. Commonly-defined resources • Semaphore s: a resource r with r.avail = s.count • Message queue M: • M.send(p, m) = release(M, (p, m)); • M.recv(p, &m) = request(M, p, m); • Memory allocation: • alloc(unsigned size, void * where) = request(main_mem, size, where); • dealloc(void *where, unsigned size) = release(main_mem, size, where); Computing Systems

  39. Scheduling • The task of allocating processors for the ready processes • A scheduler has two parts: • Process scheduling determines the order in which processes should compete for processors • The process dispatcher binds a process to a processor; removing it from the ready queue, changing its status, loading the processor state Computing Systems

  40. Process state diagram Computing Systems

  41. Autonomous vs. shared scheduling • The scheduler may be a primitive (a function call), so it is shared by all processes • The scheduler may be central; conceptually it is a separate process that either polls the system for work, or driven by wakeup signals • One common model: a system call transfers control to a system routine, which performs the requested work, moves the current process to the ready-queue, and transfers control to the Scheduler • Multiprocessing: • In asymmetric multiprocessing, one processor is devoted to system tasks, including scheduling • In symmetric multiprocessing, system work is divided among all the processors Computing Systems

  42. Scheduling specification • Sr is the set of running processes • Sra is the set of ready and running processes • Scheduler: choose Sa the set of processes that should be running • |Sa| = number of processors • For all pÎSa and qÎSra, where p ¹ q • p->priority ³q->priority • p->priority = q->priority and p precedes q in the ready-queue Computing Systems

  43. Dispatcher • Switch(p, q): switch from process p to process q • Interrupt(c): interrupt processor c • For Switch(p, q): • Interrupt p->processor to get process context • Adjust the program counter if necessary • Load q->CPU_state into the processor Computing Systems

  44. Switch(p, q) • /* save the context for process p */int c = p->processor;if(p != current) Interrupt(c);CopyState(c, p->CPU_state);if(p == current) AdjustPC(p->CPU_state);/* load the context for process q (may enter user mode) */LoadState(c, q->CPU_state); Computing Systems

  45. Scheduler • Construct a process/processor assignment recursively • Starting from the highest priority • Find the next highest priority process • Assign it to a CPU • If the CPU already had a lower priority process running, preempt that process • Be careful: don’t replace the current process until the very last step (save it in the deferred variable) • Processor affinity is not remembered Computing Systems

  46. Scheduler code Computing Systems

  47. FindHighest(int cur_prio) • Look through the ready queues with priorities cur_prio or less to find a process in the Ready state Computing Systems

  48. AllocateCPU(Process p, ...) • Look for an idle CPU to assign to process p • If an idle CPU is found • Assign process p to that processor • Otherwise • Remember the running process with lowest priority, less than p.priority, in the variable q_min Computing Systems

  49. Allocate CPU Computing Systems

  50. Preempt(q, p, deferred) • Preempt process q and replace it with process p • Be careful: if q is the current process, defer startup Computing Systems

More Related