600 likes | 720 Views
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)
E N D
CS134: Week 3 • Process control and scheduling • Process Control Block • Resource Control Block • Scheduling Computing Systems
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
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
Process Creation Hierarchy Computing Systems
Process state diagram Computing Systems
Process Control Block Computing Systems
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
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
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
Other • Process hierarchy • Parent pointer (the root process will be null) • Linked list of children • Other • Priority • Policy info • ... Computing Systems
Linux PCB (struct task_struct) Computing Systems
Linux PCB Computing Systems
Linux ID Computing Systems
Linux State Vector Computing Systems
Linux Other Computing Systems
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
Resource descriptor Computing Systems
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
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
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
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
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
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
Suspend(p) • Set the process status to Suspended • If the process was Running • Stop it • Resume by invoking the scheduler process Computing Systems
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
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
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
Activate(p) • To activate a process, remove Suspended state • If process is Ready, resume by invoking scheduler • Otherwise, leave process blocked Computing Systems
Activate(p) • if(p->status == ReadySuspended) { p->status = Ready; Scheduler();}else p->status = Blocked; Computing Systems
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
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
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
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
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
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
Release(r, ...) • Release the unit into r->avail • Poll the resource for satisfiable requests • Insert each released process into the ready queue Computing Systems
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
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
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
Process state diagram Computing Systems
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
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
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
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
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
Scheduler code Computing Systems
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
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
Allocate CPU Computing Systems
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