TTIT61: Process Programming and Operating Systems Alexandru Andrei

advertisement
Introduction
TTIT61:
Process Programming and
Operating Systems
ƒ Nachos
Alexandru Andrei
alean@ida.liu.se
phone: 282698, room: B 3D:439
TDDB63 Lab lesson
-2-
Laboratory Sessions
Laboratory Sessions
ƒ Marking changes
ƒ
ƒ
ƒ
ƒ
ƒ
SU rooms
Solaris
C/C++
Tools: emacs(or another editor), make(gmake), ddd, man
Mark your changes in the source code
-3-
// -----------------------------------------------// 07-01-24: your_name: changed
...
// end of changes
// ------------------------------------------------
TDDB63 Lab lesson
-4-
Laboratory Sessions
ƒ File header
Laboratory Sessions
ƒ Function header
// ==========================================
// TTIT61: Lab 1: solution.cpp
// -----------------------------------------------// Group XYZ:
// -----------------------------------------------// This module extends Nachos to be able to work
// with semaphores.
// -----------------------------------------------// 07-02-01: XYZ: created functin F
// 07-02-12: XYZ: semSignal: changed signal type
//
===========================================
-5-
TDDB63 - Lab lesson 1
TDDB63 Lab lesson
TDDB63 Lab lesson
// -----------------------------------------------// int readInput(int file)
// -----------------------------------------------// This function reads continuously from the
// file and returns the number of bytes read
// -----------------------------------------------// Parameters:
// file: handle of the file to read from
// ------------------------------------------------
-6-
TDDB63 Lab lesson
1
Tips
ƒ Read documentation and source code
ƒ Nachos Beginner’s Guide (html)
http://www.ida.liu.se/~TDDB72/material/begguide/
ƒ A Roadmap Through Nachos (pdf)
http://www.ida.liu.se/~TTIT61/labs/nachosroad.pdf
ƒ Use a debugger (gdb / ddd)
ƒ Use code browsing tools (emacs, grep)
ƒ Use different test cases
ƒ Check the homepage regularly
Nachos Overview
ƒ “Not Another Completely Heuristic Operating System”
ƒ Free simulation environment to study an operating system
ƒ Runs as a single Unix process (real operating systems run on
bare machines)
ƒ Simulates low-level facilities (including interrupts, virtual
memory and interrupt-driven device I/O)
ƒ Implemented in subset of C++, ca. 2.500 lines of code
ƒ See homepage material and links
ƒ Browse the source code !!!!!!!
TDDB63 Lab lesson
-7-
TDDB63 Lab lesson
-8-
Overview
code/machine
code/threads
code/userprog
OS KERNEL
Given
TDDB63 Lab lesson
-9-
Lab. 1,2,3
-10-
Machine Object
ƒ Available through variable machine
ƒ Public attributes:
ƒ registers
ƒ mainMemory
ƒ virtualMemory
(single linear page and software-managed TLB)
ƒ Methods: Machine (constructor), Translate (translates virtual
addresses), OneInstruction, Run, ReadRegister, WriteRegister,
ReadMem, WriteMem
=> Read documentation and source code (code/machine directory)
-11-
TDDB63 - Lab lesson 1
TDDB63 Lab lesson
code/test
Test programs
ƒ Nachos simulates (emulates ?) a machine approximating a
real computer architecture (MIPS)
ƒ Has:
ƒ Registers, Memory, CPU, interrupts
ƒ Event-driven simulated clock
ƒ Can execute arbitrary programs
Nachos
TDDB63 Lab lesson
Interrupt Management
ƒ Nachos simulates interrupts with an event queue and a clock
(clock ticks -> queue is examined for pending events)
ƒ The clock ticks, when ...
ƒ Interrupts are restored
(used often for mutual exclusion purposes)
ƒ One instruction is executed
ƒ “Ready list” is empty
ƒ Object Interrupt:
ƒ Main methods: Schedule (schedule event), SetLevel
(disable / enable), OneTick
=> Read documentation / source code
-12-
TDDB63 Lab lesson
2
Real-Time Clock Interrupts
ƒ Object Timer:
ƒ Simulates a real-time clock
(generates interrupts at regular intervals)
ƒ Methods: Timer (creates a real-time clock, can be randomly
influenced)
ƒ ./nachos –rs #number (nachos –rs 100)
-13-
TDDB63 Lab lesson
Address Translation
ƒ Nachos supports two virtual memory architectures:
ƒ Linear page tables
(easier to program)
ƒ Software-managed TLB
(closer to what current machines support)
ƒ Only one at a time
ƒ Linear page tables:
ƒ Virtual address: page number and page offset
ƒ Physical address: machine->mainMemory + n * PageSize + m
ƒ Read documentation / source code (code/userprog/addrspace.h)
-14-
Terminal Console Device
ƒ Accessed through low-level primitives (initiated by I/O
operation, performed by an “operation complete” interrupt)
ƒ Object Console
ƒ Simulates behavior of a character-oriented CRT device
(one character at a time)
ƒ Methods: Console (constructor, connects to files / streams),
PutChar, GetChar, CheckCharAvail
-15-
TDDB63 Lab lesson
Disk Device
ƒ Accessed through low-level primitives
(initiated by I/O operation, performed by an “operation
complete” interrupt)
ƒ Object Disk
ƒ Simulates behavior of a real disk
(single platter, multiple tracks containing sectors,
blocks uniquely identified by their sector number)
ƒ Methods: Disk (constructor), ReadRequest (read single
sector), WriteRequest (write single sector),
ComputeLatency (used to decide when to schedule an “I/O
complete” interrupt when servicing a read or write request)
-16-
Thread
1
Thread
2
Thread
3
OS KERNEL
-17-
TDDB63 - Lab lesson 1
Process
1
Process
2
Process
3
TDDB63 Lab lesson
Lab1: Threads
Nachos Processes and Threads Overview
ƒ Process: a user program in execution
ƒ Thread (Nachos): the kernel correspondent of a user process
TDDB63 Lab lesson
ƒ Nachos threads are in one of four states
ƒ READY
ƒ RUNNING
ƒ BLOCKED
ƒ JUST_CREATED (temporary state)
ƒ The scheduler decides which thread to run next
ƒ Synchronization facilities are provided through semaphores
Test
Testprograms
programs
TDDB63 Lab lesson
-18-
TDDB63 Lab lesson
3
Threads and Scheduling
ƒ Scheduler decides which thread to run next
(invoked whenever the current thread gives up the CPU or an
interrupt arrives)
ƒ Ready list (threads which are ready to run)
ƒ Nachos: unprioritized, round-robin fashion
ƒ Object Scheduler methods:
ƒ ReadyToRun(thread_object) -place the thread in ready list
ƒ FindNextToRun
ƒ Run
-19-
Thread switching
ƒ
ƒ
ƒ
ƒ
Suspend current thread
Save thread state (registers)
Restore state of of thread to switch to
=> function Switch(oldThread, nextThread)
(written in assembly language)
TDDB63 Lab lesson
TDDB63 Lab lesson
-20-
Thread Object
ƒ Thread Object methods:
ƒ Thread (constructor)
ƒ Fork (making thread executable)
ƒ StackAllocate
ƒ Yield (suspend and select next waiting one)
ƒ Sleep (suspend)
ƒ Finish (terminate)
ƒ Read documentation / source code
(threads/thread.h, threads/thread.cc, threadsthreadtest.cc)
-21-
Nachos Threads
void ThreadFunction1(int which);
void ThreadFunction2(int which);
void ThreadTest() {
Thread *t1 = new Thread(“first thread");
Thread *t2 = new Thread(“second thread");
t1->Fork(ThreadFunction1, 1);
t2->Fork(ThreadFunction2, 2);
ThreadFunction(1);
}
TDDB63 Lab lesson
-22-
Lab1: Semaphores
Synchronization and Mutual Exclusion
ƒ Mutual exclusion achieved by disabling / enabling interrupts
ƒ Synchronization provided through semaphores
ƒ Object Semaphore methods:
ƒ Semaphore (constructor)
ƒ P (decrement semaphore’s counter,
blocking caller if 0)
ƒ V (increment semaphore’s counter,
releasing one thread if any are blocked)
-23-
TDDB63 - Lab lesson 1
TDDB63 Lab lesson
TDDB63 Lab lesson
ƒ A semaphore is an integer variable, accessible only by the
operations P and V (Claim, and Release)
Wait(S): while S <= 0 do no-op;
S := S -1 ;
Signal(S): S := S + 1 ;
-24-
TDDB63 Lab lesson
4
Locks
Condition Variables
ƒ Semaphores and Locks are not very efficient when dealing
with multiple resources
P1
Lock1->Acquire();
Lock2->Acquire();
…
Lock2->Release;
Lock1->Release();
ƒ A binary semaphore
ƒ Checks that only the thread that acquired the lock can release it
ƒ Use the Semaphore class + additional stuff
P2
Lock3->Acquire();
Lock1->Acquire();
…
Lock1->Release;
Lock3->Release();
P3
Lock2->Acquire();
Lock3->Acquire();
…
Lock3->Release();
Lock2->Release();
Why hold a lock if you can not use it ?
TDDB63 Lab lesson
-25-
-26-
Condition Variables (Wait)
ƒ Wait
ƒ Release the lock
ƒ Add the thread to a queue of threads waiting on this condition
ƒ Go to sleep
ƒ Acquire the lock
Condition Variables (Signal)
ƒ Signal
ƒ Take one thread from the list
ƒ Run it (scheduler->ReadyToRun(that thread))
ƒ Broadcast
ƒ Wake up all the threads and let them compete for the lock
TDDB63 Lab lesson
-27-
-28-
Readers&Writers
-29-
TDDB63 - Lab lesson 1
TDDB63 Lab lesson
TDDB63 Lab lesson
Readers&Writers
ƒ
ƒ
ƒ
ƒ
ƒ
ƒ Given a buffer
ƒ Two kinds of processes can access that buffer:
ƒ Readers
ƒ Writers
ƒ Correct operation:
ƒ several readers can read simultaneously
ƒ Only one writer is allowed to access the file
TDDB63 Lab lesson
int counter_for_read;
Lock *lock_for_counter;
Lock *lock_for_readers_writers;
Condition *c_counter;
Condition *c_write;
-30-
TDDB63 Lab lesson
5
Lab 1: Preparation
ƒ Read and understand the partial thread system (nachos3.4/code/threads)
ƒ Run the test program in the debugger and follow the state of
each thread object
ƒ Compile in the code/threads directory
-31-
Lab1: Assignment 1
ƒ Implement locks and conditions - when used together provides
the same functionality as monitors
ƒ Write code for the Lock and Condition classes (in the file
../code/threads/sync.cc)
ƒ Interfaces (class definition) for Lock and Condition are
already defined (in ../code/threads/sync.h)
ƒ No changes to the public interfaces should be done (you
can add private variables)
TDDB63 Lab lesson
-32-
Lab1: Assignment 2
ƒ Test your implementation
ƒ Write a class BoundedBuffer (in the file
code/threads/threadtest.cc) and use it for producer/consumer
communication.
(Silberschatz, p. 172)
ƒ Use for testing the file (replace your threadtest.cc with it):
/home/TTIT61/www-pub/labs/threadtest.cc
ƒ Don’t forget border conditions: empty buffer, full buffer
ƒ Try several producers and/or consumers
-33-
TDDB63 - Lab lesson 1
TDDB63 Lab lesson
TDDB63 Lab lesson
BoundedBuffer
class BoundedBuffer {
public:
BoundedBuffer(int bufsize);
~BoundedBuffer();
int Read();
void Write(int value);
private:
// some variables you need
};
-34-
TDDB63 Lab lesson
6
Download