Presentation is loading. Please wait.

Presentation is loading. Please wait.

Processes Creation and Threads. Shared Resource Example The console is just a place where text can be printed by the application currently running. Program.

Similar presentations


Presentation on theme: "Processes Creation and Threads. Shared Resource Example The console is just a place where text can be printed by the application currently running. Program."— Presentation transcript:

1 Processes Creation and Threads

2 Shared Resource Example The console is just a place where text can be printed by the application currently running. Program writes to the console using system.out.println().

3 Problem… Any thread calling system.out.println() will result in a message being displayed on the screen. What if there are two threads trying to write messages to the screen at “the same time”?

4 Realization Only 1 thread can be active at any time. Switching between threads can happen any time in most operating systems.

5 Sample Thread1() { print “This is the first thread” } Thread2() { print “This is the second thread” } Main() { start Thread1 start Thread2 wait for both threads to finish }

6 Output on Terminal… Many possibilities…

7 Problem… Suppose we want Thread B to wait for Thread A before starting.

8 Solution 1 int turn; Thread1() { print “This is the first thread” turn = 2 } Thread2() { while (turn = 2) ; print “This is the second thread” } Main() { turn = 1 start Thread1 start Thread2 wait for both threads to finish }

9 Solution 2 int turn; Thread1() { print “This is the first thread” turn = 2 } Thread2() { while (turn = 1) sleep 5 seconds; print “This is the second thread” } Main() { turn = 1 start Thread1 start Thread2 wait for both threads to finish }

10 Solution 3 int turn; Thread1() { print “This is the first thread” Send signal } Thread2() { wait for signal print “This is the second thread” } Main() { start Thread1 start Thread2 wait for both threads to finish }

11 Semaphore A variable on which a thread (or process) can wait. The operation wait is often called “take”. The operating sending the signal is often called “give”.

12 Cell Phone Example Only 1 person can use a phone to call at any given time. If a person wants to make a call they must “take” the phone. When a person is finished they “give” the phone. If a person tries to take the phone and it is currently taken, that person must wait.

13 Message Queues When 2 people want to exchange information they often use SMS messages. When 2 threads (or processes) want to exchange information they rely on messaging techniques.

14 SMS Messages In box SenderReceiver The inbox can store many messages.

15 SMS Messages Message queue The message queue is like an inbox but is often part of the OS Sending Process Receiving Process

16 Observations Multiple senders to a single receiver is easy. Having multiple receivers using the same message queue is not simple. Most systems allow for high priority messages to be sent. Most systems allow receiver to block waiting for a message. If the receiver isn’t looking, they won’t know they have a message.

17 Problem… There are 4 students sitting around a table. There are 4 phones arranged between each pair of students. Students sleep for a random amount of time, then they decide to make a phone call.

18 Continued. To make a phone call each student must use 2 phones at the same time. Students can only pick up 1 phone at a time and must start with the left phone. If a phone is not available the student must wait. Once a phone is picked up, the student will hang onto it until they done with their call.

19 Picture

20 The problem… If all students wake up at the same time and decide to make a phone call: –All students pick up the left phone and will wait for the right phone. –The right phone will never be available. This is called “deadlock”. In big cities with traffic problems there is often called “gridlock”.

21 In computing If a process acquires 1 resource (takes) and waits forever for a second resource without giving up the first, you have the potential for deadlock. Detecting and preventing is difficult! Not a subject for this class.


Download ppt "Processes Creation and Threads. Shared Resource Example The console is just a place where text can be printed by the application currently running. Program."

Similar presentations


Ads by Google