Network Directory Has Been Removed: - For HP Workstations (Aegis, Intrepid) - Recommended
Network Directory Has Been Removed: - For HP Workstations (Aegis, Intrepid) - Recommended
n For now, we will mostly be concerned with n As distributed, Nachos does not support
code in the threads directory multiple processes, only threads
● All threads share / execute the same
n main.cc, threadtest.cc — a simple test of code (the Nachos source code)
the thread routines.
● All threads share the same global
n system.h, system.cc — Nachos variables (have to worry about synch.)
startup/shutdown routines.
n Some interesting functions:
n thread.h, thread.cc — thread data ● Thread::Fork( ) — create a new thread to
structures and thread operations such as run a specified function with a single
thread fork, thread sleep and thread finish. argument, and put it on the ready queue
● Thread::Yield( ) — if there are other
n scheduler.h, scheduler.cc — manages threads waiting to run, suspend this
the list of threads that are ready to run. thread and run another
● Thread::Sleep( ) — this thread is waiting
n list.h, list.cc — generic list management. on some event, so suspend it, and hope
someone else wakes it up later
n utility.h, utility.cc — some useful
definitions and debugging routines. ● Thread::Finish( ) — terminate the
currently running thread
5 Fall 1998, Lecture 09 6 Fall 1998, Lecture 09
void void
Thread::Fork(VoidFunctionPtr func, int arg) Thread::Yield ()
{ {
DEBUG('t',"Forking thread \"%s\" with Thread *nextThread;
func = 0x%x, arg = %d\n",
name, (int) func, arg); IntStatus oldLevel = interrupt->
SetLevel(IntOff);
StackAllocate(func, arg);
ASSERT(this == currentThread);
IntStatus oldLevel = interrupt-> DEBUG('t', "Yielding thread \"%s\"\n",
SetLevel(IntOff); getName());
scheduler->ReadyToRun(this);
(void) interrupt->SetLevel(oldLevel); nextThread = scheduler->
} FindNextToRun();
if (nextThread != NULL) {
scheduler->ReadyToRun(this);
scheduler->Run(nextThread);
}
(void) interrupt->SetLevel(oldLevel);
}
7 Fall 1998, Lecture 09 8 Fall 1998, Lecture 09
Manipulating Threads in Nachos
(cont.)
void
Thread::Sleep ()
{
Thread *nextThread;
ASSERT(this == currentThread);
ASSERT(interrupt->getLevel() == IntOff);
DEBUG('t', "Sleeping thread \"%s\"\n",
getName());
status = BLOCKED;
while ((nextThread = scheduler->
FindNextToRun()) == NULL)
interrupt->Idle();
scheduler->Run(nextThread);
}