De presentatie wordt gedownload. Even geduld aub

De presentatie wordt gedownload. Even geduld aub

Slide 1Programmatuur voor real-time controleYolande Berbers RTPReal-Time Programmatuur hoofdstuk 7: concurrent programming.

Verwante presentaties


Presentatie over: "Slide 1Programmatuur voor real-time controleYolande Berbers RTPReal-Time Programmatuur hoofdstuk 7: concurrent programming."— Transcript van de presentatie:

1 slide 1Programmatuur voor real-time controleYolande Berbers RTPReal-Time Programmatuur hoofdstuk 7: concurrent programming

2 RTP slide 2Programmatuur voor real-time controleYolande Berbers kenmerken van een RTS (copie van slide van H1) n verschillende software componenten zijn gelijktijdig actief u de apparaten in de omgeving werken gelijktijdig u deze gelijktijdigheid wordt het best gerealiseerd door gelijktijdig uitvoerende software componenten u meer en meer real-time systemen zijn gedistribueerde systemen u dit vraagt ondersteuning van programmeertalen en/of systemen

3 RTP slide 3Programmatuur voor real-time controleYolande Berbers gelijktijdigheid: inleiding n Multiprogrammatie u gelijktijdige processen u processen: onafhankelijk of samenwerkend n Monoprocessor u multiplexing van de CPU n Multiprocessor u processen voeren echt gelijktijdig uit

4 RTP slide 4Programmatuur voor real-time controleYolande Berbers samenwerkende processes n samenwerkende processen: u elke proces wordt beïvloed door of beïnvloed andere processen u ze delen informatie (gegevens, toestandsinformatie, …) u veronderstelt “communicatie mechanisme” n voordelen: u modulariteit u versnelling l monoprocessor: als ene moet wachten kan andere verder l multiprocessor: werkelijk in parallel

5 RTP slide 5Programmatuur voor real-time controleYolande Berbers communicatie mechanismen n gedeeld geheugen (zie H8) n booschappen (zie H9) u zenden - ontvangen n sturen van events (zie H11) u asynchroon

6 RTP slide 6Programmatuur voor real-time controleYolande Berbers gedeeld geheugen n processen delen stuk van geheugen n besturingssyst.oproepen voor export / import geheugen n communicatie zonder tussenkomst besturingssysteem Process AProcess B ExportImport Store Ri,xyzLoad Rj,xyz

7 RTP slide 7Programmatuur voor real-time controleYolande Berbers besturings- systeem communicatie met boodschappen n geen gedeeld geheugen n besturingssysteemoproepen voor zenden / ontvangen van boodschappen Process AProcess B SendReceive

8 RTP slide 8Programmatuur voor real-time controleYolande Berbers events (zie H11) n vergelijkbaar met interrupts n besturingssyst.oproep voor signaleren van event (kill) / voor aanduiden van handler (signal) (volgt het resumptionmodel) Process A Process B KillSignal

9 RTP slide 9Programmatuur voor real-time controleYolande Berbers concurrent programming: inleiding n concurrent programming = notaties en technieken voor u uitdrukken van potentieel parallellisme u oplossen van synchronisatie en communicatie n concurrent programming biedt u abstractiemechanismen om parallellisme te beschrijven u onafhankelijk van de implementatiedetails n de implementatie van parallellisme u onderwerp van computersystemen (hardware en software) u is onafhankelijk van concurrent programming

10 RTP slide 10Programmatuur voor real-time controleYolande Berbers seq. vs concurrent prog. talen n sequentiële prog.talen: Pascal, C, C++, Fortran, COBOL u programma’s hebben slechts 1 controledraad (Engels: thread) u ze voeren uit, bevel per bevel, totdat het programma eindigt u het pad door het programma kan variëren al naar gelang de invoer, maar bij gelijke invoer heb je een gelijk pad n ‘concurrent’ programmeertalen (programmeertalen voor gelijklopende onderdelen) bevatten de notie van proces u elke proces heeft (tenminste) 1 controledraad u implementatie van processen: l uitvoering op 1 enkele processor (multiprogrammatie) l uitvoering op multiprocessor (met gedeeld geheugen) l uitvoering op multicomputer (met gescheiden geheugen) u concurrent slaat op potentieel parallellisme

11 RTP slide 11Programmatuur voor real-time controleYolande Berbers een proces en zijn levencyclus gecreëerd niet bestaand initialiserend uitvoerbaar beëindigd Proces Code Data + Stack PC

12 RTP slide 12Programmatuur voor real-time controleYolande Berbers seq. vs concurrent prog. talen n RTSS (run-time support system) u heeft de taak van het creëren en initialiseren van een proces u eventueel ook van het schedulen van de verschillende controledraden u wordt meestal door de compiler gegenereerd n alternatief voor concurrent programmeertalen u gescheiden processen schrijven met een sequentiële taal u de mogelijkheden van het besturingssysteem gebruiken om gelijktijdige uitvoering te bekomen

13 RTP slide 13Programmatuur voor real-time controleYolande Berbers proces, controledraad n processen u worden voorzien in alle besturingssystemen u elke proces voert uit in een eigen virtuele machine, zonder interferentie van andere processen (logisch gezien alsof ze het enige proces zijn) n controledraden (threads) u soort subproces binnen een klassiek proces u alle controledraden hebben toegang tot hele virtuele machine u de programmeur en de taal moeten de controledraden voor elkaar beschermen

14 RTP slide 14Programmatuur voor real-time controleYolande Berbers Threads Process Code Data + Stack Threads Code Data + Stack PC Thread, LWP (Lightweight Process)

15 RTP slide 15Programmatuur voor real-time controleYolande Berbers Process Code Data Stack Resources Threads Process Code Data Stack PC Registers Other state... Resources PC Registers Other state... PC Registers Other state... Stack

16 RTP slide 16Programmatuur voor real-time controleYolande Berbers Threads n Thread u own state IC, registers, CC, …, stack (+ SP) u code, data, other resources: shared n Disadvantage u no protection between threads of 1 task n Advantages: u context switch = less overhead u thread creation / termination = cheap

17 RTP slide 17Programmatuur voor real-time controleYolande Berbers Threads Implementation User-Level Threads n Library Routines n Switching = Fast! n Blocking SVC!   threads blocked! n Scheduling (process) n E.g. Unix Supported by OS n SVC CREATE-THREAD n More Overhead n Blocking SVC  other threads still run n Scheduling (thread) n E.g. OS/2, Solaris,...

18 RTP slide 18Programmatuur voor real-time controleYolande Berbers Threads Client Server Client Server

19 RTP slide 19Programmatuur voor real-time controleYolande Berbers Threads

20 RTP slide 20Programmatuur voor real-time controleYolande Berbers Threads n Kernel (OS): u single-threaded vs multi-threaded u single-thread: at most 1 process active in kernel u multi-thread: many processes active in kernel l synchronization! (shared data structures)

21 RTP slide 21Programmatuur voor real-time controleYolande Berbers seq. vs concurrent prog. talen n discussie: op welk niveau moet concurrency aangeboden worden: door de taal of door het besturingssysteem ? u Ada en Java bieden concurrency in de taal u C en C++ niet

22 RTP slide 22Programmatuur voor real-time controleYolande Berbers seq. vs concurrent prog. talen n voordelen van concurrent programmeertalen u beter leesbare en onderhoudbare code u draagbaarder over meerdere besturingssystemen heen u soms is er geen besturingssysteem op een real-time systeem u de compiler kan op een aantal zaken testen n voordelen combinatie sequentiële taal + besturingssyst u gemakkelijker om programma's geschreven in verschillende talen te combineren u op sommige besturingssystemen is het moeilijk om sommige concurrent taalmodellen te implementeren u omdat er standaarden zijn voor besturingssystemen, zijn de programma's draagbaarder

23 RTP slide 23Programmatuur voor real-time controleYolande Berbers constructies voor concurrent programmeren n 3 fundamentele faciliteiten u uitdrukken van gelijktijdig uitvoeren (door de notie van proces) u synchronisatie tussen processen u communicatie tussen processen n 3 verschillende soorten relaties u onafhankelijk u samenwerkend u in competitie

24 RTP slide 24Programmatuur voor real-time controleYolande Berbers gelijktijdig uitvoeren n verschillen tussen procesmodellen u structuur u niveau u granulariteit u initialisatie u beëindiging u relaties tussen gecreëerd proces en andere u voorstelling

25 RTP slide 25Programmatuur voor real-time controleYolande Berbers gelijktijdig uitvoeren n structuur u statisch l aantal processen is vast l dit aantal is gekend tijdens compilatie u dynamisch l aantal processen kan variëren van uitvoering tot uitvoering n niveau u vlak l processen enkel gedefinieerd op hoogste niveau u genest l processen kunnen een hiërarchie vormen: processen kunnen andere processen starten (vader/kind)

26 RTP slide 26Programmatuur voor real-time controleYolande Berbers gelijktijdig uitvoeren n granulariteit u fijn l het is ‘goedkoop’ om processen te creëren l in een typisch programma worden er veel gecreëerd voor telkens een hele kleine taak u grof l het is ‘duur’ om een proces te creëren l er worden geen grote aantallen processen gecreëerd n initialisatie u parameters doorgeven u expliciete communicatie na creatie

27 RTP slide 27Programmatuur voor real-time controleYolande Berbers gelijktijdig uitvoeren n beëindiging u beëindigen van de code u door uitvoering van een specifiek ‘zelfmoord’ bevel u expliciete moord door een ander proces u uitvoering van een niet-opgevangen uitzondering u nooit (oneindige lus) u wanneer niet meer nodig (geen communicatie meer mogelijk) n relatie tussen gecreëerd proces en andere u vader/kind: vader (creërend proces) wacht tot het kind gecreëerd en geïnitialiseerd is u hoeder/afhankelijke: hoeder kan niet het blok verlaten waarin de afhankelijke is gecreëerd, voordat de afhankelijke beëindigd is u vader en hoeder kunnen maar hoeven niet zelfde proces te zijn

28 RTP slide 28Programmatuur voor real-time controleYolande Berbers Process States gecreëerd niet bestaand initialiserend uitvoerbaar beëindigd wacht op init kind wacht op afhank. einde

29 RTP slide 29Programmatuur voor real-time controleYolande Berbers gelijktijdig uitvoeren n terminologie bij object-georiënteerde systemen u elke proces is een object (maar niet omgekeerd) u actieve objecten: nemen spontaan acties u reactieve objecten l reageren enkel op invocaties (= operaties die uitgevoerd worden op een object) l bv resources (hulpmiddelen) l passieve objecten s reactieve objecten waarvan de operaties altijd kunnen uitgevoerd worden u beschermde resource: beschermd door passieve entiteit u server: resource waarbij de controle actief is (proces is nodig)

30 RTP slide 30Programmatuur voor real-time controleYolande Berbers gelijktijdig uitvoeren n voorstelling: 4 basis mechanismen u co-routines u fork/join u cobegin/coend u expliciete procesdeclaratie

31 RTP slide 31Programmatuur voor real-time controleYolande Berbers procesvoorstelling: co-routines n controle wordt expliciet doorgegeven (sleutelwoord resume): er is geen return-bevel n relatie is symmetrisch n een routine A voert ‘resume B’ uit u routine A stopt uitvoeren u routine B voert uit vanaf het punt waar het vroeger gestopt was u routine A bewaart alle statusinformatie, zodat het later op hetzelfde punt kan verder gezet worden

32 RTP slide 32Programmatuur voor real-time controleYolande Berbers procesvoorstelling: co-routines Co-routine ACo-routine BCo-routine C resume B resume C resume A resume B 12345 6 6 789 10 11 12 13 12 14 15

33 RTP slide 33Programmatuur voor real-time controleYolande Berbers procesvoorstelling: fork en join n methode: u C := fork Feen nieuw proces start de uitvoering van F oproepend proces gaat ook verder u join C;oproepend proces wacht op einde F n gebruikt in POSIX u bij fork kun je parameters doorgeven u met wait wacht je, 1 waarde keert terug u flexibel maar niet erg gestructureerd l geven gemakkelijk aanleiding tot fouten l hoeder moet bv expliciet wachten op al zijn kinderen

34 RTP slide 34Programmatuur voor real-time controleYolande Berbers procesvoorstelling: fork en join tijdsas fork join PFPF vaderproces voert join uit voordat kind beëindigd is kindproces heeft gedaan voordat vader join uitvoert

35 RTP slide 35Programmatuur voor real-time controleYolande Berbers procesvoorstelling: cobegin/coend n na cobegin (of parbegin) staan een aantal blokken die in parallel uitgevoerd worden (ze worden allemaal samen opgestart) n het vaderproces wacht, of voert 1 van de blokken uit n bij coend (of parend) wordt er gewacht tot alle blokken beëindigd zijn

36 RTP slide 36Programmatuur voor real-time controleYolande Berbers procesvoorstelling: cobegin/coend tijdsas coend cobegin

37 RTP slide 37Programmatuur voor real-time controleYolande Berbers procesvoorstelling: ex-/impliciete procesdeclaratie n expliciete procesdeclaratie u routines geven tekstueel aan of ze door een apart proces uitgevoerd worden (modula 1) u het is niet meer de oproeper van de routine die aangeeft dat een nieuw proces gecreëerd moet worden n impliciete procesdeclaratie u alle processen die gedeclareerd worden binnen een blok beginnen in parallel uit te voeren bij begin van het blok (Ada)

38 RTP slide 38Programmatuur voor real-time controleYolande Berbers gelijktijdige uitvoering in Ada n benaming voor sequentieel proces: task u kan gedeclareerd worden u wordt gecreëerd wanneer declaratie zichtbaar wordt: impliciet u procedure eenvoudige_taak eindigt niet voordat task A eindigt procedure eenvoudige_taak is task A; task body A is -- lokale declaraties begin -- bevelen van de taak A end A; begin...-- taak A begint met uitvoeren voor het eerste bevel in dit blok end eenvoudige_taak;

39 RTP slide 39Programmatuur voor real-time controleYolande Berbers gelijktijdige uitvoering in Ada n het is mogelijk een task-type te definiëren n maakt het mogelijk om een rij van taken te declareren task type T; type Long is array (1..100) of T; L: Long; task body T is -- lokale declaraties begin -- bevelen van de taak T end T;

40 RTP slide 40Programmatuur voor real-time controleYolande Berbers gelijktijdige uitvoering in Ada n parameters kunnen doorgegeven worden bij initialisatie procedure Main is type Dimension is (Xplane, Yplane); task type Control (Dim: Dimension); C1: Control(Xplane); C2: Control(Yplane); task body Control is -- lokale declaraties begin -- bevelen van de taak Control (zie in boek) end Control; begin null; end Main;

41 RTP slide 41Programmatuur voor real-time controleYolande Berbers gelijktijdige uitvoering in Ada n uitzonderingen u bij initialisatie: de creërende taak krijgt de uitzondering ‘Tasking_error’ u tijdens uitvoering: l de nieuwe taak mag de uitzonderingen opvangen l niet opgevangen uitzondering: nieuwe taak wordt beëindigd n beëindiging van een taak u de taak beëindigt zijn uitvoering (normaal of door uitzondering) u de taak voert het bevel ‘terminate’ uit (zie later) u de taak is gestopt door een andere taak (via abort)

42 RTP slide 42Programmatuur voor real-time controleYolande Berbers gelijktijdige uitvoering in POSIX n twee mechanismen u voor processen l fork() creëert een exacte copie van het oproepende proces l met wait() kan het oproepende proces wachten op het gecreëerde u voor threads l allerhande routines voor creatie en beheer van threads l vaak met veel parameters l nogal complex n beide mechanismen situeren zich op het niveau van het besturingssysteem (zijn dus niet in een taal geïntegreerd)

43 RTP slide 43Programmatuur voor real-time controleYolande Berbers gelijktijdige uitvoering in POSIX n threads in POSIX u alle threads hebben attributen (vb stack size) l attribute object om deze attributen te manipuleren typedef … pthread_t; typedef … pthread_attr_t; int pthread_attr_init (pthread_attr_t *attr); /* initializes a thread attribute pointed at by attr to default values */ int pthread_setstacksize (pthread_attr_t *attr, size_t stacksize); /* set the stack size of a thread attributed */ int pthread_attr_setstrackaddr (pthread_attr_t *attr, void *stackaddr); /* set the stack address of a thread attribute */

44 RTP slide 44Programmatuur voor real-time controleYolande Berbers gelijktijdige uitvoering in POSIX n threads in POSIX u alle threads hebben een identifier, uniek in het proces l thread kan deze identifier bekomen via pthread_self typedef … pthread_t; pthread pthread_self (void); /* return the thread_id of the calling thread */ int pthread_equal (pthread_t t1, pthread_t t2); /* compare two thread ids */

45 RTP slide 45Programmatuur voor real-time controleYolande Berbers gelijktijdige uitvoering in POSIX n threads in POSIX u thread is klaar om uit te voeren na creatie l pthread_create, 4 argumenten: thread identifier, attributes, functie met de code, parameters die doorgegeven moeten worden int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void * (*start_routine) (void*), void *arg); /* create a new thread with the given attributes and call the given start_routine with the given argument */

46 RTP slide 46Programmatuur voor real-time controleYolande Berbers gelijktijdige uitvoering in POSIX n threads in POSIX u thread kan eindigen l door oproep van pthread_exit l bij ontvangst van een signal (zie hoofdstuk 10) l gestopt door pthread_cancel int pthread_exit (void *value_ptr); /* terminate the calling thread and make the pointer value_ptr available to any joining thread */

47 RTP slide 47Programmatuur voor real-time controleYolande Berbers gelijktijdige uitvoering in POSIX n threads in POSIX u wachten op het einde van een andere thread: pthread_join int pthread_join (pthread_t thread, void **value_ptr); /* suspends the calling thread until the named thread has terminated, and returned values are pointed at by value_ptr */

48 RTP slide 48Programmatuur voor real-time controleYolande Berbers gelijktijdige uitvoering in POSIX n threads in POSIX u opkuisen na uitvoering en vrijgave van geheugen: l bij join l met detach attribute: geen join nodig int pthread_attr_setdetachstate (pthread_attr_t *attr, int *detachstate); /* set the detach state of the attribute */ int pthread_detach (pthread_t thread); /* the storage space associated with the given thread may be reclaimed when the thread terminates */

49 RTP slide 49Programmatuur voor real-time controleYolande Berbers Robot Arm in C/POSIX #include pthread_attr_t attributes; pthread_t xp, yp, zp; typedef enum {xplane, yplane, zplane} dimension; int new_setting(dimension D); void move_arm(int D, int P); void controller(dimension *dim) { int position, setting; position = 0; while (1) { setting = new_setting(*dim); position = position + setting; move_arm(*dim, position); }; /* note, process does not terminate */ }

50 int main() { dimension X, Y, Z; void *result; X = xplane, Y = yplane; Z = zplane; PTHREAD_ATTR_INIT(&attributes); /* set default attributes */ PTHREAD_CREATE(&xp, &attributes, (void *)controller, &X); PTHREAD_CREATE(&yp, &attributes, (void *)controller, &Y); PTHREAD_CREATE(&zp, &attributes, (void *)controller, &Z); PTHREAD_JOIN(xp, &result); /* need to block main program */ exit(-1); /* error exit, the program should not terminate */ } Need JOIN as when a process terminates, all its threads are forced to terminate SYS_CALL style indicates a call to sys_call with a check for error returns

51 RTP slide 51Programmatuur voor real-time controleYolande Berbers Threads in Java n Two techniques: u subclassing Thread and overriding run u Implementing Runnable Interface l for classes that extend another class l no multiple inheritence

52 RTP slide 52Programmatuur voor real-time controleYolande Berbers Threads in Java class FirstThread extends Thread { public FirstThread (String str) { super(str); } public void run (){ … what the thread should do … } } class SecondThread extends Thread { public SecondThread (String str) { super(str); } public void run () { … what this thread should do … } } public class TwoThreadsTest { public static void main (String [] args) { new FirstThread(“First”).start(); new SecondThread(“Second”).start(); }

53 RTP slide 53Programmatuur voor real-time controleYolande Berbers Threads in Java class Name extends Whatever implements Runnable { public Name (...) {... } public void run (){ … what the thread should do … } } public class ThreadsTest { public static void main (String [] args) { Name obj = new Name (…); Thread objThread = new Thread(obj, “NameThread”); objThread.start(); }

54 RTP slide 54Programmatuur voor real-time controleYolande Berbers Threads in Java n Lifecycle of a thread new Thread Runnable Dead Not Runnable run() terminates start( ) block on I/O sleep( millisec ) wait for a condition yield( )

55 RTP slide 55Programmatuur voor real-time controleYolande Berbers Robot Arm Example public class UserInterface { public int newSetting (int Dim) \{... \}... } public class Arm { public void move(int dim, int pos) \{... \} } UserInterface UI = new UserInterface(); Arm Robot = new Arm();

56 RTP slide 56Programmatuur voor real-time controleYolande Berbers Robot Arm Example public class Control extends Thread { private int dim; public Control(int Dimension) // constructor { super(); dim = Dimension; } public void run() { int position = 0; int setting; while(true) { Robot.move(dim, position); setting = UI.newSetting(dim); position = position + setting; }

57 RTP slide 57Programmatuur voor real-time controleYolande Berbers Robot Arm Example final int xPlane = 0; // final indicates a constant final int yPlane = 1; final int zPlane = 2; Control C1 = new Control(xPlane); Control C2 = new Control(yPlane); Control C3 = new Control(zPlane); C1.start(); C2.start(); C3.start();

58 RTP slide 58Programmatuur voor real-time controleYolande Berbers Alternative Robot Control public class Control implements Runnable { private int dim; public Control(int Dimension) // constructor { dim = Dimension; } public void run() { int position = 0; int setting; while(true) { Robot.move(dim, position); setting = UI.newSetting(dim); position = position + setting; }

59 RTP slide 59Programmatuur voor real-time controleYolande Berbers Alternative Robot Control final int xPlane = 0; final int yPlane = 1; final int zPlane = 2; Control C1 = new Control(xPlane); // no thread created yet Control C2 = new Control(yPlane); Control C3 = new Control(zPlane); // constructors passed a Runnable interface and threads created Thread X = new Thread(C1); Thread Y = new Thread(C2); Thread Z = new Thread(C2); X.start(); // thread started Y.start(); Z.start();

60 RTP slide 60Programmatuur voor real-time controleYolande Berbers Java Thread States deadblocked non-existing new executable Create thread object start run method exits stop, destroy

61 RTP slide 61Programmatuur voor real-time controleYolande Berbers Points about Java Threads n Java allows dynamic thread creation n Java (by means of constructor methods) allows arbitrary data to be passed as parameters n Java allows thread hierarchies and thread groups to be created but there is no master or guardian concept; Java relies on garbage collection to clean up objects which can no longer be accessed n The main program in Java terminates when all its user threads have terminated (see later) One thread can wait for another thread (the target) to terminate by issuing the join method call on the target's thread object. The isAlive method allows a thread to determine if the target thread has terminated

62 RTP slide 62Programmatuur voor real-time controleYolande Berbers A Thread Terminates: when it completes execution of its run method either normally or as the result of an unhandled exception; via its stop method — the run method is stopped and the thread class cleans up before terminating the thread (releases locks and executes any finally clauses) u the thread object is now eligible for garbage collection.  if a Throwable object is passed as a parameter to stop, then this exception is thrown in the target thread; this allows the run method to exit more gracefully and cleanup after itself  stop is inherently unsafe as it releases locks on objects and can leave those objects in inconsistent states; the method is now deemed obsolete (depreciated) and should not be used n by its destroy method being called -- destroy terminates the thread without any cleanup (never implemented in JVM)

63 RTP slide 63Programmatuur voor real-time controleYolande Berbers Daemon Threads n Java threads can be of two types: user threads or daemon threads n Daemon threads are those threads which provide general services and typically never terminate n When all user threads have terminated, daemon threads can also be terminated and the main program terminates The setDaemon method must be called before the thread is started n (Daemon threads provide the same functionality as the Ada ``or terminate'' option on the select statement)

64 RTP slide 64Programmatuur voor real-time controleYolande Berbers Thread Exceptions IllegalThreadStateException is thrown when:  start method is called and thread has already been started  the setDaemon method has been called and the thread has already been started SecurityException is thrown by security manager when:  a stop or destroy method has been called on a thread for which the caller does not have the correct permissions for the operation requested NullPointerException is thrown when:  A null pointer is passed to the stop method InterruptException is thrown if a thread which has issued a join method is woken up by the thread being interrupted rather than the target thread terminating

65 RTP slide 65Programmatuur voor real-time controleYolande Berbers Ada, Java and C/POSIX n Ada and Java provide a dynamic model with support for nested tasks and a range of termination options. n POSIX allows dynamic threads to be created with a flat structure; threads must explicitly terminate or be killed.

66 RTP slide 66Programmatuur voor real-time controleYolande Berbers een eenvoudig real-time systeem n aflezen van temperatuur en aanpassen van verwarming n aflezen van druk en aanpassen van pomp n telkens de waarden ook op het scherm printen P T S thermokoppel verwarming drukmeter pomp scherm

67 RTP slide 67Programmatuur voor real-time controleYolande Berbers een eenvoudig real-time systeem n 3 implementatiemethoden u 1 sequentieel programma l houdt geen rekening met het feit dat T, P en S logisch gezien gelijktijdig en onafhankelijk van elkaar zijn l vraagt geen ondersteuning van het run-time- of besturingssysteem u 3 sequentiële programma's l T, P en S zijn geschreven in een sequentiële taal l met primitieven van het besturingssysteem worden 3 processen hiervoor gecreëerd u 1 concurrent programma l T, P en S zijn 3 threads l run-time ondersteuning is nodig

68 RTP slide 68Programmatuur voor real-time controleYolande Berbers een eenvoudig real-time systeem u 1 sequentieel programma, houdt geen rekening met feit dat T, P en S logisch gezien gelijktijdig en onafhankelijk van elkaar zijn procedure Controller is TR : Temp_Reading; PR : Pressure_Reading; HS : Heater_Setting; PS : Pressure_Setting; begin loop Read(TR); Temp_Convert(TR, HS); Write(HS); Write(TR); Read(PR); Pressure_Convert(PR, PS); Write(PS); Write(PR); end loop; end Controller;

69 RTP slide 69Programmatuur voor real-time controleYolande Berbers een eenvoudig real-time systeem n 1 sequentieel programma: bespreking u temperatuurmetingen en drukmetingen gebeuren met gelijke intervallen (misschien hoeft dat niet in de praktijk) l mogelijke oplossing: een aantal if-then-else bevelen toevoegen u wanneer het programma bezig is met temperatuur kan er geen aandacht gegeven worden aan de druk en vice-versa u als probleem met lezen van 1 van de 2 metingen blokkeert het geheel (dus ook het deel dat daar niets mee te maken heeft) l mogelijke oplossing: testen of er kan gelezen worden (p. 195) s nadeel is hier wel dat men test via ‘polling’ (busy waiting) s dit vraagt veel processortijd u belangrijkste opmerking: geen weerspiegeling van werkelijkheid, waar temperatuur en druk volledig onafhankelijke subsystemen zijn

70 RTP slide 70Programmatuur voor real-time controleYolande Berbers een eenvoudig real-time systeem u 3 sequentiële programma's (met primitieven van het besturingssysteem worden 3 processen hiervoor gecreëerd) package Operating_System_Interface is type Thread_Id is private; type Thread is access procedure; function Create_Thread (Code: Thread) return Thread_Id;... end Operating_System_Interface; package processes is procedure Temp_Controller; procedure Pressure_Controller; end processes; package body processes is...

71 RTP slide 71Programmatuur voor real-time controleYolande Berbers een eenvoudig real-time systeem u 3 sequentiële programma's (met primitieven van het besturingssysteem worden 3 processen hiervoor gecreëerd) procedure Controller is TC, PS: Thread_Id; begin TC := Create_Thread (Temp_Controller’Access); PC := Create_Thread (Pressure_Controller’Access); end Controller; n bespreking u doordat de taal geen ondersteuning biedt is dit moeilijk te schrijven en te onderhouden (zeker voor grotere systemen) u bv niet erg duidelijk uit de code welke procedures nu gewone zijn en welke bedoeld zijn om als proces uit te voeren

72 RTP slide 72Programmatuur voor real-time controleYolande Berbers een eenvoudig real-time systeem u 1 concurrent programma waarbij T, P en S 3 threads zijn procedure Controller is task Pressure_Controller; task Temp_Controller; task body Temp_Controller is beginloop Read(TR); Temp_Convert(TR, HS); Write(HS); Write(TR); end loop; end Temp_Controller ; task body Pressure_Controller is... begin null; end Controller ;

73 RTP slide 73Programmatuur voor real-time controleYolande Berbers een eenvoudig real-time systeem n 1 concurrent programma: bespreking u de logica van de toepassing is mooi zichtbaar in de code u dit maakt het geheel leesbaar en onderhoudbaar n probleem dat telkens genegeerd werd: synchronisatie van de data die naar het scherm gestuurd worden (zie hoofdstuk 8)

74 RTP slide 74Programmatuur voor real-time controleYolande Berbers seq. vs concurrent prog. talen n voordelen van concurrent programmeertalen u beter leesbare en onderhoudbare code u draagbaarder over meerdere besturingssystemen heen u soms is er geen besturingssysteem op een real-time systeem u de compiler kan op een aantal zaken testen n voordelen combinatie sequentiële taal + besturingssyst u gemakkelijker om programma's geschreven in verschillende talen te combineren u op sommige besturingssystemen is het moeilijk om sommige concurrent taalmodellen te implementeren u omdat er standaarden zijn voor besturingssystemen, zijn de programma's draagbaarder n Ada/Java: voordelen groter dan nadelen


Download ppt "Slide 1Programmatuur voor real-time controleYolande Berbers RTPReal-Time Programmatuur hoofdstuk 7: concurrent programming."

Verwante presentaties


Ads door Google