Title: Paralel Prosesler
1Paralel Prosesler
Proses Rapor_Yaz Begin while (true)
do .......bir süre bekle... print(sayac) sayac
0 end end
- Proses Gözle
- Begin
- while (true) do
- ......olayi gözle......
- sayac
- end
- end
sayac ortak degisken
2Problem
Proses Rapor_Yaz Begin while (true)
do .......bir süre bekle... print(sayac) sayac
0 end end
- Proses Gözle
- Begin
- while (true) do
- ......olayi gözle......
- sayac
- end
- end
Kritik Bölge Ilan Edilir
1.) Program sayac noktasinda kesildi ve
sayac degeri 8 idi. 3.) devam edip 3 olay
gözledi ve sayac degerini 3 yapti , durdu.
2) print(sayac) ile ekrana 8 yazdi ve sonra
kesildi. 4.) sayac0 atamasi yapti, sayac
içindeki 3 bilgisi kayboldu
3Diger Problem
- sayac makine düzeyi anlaminda hangi
islemlerinden olusur
ACC ? 5 kesildi INC 6? sayac
sayaci ekrana yaz sayac? 0
LDA sayac INC STA sayac
4Semafor integer deger 0,1,2... kuyruk
isaretcisi prosesler bu kuyrukta askiya
alinirlar
- Kritik Bölgeye Giris ? P(mySemafor)
- ----- kritik bölge
- Çikista ? V(mySemafor)
V islemi If(kuyrukta bekleyen varmi) uyandir Els
e mySemafor
P islemi If(mySemaforgt0) mySemafor - - Else
kuyruk üzerinde bekle
5Monitor Yapisi
- Kritik bölüm içindeki kodlar bu monitor içine
yazilir - Böylece Monitor içinde hem kaynak degiskenler hem
de tüm kritik bölüm kodlari yer alir - Monitörün kod parçalarina entry ismi verilir
- Monitorden bir anda sadece bir proses
yararlanabilir.
Proses C
Proses A
Anahtar_al
Anahtar_ver
sayac
Proses B
Veri tabanini kullan
Proses D
mesgul
6Mutual - Obstacle
7Fatal Lock
8JAVA Thread
- public interface Runnable
-
- abstract public void run()
-
class Animation implements Runnable ...
public void run( ) while ( true ) // draw
Frames ...
Animation happy new Animation("Mr.
Happy") Thread myThread new Thread( happy )
myThread.start( )
9A natural-born thread
- class Animation extends Thread
- ...
- public void run( )
- while (true )
- // draw Frames ...
-
-
-
Animation bouncy new Animation("Bouncy")
bouncy.start( )
10Using an adapter
- class Animation
-
- public void startAnimating( )
- // do setup, load images, etc.
- ...
- // start a drawing thread
- myThread new Thread ( new Runnable( )
- public void run() drawFrames( )
- )
- myThread.start( )
-
-
- private void drawFrames( )
- // do animation ...
-
-
11Synchronization
- class SpreadSheet
- int cellA1, cellA2, cellA3
-
- synchronized int sumRow( )
- return cellA1 cellA2 cellA3
-
- synchronized void setRow( int a1, int a2, int
a3 ) - cellA1 a1
- cellA2 a2
- cellA3 a3
-
- ...
-
synchronized ( myObject ) //
Functionality that needs to be synced
12notifierMethod waiterMethod
- class MyThing
- synchronized void waiterMethod( )
- // do some stuff
- wait( ) // now wait for notifier to
do something - // continue where we left off
-
-
- synchronized void notifierMethod( )
- // do some stuff
- notify( ) // notify waiter that we've
done it - // do more things
-
-
- synchronized void relatedMethod( )
- // do some related stuff
-
- ...
-
13- import java.util.Vector
- class Producer extends Thread
- static final int MAXQUEUE 5
- private Vector messages new Vector( )
-
- public void run( )
- try
- while ( true )
- putMessage( )
- sleep( 1000 )
-
- catch( InterruptedException e )
-
-
- private synchronized void putMessage( )
- throws InterruptedException
- while ( messages.size( )
MAXQUEUE ) - wait( )
- messages.addElement( new
java.util.Date().toString( ) )
// called by Consumer public synchronized
String getMessage( ) throws
InterruptedException notify( )
while ( messages.size( ) 0 ) wait(
) String message (String)messages.fir
stElement( ) messages.removeElement(
message ) return message
// end of class Producer
14- public class Consumer extends Thread
- Producer producer
-
- Consumer(Producer p)
- producer p
-
-
- public void run( )
- try
- while ( true )
- String message
producer.getMessage( ) - System.out.println("Got message
" message) - sleep( 2000 )
-
-
- catch( InterruptedException e )
-
-
- public static void main(String args)
15Producer- Comsumer Output
- java Consumer
- The output is the timestamp messages created by
the Producer - Got message Sun Dec 19 033555 CST 1999
- Got message Sun Dec 19 033556 CST 1999
- Got message Sun Dec 19 033557 CST 1999
- ...
16Thread Priority
17class MyThread extends Thread String
message MyThread ( String message )
this.message message
public void run( ) while ( true )
System.out.println( message )
- public class Thready
- public static void main( String args )
- new MyThread("Foo").start( )
- new MyThread("Bar").start( )
-
- // end of class Thready
-
class Thready public static void main(
String args ) new
MyThread("Foo").start( ) Thread bar
new MyThread("Bar") bar.setPriority(
Thread.NORM_PRIORITY 1 ) bar.start(
)
18- public class SimpleThread extends Thread
- public SimpleThread(String str)
- super(str)
-
- public void run()
- for (int i 0 i lt 10 i)
- System.out.println(i " "
getName()) - try
- sleep((long)(Math.random()
1000)) - catch (InterruptedException e)
-
- System.out.println("DONE! " getName())
-
-
190 Jamaica 0 Fiji 1 Fiji 1 Jamaica 2 Jamaica 2
Fiji 3 Fiji 3 Jamaica 4 Jamaica 4 Fiji 5
Jamaica 5 Fiji 6 Fiji 6 Jamaica 7 Jamaica 7
Fiji 8 Fiji 9 Fiji 8 Jamaica DONE! Fiji 9
Jamaica DONE! Jamaica
- public class TwoThreadsDemo
- public static void main (String args)
- new SimpleThread("Jamaica").start()
- new SimpleThread("Fiji").start()
-
-
20Makarna Yiyen Filozoflar
5
1
1
5
2
4
4
3
2
3