processes creation and threads. shared resource example the console is just a place where text can...

21
Processes Creation and Threads

Upload: thomasine-mosley

Post on 08-Jan-2018

215 views

Category:

Documents


0 download

DESCRIPTION

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”?

TRANSCRIPT

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

Processes Creation and

Threads

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

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().

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

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”?

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

Realization• Only 1 thread can be active at any

time.• Switching between threads can

happen any time in most operating systems.

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

SampleThread1() {

print “This is the first thread”}

Thread2() {print “This is the second thread”

}

Main(){

start Thread1start Thread2wait for both threads to finish

}

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

Output on Terminal…• Many possibilities…

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

Problem…• Suppose we want Thread B to wait

for Thread A before starting.

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

Solution 1int turn;

Thread1() {print “This is the first thread”turn = 2

}

Thread2() {while (turn = 2);print “This is the second thread”

}

Main(){

turn = 1start Thread1start Thread2wait for both threads to finish

}

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

Solution 2int 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 = 1start Thread1start Thread2wait for both threads to finish

}

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

Solution 3int turn;

Thread1() {print “This is the first thread”Send signal

}

Thread2() {wait for signalprint “This is the second thread”

}

Main(){

start Thread1start Thread2wait for both threads to finish

}

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

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”.

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

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.

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

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.

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

SMS Messages

In box

Sender Receiver

The inbox can store many messages.

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

SMS Messages

Message queue

The message queue is like an inbox but is often part of the OS

SendingProcess

ReceivingProcess

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

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.

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

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.

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

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.

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

Picture

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

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”.

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

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.