DOC PREVIEW
U of I CS 241 - Threads

This preview shows page 1-2-3-26-27-28 out of 28 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

ThreadsAdministrativeProcess ReviewWhy Threads?Threads: Lightweight ProcessesReal Life Example?What does a Thread Comprise?Thread vs. ProcessThread Model: StackThread Model : Context SwitchThread StateExample programQuestionsThread Usage: word processorThread Usage: Web ServerWeb ServerTradeoffsBenefits of ThreadsThread Creation vs. Process CreationDesigning Threaded ProgramsTasks Suitable for threadingCommon Multi-thread Software ArchitecturesThread PackagesImplementing Threads in User Space (old Linux)User-level ThreadsA Challenge: Making Single-Threaded Code MultithreadedA solution: Private Global VariablesSummaryCS 241 Spring 2007System Programming 01/13/19 CS241 © 2006-2007 LA,RHC and YZ, All Rights Reserved1ThreadsLawrence AngraveLecture 82AdministrativeThis week SMP2 Self assessment quiz for this weekReading assignment:TextbookR & R Chapter 2.2 & Chapter12Stallings Chapter 4.1Threading tutorialhttp://www.llnl.gov/computing/tutorials/pthreads/#WhyPthreads3Process ReviewSo What Is A Process?It’s one executing instance of a “program”It’s separate from other instancesIt can start (“launch”) children processesWhat’s in a process?Code (text), data, stack, heapProcess state: new, block, running, ready, terminated4Why Threads? Processes do not share resources very wellWhy?Process context switching cost is very highWhy?Thread: a light-weighted processA sequence of execution5Threads: Lightweight Processes(a) Three processes each with one thread(b) One process with three threadsEnvironment (resource)execution6Real Life Example?Process CS241Different from CS 225’sThread CS241 MP, Reading, Self-assessment quizEach is a different “execution”But all shareContentTextbookPersonnel (TAs, instructors)Affect each other7What does a Thread Comprise?Necessary information to have its own execution streamThread IDProgram counterRegister setStack spaceSignal mask (later…)Threads in the same process shareCode sectionData sectionOS resources such as open files, signals belonging to the task8Thread vs. ProcessEach thread execute separatelyThreads in the same process share resourcesNo protection among threads!!9Thread Model: Stack10Thread Model : Context SwitchExtensive sharing makes CPU switching among peer threads and creation of threads inexpensive compared to processesThread context switch still requiresRegister set switchBut no memory management related work!!!Why need to switch from one thread (process) to another?Some thread (process) may block for I/OMany threads (processes) share limited #CPUs11Thread State Threads states are ReadyBlockedRunningTerminatedWhy these states?Threads share CPUOn single processor machine only one thread can run at a timeThreads can block waiting for a system call to be completed12Example 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; }13QuestionsWhat are the similarities between processes and threads? What are the differences between processes and threads?Why threads are used in many real-world software?ServersDesktop appsMicrosoft outlook use >32 threads14Thread Usage: word processorWhat if it is single-threaded?15Thread Usage: Web Server16Web ServerRough outline of code for previous slide(a) Dispatcher thread(b) Worker thread17TradeoffsFour ways to construct a serverFourth?18Benefits of ThreadsResponsiveness Multi-threading allows applications to run even if part of it is blockedResource sharingSharing of memory, files and other resources of the process to which the threads belongEconomyMuch more costly and time consuming to create and manage processes than threadsUtilization of multiprocessor architectures Each thread can run in parallel on a different processor19Thread Creation vs. Process CreationTime in seconds for 50000 fork or thread creations20Designing Threaded ProgramsThread candidates?Discrete, independent tasks which can execute concurrentlyE.g. if routine1 and routine2 can be interchanged, interleaved and/or overlapped in real time, they are candidates for threading21Tasks Suitable for threadingBlock for potentially long waits Use many CPU cycles Must respond to asynchronous events Are of lesser or greater importance than other tasks Are able to be performed in parallel with other tasks22Common Multi-thread Software ArchitecturesManager/workera single thread, the manager assigns work to other threads, the workers. Typically, the manager handles all input and parcels out work to the other tasks Pipeline:a task is broken into a series of suboperations, each of which is handled in series, but concurrently, by a different thread. An automobile assembly line best describes this model Peersimilar to the manager/worker model, but after the main thread creates other threads, it participates in the work.23Thread PackagesKernel thread packages Implemented and supported at kernel levelUser-level thread packagesImplemented at user level24Implementing Threads in User Space (old Linux)A user-level threads package25User-level ThreadsAdvantagesFast 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 Scheduling26A Challenge: Making Single-Threaded Code Multithreaded Conflicts between threads over the use of a global variable27A solution: Private Global Variables28SummaryWhat is thread? Why do we need threads?Difference between process and threadThread Implementations and their tradeoffsUser-levelKernel-levelHybridNext lecture:


View Full Document

U of I CS 241 - Threads

Documents in this Course
Process

Process

28 pages

Files

Files

37 pages

File I/O

File I/O

52 pages

C Basics

C Basics

69 pages

Memory

Memory

23 pages

Threads

Threads

14 pages

Lecture

Lecture

55 pages

C Basics

C Basics

24 pages

Signals

Signals

27 pages

Memory

Memory

45 pages

Threads

Threads

47 pages

LECTURE

LECTURE

45 pages

Threads

Threads

30 pages

Threads

Threads

55 pages

Files

Files

37 pages

SIGNALS

SIGNALS

22 pages

Files

Files

37 pages

Threads

Threads

14 pages

Threads

Threads

13 pages

Load more
Download Threads
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Threads and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Threads 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?