processes creation and threads. shared resource example the console is just a place where text can...
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](https://reader036.vdocuments.us/reader036/viewer/2022062503/5a4d1ae57f8b9ab059978caf/html5/thumbnails/1.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062503/5a4d1ae57f8b9ab059978caf/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062503/5a4d1ae57f8b9ab059978caf/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062503/5a4d1ae57f8b9ab059978caf/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062503/5a4d1ae57f8b9ab059978caf/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062503/5a4d1ae57f8b9ab059978caf/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062503/5a4d1ae57f8b9ab059978caf/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062503/5a4d1ae57f8b9ab059978caf/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062503/5a4d1ae57f8b9ab059978caf/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062503/5a4d1ae57f8b9ab059978caf/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062503/5a4d1ae57f8b9ab059978caf/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062503/5a4d1ae57f8b9ab059978caf/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062503/5a4d1ae57f8b9ab059978caf/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062503/5a4d1ae57f8b9ab059978caf/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062503/5a4d1ae57f8b9ab059978caf/html5/thumbnails/15.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062503/5a4d1ae57f8b9ab059978caf/html5/thumbnails/16.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062503/5a4d1ae57f8b9ab059978caf/html5/thumbnails/17.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062503/5a4d1ae57f8b9ab059978caf/html5/thumbnails/18.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062503/5a4d1ae57f8b9ab059978caf/html5/thumbnails/19.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062503/5a4d1ae57f8b9ab059978caf/html5/thumbnails/20.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062503/5a4d1ae57f8b9ab059978caf/html5/thumbnails/21.jpg)
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.