Threads and ConcurrencyThreadThread requirementsExamples of thread usageThreads and ProcessesThreads and ProcessesThread OperationsNachos Threads (Section 3, .. RoadMap)Nachos Thread Description and ControlNachos Thread ClassThread Control and SchedulingNachos Scheduler Class2/6/02 B.Ramamurthy 1Threads and ConcurrencyB.Ramamurthy2/6/02 B.Ramamurthy 2ThreadUnit of workA process has address space, registers, PC and stack (See Section 3, page 9.. in A Roadmap through Nachos for the detailed list)A thread has registers, program counter and stack, but the address space is shared with process that started it. n This means that a user level thread could be invoked without assistance from the OS. This low overhead is one of the main advantages of threads. n If a thread of a process is blocked, the process could go on.n Concurrency: Many threads could be operating concurrently, on a multi threaded kernel.n User level scheduling is simplified and realistic (bound, unbound, set concurrency, priorities etc.)n Communication among the threads is easy and can be carried out without OS intervention.2/6/02 B.Ramamurthy 3Thread requirementsAn execution stateIndependent PC working within the same process.An execution stack.Per-thread static storage for local variables.Access to memory and resources of the creator-process shared with all other threads in the task.Key benefits: less time to create than creating a new process, less time to switch, less time to terminate, more intuitive for implementing concurrency if the application is a collection of execution units.2/6/02 B.Ramamurthy 4Examples of thread usageForeground and background work: Consider spreadsheet program: one thread could display menu and get response while another could be processing the request. Increases the perceived speed of the application.Asynchronous processing: Periodic backup (auto-saving) of RAM into disk. A thread could schedule itself to come-alive every 1 minute or so to do this saving concurrently with main processing.Speed execution: In hard-core data-processing simple concurrency can speed up process.Transaction processing: Many independent transactions can be modeled very nicely using threads. Such applications as neural networks, searches, graphics, agent/actor model suit well for thread-implementation.2/6/02 B.Ramamurthy 5Threads and ProcessesA thread is a unit of work to a CPU. It is strand of control flow.A traditional UNIX process has a single thread that has sole possession of the process’s memory and resources.Threads within a process are scheduled and execute independently.Many threads may share the same address space.Each thread has its own private attributes: stack, program counter and register context.2/6/02 B.Ramamurthy 6Threads and Processes2/6/02 B.Ramamurthy 7Thread OperationsBasic Operations associated with a thread are:Spawn : newly created into the ready stateBlock : waiting for an eventUnblock : moved into ready from blockedFinish : exit after normal or abnormal termination.2/6/02 B.Ramamurthy 8Nachos Threads (Section 3, .. RoadMap)Nachos process has an address space, a single thread of control, and other objects such as open file descriptors.Global variables are shared among threads. Ex: buffer of a mailbox you use in Lab1.Nachos “scheduler” maintains a data structure called ready list which keeps track of threads that are ready to execute.Each thread has an associated state: READY, RUNNING, BLOCKED, JUST_CREATEDGlobal variable currentThread always points to the currently running thread.2/6/02 B.Ramamurthy 9Nachos Thread Description and ControlThread specific data: local data, stack and registers such as PC, SP.Control:n Thread creation (Ex: fork)n Thread schedule (Ex: yield)n Thread synchronization (Ex: using barrier)n Code for execution (Ex: fork’s function parameter)Nachos Thread ClassThread//operationsThread *Thread(char *name);Thread *Thread(char *name, int priority);Fork (VoidFunctioNPtr func, int arg);void Yield(); // Scheduler::FindNextToRun()void Sleep(); // change state to BLOCKEDvoid Finish(); // cleanupvoid CheckOverflow();// stack overflowvoid setStatus(ThreadStatus st); //ready, running..// blockedchar* getName();void Print(); //print name//dataint*stackTop;int machineState[MachineStateSize];//registersint* stack;ThreadStatus status;char* name;2/6/02 B.Ramamurthy 11Thread Control and SchedulingSwitch (oldThread, newThread); // assembly language routineThreads that are ready kept in a ready list.The scheduler decides which thread to run next.Nachos Scheduling policy is: FIFO.2/6/02 B.Ramamurthy 12Nachos Scheduler ClassSchedulerScheduler();~Scheduler();void ReadyTnRun(Thread* thread);Thread* FindNextToRun();void Run(Thread* nextThread);void Print();List*
View Full Document