CS241 System Programming ThreadsOverviewAdministrativeProcess ReviewThreadsThreadsThreads: Lightweight ProcessesThread ModelThread Model: StackThread Model : Context SwitchThread Model: StateExample programQuestionsReal Life ExampleThread Usage: word processorThread Usage: Web ServerWeb ServerTradeoffsBenefits of ThreadsImplementing Threads in User Space (old Linux)User-level ThreadsUser level ThreadsImplementing Threads in the Kernel (Windows 2000/XP)Hybrid Implementations (Solaris)Kernel Threads (Linux)Multi-threading ModelsSlide 27A Challenge: Making Single-Threaded Code MultithreadedA solution: Private Global VariablesSummaryCS241 System ProgrammingThreadsKlara NahrstedtLecture 51/30/200601/13/19 CS 241 - System Programming, Klara Nahrstedt2OverviewWhat is a thread?Examples of using threadsThread implementationsSummary01/13/19 CS 241 - System Programming, Klara Nahrstedt3AdministrativeRead T: Chapter 2.1Read: R&R: 1201/13/19 CS 241 - System Programming, Klara Nahrstedt4Process ReviewSo What Is A Process?–It’s one executing instance of a “program”–It’s separate from other instances–It can start (“launch”) other processesWhat’s in a process?–Code (text), data, stack, heap–Process control block (PCB)Process state, priority, accountingProgram counter, register variables, stack pointers, etcOpen files and devices01/13/19 CS 241 - System Programming, Klara Nahrstedt5Threads Processes do not share resources very well, therefore context switching cost is very high. This leads to ‘threads’ which share some of the resources–Thread is a light-weighted process and it is the basic unit of CPU utilization. Thread comprises–Thread ID–Program counter–Register set–Stack spaceThread shares–Code section–Data section–OS resources such as open files, signals belonging to the task01/13/19 CS 241 - System Programming, Klara Nahrstedt6ThreadsSystem –Light-weighted threads switch between threads but not between virtual memories–Threads allow user programs to continue after starting I/O–Threads allow parallel processingUser–Threads may reduce context switching times by eliminating kernel overhead–Thread management allows user scheduling of threads01/13/19 CS 241 - System Programming, Klara Nahrstedt7Threads: Lightweight Processes(a) Three processes each with one thread(b) One process with three threadsEnvironment (resource)execution01/13/19 CS 241 - System Programming, Klara Nahrstedt8Thread ModelThreads in the same process share resourcesEach thread execute separately01/13/19 CS 241 - System Programming, Klara Nahrstedt9Thread Model: Stack01/13/19 CS 241 - System Programming, Klara Nahrstedt10Thread Model : Context SwitchExtensive sharing makes CPU switching among peer threads and creation of threads inexpensive compared to processesThread context switch still requires–Register set switch–But no memory management related work!!!01/13/19 CS 241 - System Programming, Klara Nahrstedt11Thread Model: State Threads states are –Ready–Blocked–Running–TerminatedThreads share CPU and on single processor machine only one thread can run at a timeThread management can create child threads which can block waiting for a system call to be completedNo protection among threads!!01/13/19 CS 241 - System Programming, Klara Nahrstedt12Example program#include <phtread.h>#include <thread.h>#include <stdio.h>void *threadex(void *); int main() { pthread_t tid; /* stores the new thread ID */ pthread_create(&tid, NULL, threadex, NULL); /*create a new thread*/ pthread_join(tid, NULL); /*main thread waits for other thread to terminate */ return 0; /* main thread exits */ } void *threadex(void *arg) /*thread routine*/ { int i; for (i=0; i<5; i++) fprintf(stderr, `Hello, world! \n''); return NULL; }01/13/19 CS 241 - System Programming, Klara Nahrstedt13QuestionsWhat are the similarities between processes and threads? What are the differences between processes and threads?01/13/19 CS 241 - System Programming, Klara Nahrstedt14Real Life ExampleProcess –CS241 MP –Different from CS 225’s MPThread –CS241 MP1, MP2, MP3, MP4, MP5–Each is different –ShareTextbookPersonnel (TAs, instructors)–Affect each other01/13/19 CS 241 - System Programming, Klara Nahrstedt15Thread Usage: word processorWhat if it is single-threaded?01/13/19 CS 241 - System Programming, Klara Nahrstedt16Thread Usage: Web Server01/13/19 CS 241 - System Programming, Klara Nahrstedt17Web ServerRough outline of code for previous slide(a) Dispatcher thread(b) Worker thread01/13/19 CS 241 - System Programming, Klara Nahrstedt18TradeoffsThree ways to construct a server01/13/19 CS 241 - System Programming, Klara Nahrstedt19Benefits of ThreadsResponsiveness –Multi-threading allows applications to run even if part of it is blockedResource sharing–Sharing of memory, files and other resources of the process to which the threads belongEconomy–Much more costly and time consuming to create and manage processes than threadsUtilization of multiprocessor architectures –Each thread can run in parallel on a different processor01/13/19 CS 241 - System Programming, Klara Nahrstedt20Implementing Threads in User Space (old Linux)A user-level threads package01/13/19 CS 241 - System Programming, Klara Nahrstedt21User-level ThreadsAdvantages–Fast Context Switching: User level threads are implemented using user level thread libraries, rather than system calls, hence no call to OS and no interrupts to kernelOne key difference with processes: when a thread is finished running for the moment, it can call thread_yield. This instruction (a) saves the thread information in the thread table itself, and (b) calls the thread scheduler to pick another thread to run.The procedure that saves the local thread state and the scheduler are local procedures, hence no trap to kernel, no context switch, no memory switch, and this makes the thread scheduling very fast. –Customized Scheduling01/13/19 CS 241 - System Programming, Klara Nahrstedt22User level ThreadsDisadvantages–Blocking If kernel is single threaded, then any user-level thread can block the entire task executing a single system call–No ProtectionThere is no protection between threads, since the threads share memory space01/13/19 CS 241 - System Programming, Klara Nahrstedt23Implementing Threads in the Kernel (Windows
View Full Document