Lec 5
Lec 5
Lec 5
LAB # 06
Statement Purpose:
This lab will give you an overview of multi-threading.
Activity Outcomes:
This lab teaches you the following topics:
• Overview of multi-threading
• Multithread programming
Instructor Note:
Lecture 9
Text book:
Galvin,PeterB.,GregGagne,andAbrahamSilberschatz.Operatingsystemconcepts.John
Wiley&Sons, Inc., 2016.
41
1) Stage J(Journey)
Introduction
Overview of Multithreading
Multithreading is the ability of a program or an operating system process to manage
its use by more than one user at a time and to even manage multiple requests by the
same user without having to have multiple copies of the programming running in the
computer. Each user request for a program or system service (and here a user can also
be another program) is kept track of as a thread with a separate identity. As programs
work on behalf of the initial request for that thread and are interrupted by other
requests, the status of work on behalf of that thread is kept track of until the work is
completed.
Locks
Lock: prevents someone from doing something.
• Lock before entering critical section, before accessing shared data
• unlock when leaving, after done accessing shared data
• wait if locked
o Lock::Acquire -- wait until lock is free, then grab it
o Lock::Release -- unlock, waking up a waiter if any
• These must be atomic operations -- if two threads are waiting for the lock, and
both see it's free, only one grabs it! With locks, the too much milk problem
becomes really easy!
lock->Acquire();
if (nomilk)
buy milk;
lock->Release();
Ways of implementing locks
• All require some level of hardware support
• Directly implement locks and context switches in hardware Implemented in
the Intel 432.
• Disable interrupts (uniprocessor only) Two ways for dispatcher to get control:
• internal events -- thread does something to relinquish the CPU
• external events -- interrrupts cause dispatcher to take CPU away
• On a uniprocessor, an operation will be atomic as long as a context switch
does not occur in the middle of the operation. Need to prevent both internal
and external events. Preventing internal events is easy.
42
and release would be done in the protected part of the operating system, but they
could be called by arbitrary user code.
2. Might want to take interrupts during critical section. For instance, what if the lock
holder takes a page fault? Or does disk I/O?
3. Many physical devices depend on real-time constraints. For example, keystrokes
can be lost if interrupt for one keystroke isn't handled by the time the next keystroke
occurs. Thus, want to disable interrupts for the shortest time possible. Critical
sections could be very long running.
2) Stage a1 (apply)
Lab Activities 1:
43
Return 0;
}
}
/* sleep() causes the current thread to suspend execution for a specified period. This is an
efficient means of making processor time available to the other threads of an application or
other applications that might be running on a computer system. */
44
Lab Activities 2:
Busy-waiting implementation
class Lock {
int value = FREE;
Lock::Acquire() {
Disable interrupts;
while (value != FREE) {
Enable interrupts; // allow interrupts
Disable interrupts;
}
value = BUSY;
Enable interrupts;
}
Lock::Release() {
Disable interrupts;
value = FREE;
Enable interrupts;
}
}
• Thread consumes CPU cycles while it is waiting. Not only is this inefficient, it
could cause problems if threads can have different priorities. If the busy-waiting
thread has higher priority than the thread holding the lock, the timer will go off, but
(depending on the scheduling policy), the lower priority thread might never run.
• Also, for semaphores and monitors, if not for locks, waiting thread may wait for
an arbitrary length of time. Thus, even if busy-waiting was OK for locks, it could be
very inefficient for implementing other primitives.
Lock::Acquire()
{
Disable interrupts;
while (value != FREE) {
put on queue of threads waiting for lock
change state to sleeping or blocked
}
value = BUSY;
Enable interrupts;
}
Lock::Release()
{
Disable interrupts;
if anyone on wait queue {
take a waiting thread off
put it on ready queue
45
change its state to ready
}
value = FREE;
Enable interrupts;
}
3) Stage v(verify)
Activity 1:
Write a program for matrix addition, subtraction and multiplication using multithreading.
4) Stage a2(assess)
Through viva or practical demonstration
46