Docstoc

handouts

Document Sample
handouts Powered By Docstoc
					CS134a




                                       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                                        CS134a, 2000
                                         Processes II.31




                               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                                        CS134a, 2000
                                         Processes II.32




                             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                                        CS134a, 2000
                                         Processes II.33




                                                                                 11
CS134a




                                       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                                                        CS134a, 2000
                                             Processes II.34




                  Request(r, req_details, alloc_details)
            •   insert (p, req_details, alloc_details) into r.waiters
                Allocate(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();                                                   CS134a, 2000
         Computing Systems                   Processes II.35




                                       Release(r, ...)


            • Release the unit into r->avail
            • Poll the resource for satisfiable requests
            • Insert each released process into the ready queue




         Computing Systems                                                        CS134a, 2000
                                             Processes II.36




                                                                                                 12
CS134a




                                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                                         CS134a, 2000
                                        Processes II.37




                             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                                         CS134a, 2000
                                        Processes II.38




                                     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                                         CS134a, 2000
                                        Processes II.39




                                                                                  13
CS134a




                                     Process state diagram

                                                      allocate
                     BlockedSuspended                               ReadySuspended

              suspend                      activate             suspend    activate

                                                  allocate
                                 Blocked                              Ready
                                                        processor
                                                        allocated time-slice
                request resource                                  expired       suspend
                                                   Running




         Computing Systems                                                            CS134a, 2000
                                                  Processes II.40




                   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                                                            CS134a, 2000
                                                  Processes II.41




                                   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                                                            CS134a, 2000
                                                  Processes II.42




                                                                                                     14
CS134a




                                      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                                             CS134a, 2000
                                         Processes II.43




                                     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                                             CS134a, 2000
                                         Processes II.44




                                      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                                             CS134a, 2000
                                         Processes II.45




                                                                                      15

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:2
posted:8/14/2012
language:
pages:5