1 / 64

Processes and Threads

Processes and Threads. 2.1 Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling. Abläufe / Vorgänge im Rechner. x:=x+1. open f1. f1 := „abc“. Chapter 2. t. Technischer Prozess.

Download Presentation

Processes and Threads

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 Threads 2.1 Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling Abläufe / Vorgänge im Rechner x:=x+1 open f1 f1 := „abc“ Chapter 2 t

  2. Technischer Prozess Technischer Prozess:Nach DIN 66201 ist ein Prozess ein Vorgang zur Umformung und / oder zum Transport von Material, Energie und / oder Informationen. Man unterscheidet nach Prozessklassen − Rechenprozesse − Verfahrensprozesse, − Fertigungsprozesse, − Verteilungsprozesse, − Mess- und Prüfprozesse, ….

  3. Prozess im Sinne des BS Verwaltungseinheit des Ablaufgeschehens • Einem Nutzer zugeordnet • Einem Zweck zugeordnet • Ist gegen andere Prozesse zu schützen • Belegt eine Menge von Betriebsmitteln • Hat einen Zustand: • Ablaufzustand (wartend, rechnend, ..) • detaillierter Zustand (Prozessorregister, Speicherabbild, (Dateiabbilder)) • Fortschritt durch Prozessor-Zuteilung

  4. Prozess-Verwaltung im BS Prozessliste Prozess-kontroll-block P1 Prozess-kontroll-block P1 Prozess-kontroll-block P1 Dispatcher Short Time Scheduler Prozessor

  5. ProcessesThe Process Model • 1 Progamm, 4 Aufgaben, Programm verzweigt zwischen den Aufgaben: 1 Prozess • 4 Programme, 4 Aufgaben: 4 Prozesse • Wenn man nur 1 Prozessor hat:jeweils nur 1 Prozess kann rechnen / aktiv seinProzessumschalter (Dispatcher)

  6. Process Creation Principal events that cause process creation • System initialization • Execution of a process creation system • User request to create a new process • Initiation of a batch job

  7. Process Termination Conditions which terminate processes • Normal exit (voluntary) • Error exit (voluntary) • Fatal error (involuntary) • Killed by another process (involuntary)

  8. Process Hierarchies • Parent creates a child process, child processes can create its own process • Forms a hierarchy • UNIX calls this a "process group“ • Windows has no concept of process hierarchy • all processes are created equal Prozess Kindprozess Kindprozess Kindprozess

  9. Process States (1)Prozess-Ablaufzustände DAS PROZESS-ABLAUF-DREIECK • Possible process states • running • blocked • ready • Transitions between states shown

  10. Process States (2) • Lowest layer of process-structured OS • handles interrupts, scheduling • Above that layer are sequential processes Dispatcher / Short Term Scheduler Prozessor(en)

  11. Implementation of Processes (1) Fields of a process table entry

  12. Implementation of Processes (2) Skeleton of what lowest level of OS does when an interrupt occurs

  13. Prozesse und Threads • „Früher“1 Prozess hat genau einen Thread • „Heute“1. Mehrprozessor-Maschinen2. Nebenläufigkeit in derselben Anwendung3. E/A-Wartezeiten und Simultanaufträge • Mehrere Threads pro Prozess möglich • Prozess: • Betriebsmittel, Nutzerzuordnung, Zweck, Schutz • Thread: • Programmfortschritt, Prozessorzuordnung

  14. ThreadsThe Thread Model (1) (a) Three processes each with one thread (b) One process with three threads

  15. The Thread Model (2) • Items shared by all threads in a process • Items private to each thread Prozesszustand Threadzustand

  16. The Thread Model (3) Each thread has its own stack

  17. Thread Usage (1) A word processor with three threads

  18. Thread Usage (2) A multithreaded Web server

  19. Thread Usage (3) • Rough outline of code for previous slide (a) Dispatcher thread (b) Worker thread

  20. Ein/Ausgabe-Aufrufe Gerät Prozess • Problem: • Prozessor ist schnell, E/A-Gerät ist langsam • Prozess muss synchronisiert werden • Blockierende Aufrufe • starte Auftrag • warte • werde fortgesetzt bei Auftragsende • Nicht-blockierende Aufrufe • starte Auftrag • arbeite etwas anderes weiter, während Auftrag läuft • schaue nach, ob Auftrag schon zu Ende t

  21. Thread Usage (4) Three ways to construct a server • Wartezeiten ausnutzen:Mehrere Aufträge gleichzeitig bearbeiten • Programmierkomfort:Nicht an Menge vieler gleichzeitig anstehender Aufträge denken müssen

  22. Multithreaded Servers: Zustandsautomat Implementierung Entwurf variables Zustand loop Erkenne nächstes Ereignis Ereignis Verzweige nach Zustand x Ereignistyp Aktion Führe Aktion n aus Führe Aktion 2 aus Führe Aktion 1 aus Zustand Ereignisse werden ohne zublockieren entgegengenommen !

  23. Prozesse und Threads A] Wie werden sie im Betriebssystem unterstützt: Prozessliste, Dispatcher, Betriebsmittelzuordnung B] Was muss man beim Programmieren von nebenläufigen Prozess-Systemen beachten: Race-Conditions, Synchronisation, Interprozess-Kommunikation

  24. Probleme nebenläufiger Abläufe • Wenn Abläufe miteinander interagieren: • Race Conditions:nichtdeterministische Einflüsse der Ausführungsgeschwindigkeit der Abläufe • Inkonsistente Zustände geteilter Betriebsmittel:Überlagerung partieller Zugriffe • deshalb Interprozesskommunikation (IPC) • Explizite Abstimmung des Ablaufs: Synchronisation • Expliziter Datenaustausch

  25. Interprocess CommunicationRace Conditions Two processes want to access shared memory at same time

  26. Interprozesskommunikation (IPC) • SynchronisationBlockieren und Fortsetzen von Threads / Prozessen • DatenaustauschÜbergabe von Daten zwischen Threads / Prozessen

  27. Synchronisation • Operationen zum direkten Warten und Fortsetzen • Operationen zum gepufferten Warten und Fortsetzen,insbesondere Semaphore

  28. Datenaustausch • Zugriff auf geteilte Betriebsmittel (z.B. globale Variablen, geteilter Speicher)+Zugriffssynchronisation • Gekapselte und synchronisierte geteilte Objekte: Monitore • Nachrichtenaustauschsysteme

  29. Synchronisation • Wichtiges Synchronisationsproblem:Gegenseitiger Ausschluss (Mutual Exclusion) Geteiltes Betriebsmittel t Kritischer Abschnitt

  30. Critical Regions (1) Four conditions to provide mutual exclusion • No two processes simultaneously in critical region • No assumptions made about speeds or numbers of CPUs • No process running outside its critical region may block another process • No process must wait forever to enter its critical region

  31. Critical Regions (2) Mutual exclusion using critical regions Einfache Lösung: Interrupts sperren (1-Prozessormaschine)

  32. Mutual Exclusion with Busy Waiting (1) global var: int turn P1: P2: Proposed solution to critical region problem:Aktives Warten / Busy Wait Problem 1: Prozessorbelastung Problem 2 (diese Lösung): Nur im strikten Wechsel

  33. Mutual Exclusion with Busy Waiting (2) Peterson's solution for achieving mutual exclusion

  34. Mutual Exclusion with Busy Waiting (3) Entering and leaving a critical region using the TSL instruction Test-And-Set-Befehl als unteilbare Einheit

  35. Passives WartenBeispiel Producer - Consumer P2:Consumer P1:Producer Puffer:n Plätze Kapazität Daten- paket Daten- paket .... zyklisch Paketeverarbeiten zyklisch Pakete erzeugen

  36. Sleep and Wakeup Producer-consumer problem with fatal race condition

  37. Semaphor (nach E.W.Dijkstra) • Globales Objekt, enthaltend: • Zählvariable // Mitzählen der Sperren/Freigaben • Queue mit IDs der Wartenden • Initialisierung explizit // Startwert: gesperrt / offen • Zugriffsoperationen: • Pbzw.down// Passieren (Sperre setzen und betreten) • Vbzw.up // Verlassen (Sperre lösen und verlassen)

  38. Semaphores The producer-consumer problem using semaphores

  39. Mutexes (effiziente binäre Semaphorezur Realisierung des gegenseitigen Ausschlusses) Implementation of mutex_lock and mutex_unlock

  40. Monitor • Gekapseltes und synchronisiertes geteiltes Objekt (Operationen im gegenseitigen Ausschluss, explizites Warten und Fortsetzen) P2 P1 • Monitor: • Variablen • Warteverwaltung • Zugriffsoperationen • Initialisierung

  41. Monitors (1) • Monitor: • Variablen • Warteverwaltung • Zugriffsoperationen • Initialisierung Example of a monitor

  42. Monitors (2) our_monitor • Outline of producer-consumer problem with monitors • only one monitor procedure active at one time • buffer has N slots

  43. Monitors (3) Solution to producer-consumer problem in Java (part 1)

  44. Monitors (4) Solution to producer-consumer problem in Java (part 2)

  45. Nachrichtenaustauschsysteme Nachricht receive send P2 P1 send receive • IPC-Kanäle • „Verdrahtung“ der Prozesse / Threads • Nachrichten-Sende- und Empfangsoperationen • Pufferung der Nachrichten im Kanal • Beispiel • Unix / Linux: Pipes

  46. Message Passing The producer-consumer problem with N messages

  47. Barriers • Use of a barrier • processes approaching a barrier • all processes but one blocked at barrier • last process arrives, all are let through

  48. Dining Philosophers (1) • Philosophers eat/think • Eating needs 2 forks • Pick one fork at a time • How to prevent deadlock

  49. Dining Philosophers (2) A nonsolution to the dining philosophers problem

  50. Dining Philosophers (3) Solution to dining philosophers problem (part 1)

More Related