RINNAKKAINEN OHJELMOINTI 815301A, 18.6.2005 1. Vastaa lyhyesti (2p kustakin): a) Mitkä ovat rinnakkaisen ohjelman oikeellisuuskriteerit? b) Mitä tarkoittaa laiska säikeen luominen? c) Mitä ovat kohtaaminen ja laajennettu kohtaaminen? 2. Petersonin algoritmi poissulkevuusongelman ratkaisuksi kahden säikeen S0 ja S1 tapauksessa on pseudokoodilla kuvattuna seuraava: int last = 0; boolean Critical0 = false; boolean Critical1 = false; S0: Critical0 = true; last = 0; while(critical1 && last == 0) Critical0 = false; S1: Critical1 = true; last = 1; while(critical0 && last == 1) Critical1 = false; Osoita, että algoritmi on poissulkevuusongelman ratkaisu. Jatkuu toisella puolella ->
3. Kirjoita seuraava Java-ohjelma: Ohjelmassa toimii pääohjelmasäikeen lisäksi toinen säie, joka tulostaa konsolille alenevassa järjestyksessä luvut 10:stä nollaan sekunnin välein. Tulostettuaan nollan säie päättyy. Pääohjelma ei saa päättyä ennen säikeen päättymistä; pääohjelman loppuessa tulostetaan konsolille ilmoitus Laskenta loppui. 4. Vertaile semaforia ja monitoria rinnakkaisuuden kontrollirakenteina. 5. Liitteenä olevassa Java -ohjelmassa ProgTest on määritelty luokka ArrayCopy, jonka oliot sisältävät jäsenmuuttujanaan kokonaislukutaulukon. Luokassa on metodi copyto, jonka avulla taulukon arvot voidaan kopioida parametrina annettuun kokonaislukutaulukkoon. Lisäksi luokan metodi copymet kopioi toisen ArrayCopy -olion taulukon arvot olion taulukkoon. Ohjelmassa luodaan kaksi luokan oliota, joiden taulukkojen arvot kopioidaan toisiinsa kahdessa eri säikeessä. Ohjelmassa on kuitenkin eräs tyypillinen rinnakkaisuuteen liittyvä virhe. Minkälainen virhetilanne tästä voi ohjelmaa suoritettaessa aiheutua? Kuvaa yksityiskohtaisesti virhetilanteen synty. Tentin tulokset julkistetaan viimeistään 31.8.2005.
LIITE. Tehtävän 5 Java ohjelma: class ArrayCopy int[] myarray; public ArrayCopy(int[] array) myarray = array; synchronized public void copymet(arraycopy another) another.copyto(myarray); synchronized public void copyto(int[] destarray) for(int i = 0; i < myarray.length; i++) if(i < destarray.length) destarray[i] = myarray[i]; // class ArrayCopy public class ProgTest public static void main(string [] argv ) int[] firstarray = 1,2,3,4,5; int[] secondarray = 8,7,6,5,4,3,2,1; final ArrayCopy firstcopier = new ArrayCopy(firstArray); final ArrayCopy secondcopier = new ArrayCopy(secondArray); Runnable ri = new Runnable() firstcopier.copymet(secondcopier); ; Runnable rii = new Runnable() secondcopier.copymet(firstcopier); ; Thread T1 = new Thread(rI); Thread T2 = new Thread(rII); T1.start(); T2.start();
PARALLEL PROGRAMMING (RINNAKKAINEN OHJELMOINTI, ENGLISH) 815301A, 18.6.2005 1. Give short answers (2p each): a) What are the two main criteria for parallel program correctness? b) What does lazy thread creation mean in parallel programming? c) What do rendezvous and extended rendezvous mean in parallel programming? 2. Peterson s algorithm for the solution of the mutual exclusion problem, in the case of two threads S0 and S1, is in pseudo code: int last = 0; boolean Critical0 = false; boolean Critical1 = false; S0: Critical0 = true; last = 0; while(critical1 && last == 0) Critical0 = false; S1: Critical1 = true; last = 1; while(critical0 && last == 1) Critical1 = false; Prove that the algorithm indeed is a solution to the problem. Continued on the other side ->
3. Write a Java program that, in addition to the main program thread, includes one additional thread printing numbers from 10 to 0 in descending order on the console. Numbers are printed with one second interval, until zero is printed, after which the thread ends. The main program thread is not allowed to end before the other thread ends, and, when stopping, the main thread prints Countdown finished. 4. Compare monitor and semaphore as control structures of concurrency. 5. Attached is Java program ProgTest, defining a class ArrayCopy, whose objects include an integer array as a class variable. The class contains the method copyto that can be used to copy values from the integer array to an array that is given as a parameter to the method. Also, the class contains the method copymet that copies the values from the array of another ArrayCopy object to object s array. Program creates two ArrayCopy objects, whose arrays are copied in two separate threads. The program, however, contains a typical mistake relating to concurrency. What kind of error situation can this cause, when program is executed? Describe in detail, how the error can occur. Results are available not later than 31.8.2005.
ATTACHMENT. Java program for problem number 5: class ArrayCopy int[] myarray; public ArrayCopy(int[] array) myarray = array; synchronized public void copymet(arraycopy another) another.copyto(myarray); synchronized public void copyto(int[] destarray) for(int i = 0; i < myarray.length; i++) if(i < destarray.length) destarray[i] = myarray[i]; // class ArrayCopy public class ProgTest public static void main(string [] argv ) int[] firstarray = 1,2,3,4,5; int[] secondarray = 8,7,6,5,4,3,2,1; final ArrayCopy firstcopier = new ArrayCopy(firstArray); final ArrayCopy secondcopier = new ArrayCopy(secondArray); Runnable ri = new Runnable() firstcopier.copymet(secondcopier); ; Runnable rii = new Runnable() secondcopier.copymet(firstcopier); ; Thread T1 = new Thread(rI); Thread T2 = new Thread(rII); T1.start(); T2.start();