DOC PREVIEW
U of I CS 241 - Threads

This preview shows page 1-2-14-15-29-30 out of 30 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 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 30 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 30 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 30 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 30 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 30 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 Nahrstedt2OverviewWhat is a thread?Examples of using threadsThread implementationsSummary01/13/19 CS 241 - System Programming, Klara Nahrstedt3AdministrativeRead T: Chapter 2.1Read: R&R: 1201/13/19 CS 241 - System Programming, Klara Nahrstedt4Process ReviewSo What Is A Process?–It’s one executing instance of a “program”–It’s separate from other instances–It can start (“launch”) other processesWhat’s in a process?–Code (text), data, stack, heap–Process control block (PCB)Process state, priority, accountingProgram counter, register variables, stack pointers, etcOpen 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 spaceThread shares–Code section–Data section–OS resources such as open files, signals belonging to the task01/13/19 CS 241 - System Programming, Klara Nahrstedt6ThreadsSystem –Light-weighted threads switch between threads but not between virtual memories–Threads allow user programs to continue after starting I/O–Threads allow parallel processingUser–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 ModelThreads in the same process share resourcesEach thread execute separately01/13/19 CS 241 - System Programming, Klara Nahrstedt9Thread Model: Stack01/13/19 CS 241 - System Programming, Klara Nahrstedt10Thread Model : Context SwitchExtensive sharing makes CPU switching among peer threads and creation of threads inexpensive compared to processesThread 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–TerminatedThreads share CPU and on single processor machine only one thread can run at a timeThread management can create child threads which can block waiting for a system call to be completedNo 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 Nahrstedt13QuestionsWhat 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 ExampleProcess –CS241 MP –Different from CS 225’s MPThread –CS241 MP1, MP2, MP3, MP4, MP5–Each is different –ShareTextbookPersonnel (TAs, instructors)–Affect each other01/13/19 CS 241 - System Programming, Klara Nahrstedt15Thread Usage: word processorWhat 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 ServerRough 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 ThreadsResponsiveness –Multi-threading allows applications to run even if part of it is blockedResource sharing–Sharing of memory, files and other resources of the process to which the threads belongEconomy–Much more costly and time consuming to create and manage processes than threadsUtilization 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 ThreadsAdvantages–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 kernelOne 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 ThreadsDisadvantages–Blocking If kernel is single threaded, then any user-level thread can block the entire task executing a single system call–No ProtectionThere 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

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

Threads

Threads

28 pages

LECTURE

LECTURE

45 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?