DOC PREVIEW
U of I CS 241 - Lecture notes

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:

CS 241 Spring 2007System Programming2/2/07 CS241 © 2006-2007 LA,RHC andYZ, All Rights Reserved1ThreadsLawrence AngraveLecture 82AdministrativeThis weekSMP2Self assessment quiz for this weekLecture quizReading 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?ProcessCS241Different from CS 225’sThreadCS241 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 executionstreamThread 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 amongpeer threads and creation of threadsinexpensive compared to processesThread context switch still requiresRegister set switchBut no memory management related work!!!Why need to switch from one thread (process) toanother?Some thread (process) may block for I/OMany threads (processes) share limited #CPUs11Thread StateThreads states areReadyBlockedRunningTerminatedWhy 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 andthreads?What are the differences between processes andthreads?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 ThreadsResponsivenessMulti-threading allows applications to run even ifpart of it is blockedResource sharingSharing of memory, files and other resources of theprocess to which the threads belongEconomyMuch more costly and time consuming to createand manage processes than threadsUtilization of multiprocessor architecturesEach thread can run in parallel on a differentprocessor19Thread Creation vs. Process CreationTime in seconds for 50000 fork orthread creations20Designing Threaded ProgramsThread candidates?Discrete, independent tasks which can execute concurrentlyE.g. if routine1 and routine2 can be interchanged, interleavedand/or overlapped in real time, they are candidates for threading21Tasks Suitable for threadingBlock for potentially long waitsUse many CPU cyclesMust respond to asynchronous eventsAre of lesser or greater importance than othertasksAre able to be performed in parallel with othertasks22Common Multi-thread Software ArchitecturesManager/workera single thread, the manager assigns work to other threads, theworkers. Typically, the manager handles all input and parcelsout work to the other tasksPipeline:a task is broken into a series of suboperations, each of which ishandled in series, but concurrently, by a different thread. Anautomobile assembly line best describes this modelPeersimilar to the manager/worker model, but after the main threadcreates other threads, it participates in the work.23Thread PackagesKernel thread packagesImplemented 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 interruptsto kernelOne key difference with processes: when a thread is finishedrunning for the moment, it can call thread_yield. Thisinstruction (a) saves the thread information in the thread tableitself, and (b) calls the thread scheduler to pick another threadto run.The procedure that saves the local thread state and the schedulerare local procedures, hence no trap to kernel, no context switch,no memory switch, and this makes the thread scheduling veryfast.Customized Scheduling26A Challenge: Making Single-Threaded Code MultithreadedConflicts 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 - Lecture notes

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

Threads

Threads

28 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 Lecture notes
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 Lecture notes 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 Lecture notes 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?