Download presentation
Presentation is loading. Please wait.
Published byBrendan Tucker Modified over 9 years ago
1
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization Codes
2
6.2 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Example I Design a producer consumer problem with single buffer
3
6.3 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Producer
4
6.4 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Consumer
5
6.5 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Tester Class
6
6.6 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Example 2 (without separate classes) import java.util.Random; public class ProducerConsumerProblem { public static void main(String[] args) { final Buffer buffer = new Buffer(); // buffer // producer thread Thread producer = new Thread(new Runnable() { public void run() { Random r = new Random(); for (int i = 0; i < 10; i++) { try { int x = r.nextInt(100-2) + 2; buffer.put(x); } catch (InterruptedException e) { e.printStackTrace(); } }); // consumer thread Thread consumer = new Thread(new Runnable() { public void run() { for (int i = 0; i < 10; i++) { try { buffer.get(); } catch (InterruptedException e) { e.printStackTrace(); } });
7
6.7 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Bounded Buffer Problem (buffer as stack)
8
6.8 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Tester Code
9
6.9 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Semaphore (mutex) import java.util.concurrent.Semaphore; //library public class SemaphoreTest { Semaphore binary = new Semaphore(1); public static void main(String args[]) { final SemaphoreTest test = new SemaphoreTest(); new Thread(){ @Override public void run(){ test.mutualExclusion(); } }.start(); new Thread(){ @Override public void run(){ test.mutualExclusion(); } }.start(); }
10
6.10 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Counting Semaphore It will take more than two values
11
6.11 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Semaphore methods Notify and notifyAll()
12
6.12 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Process class
13
6.13 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Tester
14
6.14 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Semaphores
15
6.15 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Monitors
16
6.16 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Monitors (Contd…)
17
6.17 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Monitor Class
18
6.18 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Output
19
6.19 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.