1 / 84

Processes and operating systems

Processes and operating systems. Motivation for processes. The process abstraction. Context switching. Multitasking. Processes and UML. Why multiple processes?. Processes help us manage timing complexity: multiple rates multimedia automotive asynchronous input user interfaces

lloyd
Télécharger la présentation

Processes and operating systems

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. Processes and operating systems • Motivation for processes. • The process abstraction. • Context switching. • Multitasking. • Processes and UML. Overheads for Computers as Components

  2. Why multiple processes? • Processes help us manage timing complexity: • multiple rates • multimedia • automotive • asynchronous input • user interfaces • communication systems Overheads for Computers as Components

  3. Tasks: spark control crankshaft sensing fuel/air mixture oxygen sensor Kalman filter Example: engine control engine controller Overheads for Computers as Components

  4. Code turns into a mess: interruptions of one task for another spaghetti code Life without processes A_code(); … B_code(); … if (C) C_code(); … A_code(); … switch (x) { case C: C(); case D: D(); ... A time B C A C Overheads for Computers as Components

  5. ADR r14,co2a co1a … ADR r13,co1b MOV r15,r14 co1b … ADR r13,co1c MOV r15,r14 co1c ... co2a … ADR r13,co2b MOV r15,r13 co2b … ADR r13,co2c MOV r15,r13 co2c … Co-routines Co-routine 1 Co-routine 2 Overheads for Computers as Components

  6. Co-routine methodology • Like subroutine, but caller determines the return address. • Co-routines voluntarily give up control to other co-routines. • Pattern of control transfers is embedded in the code. Overheads for Computers as Components

  7. Processes • A process is a unique execution of a program. • Several copies of a program may run simultaneously or at different times. • A process has its own state: • registers; • memory. • The operating system manages processes. Overheads for Computers as Components

  8. Activation record: copy of process state. Context switch: current CPU context goes out; new CPU context goes in. Processes and CPUs process 1 PC registers process 2 CPU ... memory Overheads for Computers as Components

  9. Terms • Thread = lightweight process: a process that shares memory space with other processes. • Reentrancy: ability of a program to be executed several times with the same results. Overheads for Computers as Components

  10. Create a process with fork: parent process keeps executing old program; child process executes new program. Processes in POSIX process a process a process b Overheads for Computers as Components

  11. fork() • The fork process creates child: childid = fork(); if (childid == 0) { /* child operations */ } else { /* parent operations */ } Overheads for Computers as Components

  12. execv() • Overlays child code: childid = fork(); if (childid == 0) { execv(“mychild”,childargs); perror(“execv”); exit(1); } file with child code Overheads for Computers as Components

  13. Context switching • Who controls when the context is switched? • How is the context switched? Overheads for Computers as Components

  14. Co-operative multitasking • Improvement on co-routines: • hides context switching mechanism; • still relies on processes to give up CPU. • Each process allows a context switch at cswitch() call. • Separate scheduler chooses which process runs next. Overheads for Computers as Components

  15. Problems with co-operative multitasking • Programming errors can keep other processes out: • process never gives up CPU; • process waits too long to switch, missing input. Overheads for Computers as Components

  16. Context switching • Must copy all registers to activation record, keeping proper return value for PC. • Must copy new activation record into CPU state. • How does the program that copies the context keep its own context? Overheads for Computers as Components

  17. Save old process: STMIA r13,{r0-r14}^ MRS r0,SPSR STMDB r13,{r0,r15} Start new process: ADR r0,NEXTPROC LDR r13,[r0] LDMDB r13,{r0,r14} MSR SPSR,r0 LDMIA r13,{r0-r14}^ MOVS pc,r14 Context switching in ARM Overheads for Computers as Components

  18. Preemptive multitasking • Most powerful form of multitasking: • OS controls when contexts switches; • OS determines what process runs next. • Use timer to call OS, switch contexts: interrupt CPU timer Overheads for Computers as Components

  19. Flow of control with preemption interrupt interrupt P1 OS P1 OS P2 time Overheads for Computers as Components

  20. Preemptive context switching • Timer interrupt gives control to OS, which saves interrupted process’s state in an activation record. • OS chooses next process to run. • OS installs desired activation record as current CPU state. Overheads for Computers as Components

  21. Why not use interrupts? • We could change the interrupt vector at every period, but: • we would need management code anyway; • we would have to know the next period’s process at the start of the current process. Overheads for Computers as Components

  22. Processes and UML • A process is an active class---independent thread of control. processClass1 myAttributes myOperations() start resume Signals Overheads for Computers as Components

  23. UML signals • Signal: object that is passed between processes for active communication: acomm: datasignal Overheads for Computers as Components

  24. Designing with active objects • Can mix normal and active objects: p1: processClass1 a: rawMsg w: wrapperClass ahat: fullMsg master: masterClass Overheads for Computers as Components

  25. Processes and operating systems • Operating systems. Overheads for Computers as Components

  26. Operating systems • The operating system controls resources: • who gets the CPU; • when I/O takes place; • how much memory is allocated. • The most important resource is the CPU itself. • CPU access controlled by the scheduler. Overheads for Computers as Components

  27. A process can be in one of three states: executing on the CPU; ready to run; waiting for data. Process state executing gets data and CPU gets CPU preempted needs data gets data ready waiting needs data Overheads for Computers as Components

  28. Operating system structure • OS needs to keep track of: • process priorities; • scheduling state; • process activation record. • Processes may be created: • statically before system starts; • dynamically during execution. Overheads for Computers as Components

  29. Embedded vs. general-purpose scheduling • Workstations try to avoid starving processes of CPU access. • Fairness = access to CPU. • Embedded systems must meet deadlines. • Low-priority processes may not run for a long time. Overheads for Computers as Components

  30. Priority-driven scheduling • Each process has a priority. • CPU goes to highest-priority process that is ready. • Priorities determine scheduling policy: • fixed priority; • time-varying priorities. Overheads for Computers as Components

  31. Priority-driven scheduling example • Rules: • each process has a fixed priority (1 highest); • highest-priority ready process gets CPU; • process continues until done or wait state. • Processes • P1: priority 1, execution time 10 • P2: priority 2, execution time 30 • P3: priority 3, execution time 20 Overheads for Computers as Components

  32. P3 ready t=18 P2 ready t=0 P1 ready t=15 Priority-driven scheduling example P2 P1 P2 P3 30 60 0 10 20 40 50 time Overheads for Computers as Components

  33. The scheduling problem • Can we meet all deadlines? • Must be able to meet deadlines in all cases. • How much CPU horsepower do we need to meet our deadlines? Overheads for Computers as Components

  34. Process initiation disciplines • Periodic process: executes on (almost) every period. • Aperiodic process: executes on demand. • Analyzing aperiodic process sets is harder---must consider worst-case combinations of process activations. Overheads for Computers as Components

  35. Timing requirements on processes • Period: interval between process activations. • Initiation interval: reciprocal of period. • Initiation time: time at which process becomes ready. • Deadline: time at which process must finish. Overheads for Computers as Components

  36. Timing violations • What happens if a process doesn’t finish by its deadline? • Hard deadline: system fails if missed. • Soft deadline: user may notice, but system doesn’t necessarily fail. Overheads for Computers as Components

  37. Example: Space Shuttle software error • Space Shuttle’s first launch was delayed by a software timing error: • Primary control system PASS and backup system BFS. • BFS failed to synchronize with PASS. • Change to one routine added delay that threw off start time calculation. • 1 in 67 chance of timing problem. Overheads for Computers as Components

  38. Interprocess communication • Interprocess communication (IPC): OS provides mechanisms so that processes can pass data. • Two types of semantics: • blocking: sending process waits for response; • non-blocking: sending process continues. Overheads for Computers as Components

  39. IPC styles • Shared memory: • processes have some memory in common; • must cooperate to avoid destroying/missing messages. • Message passing: • processes send messages along a communication channel---no common address space. Overheads for Computers as Components

  40. Shared memory • Shared memory on a bus: memory CPU 1 CPU 2 Overheads for Computers as Components

  41. Race condition in shared memory • Problem when two CPUs try to write the same location: • CPU 1 reads flag and sees 0. • CPU 2 reads flag and sees 0. • CPU 1 sets flag to one and writes location. • CPU 2 sets flag to one and overwrites location. Overheads for Computers as Components

  42. Atomic test-and-set • Problem can be solved with an atomic test-and-set: • single bus operation reads memory location, tests it, writes it. • ARM test-and-set provided by SWP: ADR r0,SEMAPHORE LDR r1,#1 GETFLAG SWP r1,r1,[r0] BNZ GETFLAG Overheads for Computers as Components

  43. Critical regions • Critical region: section of code that cannot be interrupted by another process. • Examples: • writing shared memory; • accessing I/O device. Overheads for Computers as Components

  44. Semaphores • Semaphore: OS primitive for controlling access to critical regions. • Protocol: • Get access to semaphore with P(). • Perform critical region operations. • Release semaphore with V(). Overheads for Computers as Components

  45. message Message passing • Message passing on a network: CPU 1 CPU 2 message message Overheads for Computers as Components

  46. One process may not be able to start until another finishes. Data dependencies defined in a task graph. All processes in one task run at the same rate. Process data dependencies P1 P2 P3 P4 Overheads for Computers as Components

  47. Other operating system functions • Date/time. • File system. • Networking. • Security. Overheads for Computers as Components

  48. Processes and operating systems • Scheduling policies: • RMS; • EDF. • Scheduling modeling assumptions. • Interprocess communication. • Power management. Overheads for Computers as Components

  49. Metrics • How do we evaluate a scheduling policy: • Ability to satisfy all deadlines. • CPU utilization---percentage of time devoted to useful work. • Scheduling overhead---time required to make scheduling decision. Overheads for Computers as Components

  50. Rate monotonic scheduling • RMS (Liu and Layland): widely-used, analyzable scheduling policy. • Analysis is known as Rate Monotonic Analysis (RMA). Overheads for Computers as Components

More Related