DOC PREVIEW
U of I CS 241 - System Programming 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 ProgrammingThreadsOverviewAdministrativeProcess 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 ModelsMulti-threading ModelsA Challenge: Making Single-Threaded Code MultithreadedA solution: Private Global VariablesSummaryCS241 System ProgrammingThreadsKlara NahrstedtLecture 51/30/20061/31/2006CS 241 - System Programming, Klara Nahrstedt2Overviewz What is a thread?z Examples of using threadsz Thread implementationsz Summary1/31/2006CS 241 - System Programming, Klara Nahrstedt3AdministrativezRead T: Chapter 2.1z Read: R&R: 121/31/2006CS 241 - System Programming, Klara Nahrstedt4Process ReviewzSo What Is A Process?–It’s one executing instance of a “program”–It’s separate from other instances–It can start (“launch”) other processeszWhat’s in a process?–Code (text), data, stack, heap–Process control block (PCB)zProcess state, priority, accountingzProgram counter, register variables, stack pointers, etczOpen files and devices1/31/2006CS 241 - System Programming, Klara Nahrstedt5Threads zProcesses do not share resources very well, therefore context switching cost is very high. zThis leads to ‘threads’ which share some of the resources–Thread is a light-weighted process and it is the basic unit of CPU utilization. zThread comprises–Thread ID–Program counter–Register set–Stack spacezThread shares–Code section–Data section–OS resources such as open files, signals belonging to the task1/31/2006CS 241 - System Programming, Klara Nahrstedt6ThreadszSystem –Light-weighted threads switch between threads but not between virtual memories–Threads allow user programs to continue after starting I/O–Threads allow parallel processingzUser–Threads may reduce context switching times by eliminating kernel overhead–Thread management allows user scheduling of threads1/31/2006CS 241 - System Programming, Klara Nahrstedt7Threads: Lightweight ProcessesEnvironment (resource)execution(a) Three processes each with one thread(b) One process with three threads1/31/2006CS 241 - System Programming, Klara Nahrstedt8Thread ModelzThreads in the same process share resourcesz Each thread execute separately1/31/2006CS 241 - System Programming, Klara Nahrstedt9Thread Model: Stack1/31/2006CS 241 - System Programming, Klara Nahrstedt10Thread Model : Context SwitchzExtensive sharing makes CPU switching among peer threads and creation of threads inexpensive compared to processesz Thread context switch still requires–Register set switch–But no memory management related work!!!1/31/2006CS 241 - System Programming, Klara Nahrstedt11Thread Model: State zThreads states are –Ready–Blocked–Running–TerminatedzThreads share CPU and on single processor machine only one thread can run at a timez Thread management can create child threads which can block waiting for a system call to be completedz No protection among threads!!1/31/2006CS 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; }1/31/2006CS 241 - System Programming, Klara Nahrstedt13QuestionszWhat are the similarities between processes and threads? z What are the differences between processes and threads?1/31/2006CS 241 - System Programming, Klara Nahrstedt14Real Life ExamplezProcess –CS241 MP –Different from CS 225’s MPzThread –CS241 MP1, MP2, MP3, MP4, MP5–Each is different –SharezTextbookzPersonnel (TAs, instructors)–Affect each other1/31/2006CS 241 - System Programming, Klara Nahrstedt15Thread Usage: word processorzWhat if it is single-threaded?1/31/2006CS 241 - System Programming, Klara Nahrstedt16Thread Usage: Web Server1/31/2006CS 241 - System Programming, Klara Nahrstedt17Web ServerzRough outline of code for previous slide(a) Dispatcher thread(b) Worker thread1/31/2006CS 241 - System Programming, Klara Nahrstedt18TradeoffsThree ways to construct a server1/31/2006CS 241 - System Programming, Klara Nahrstedt19Benefits of ThreadszResponsiveness –Multi-threading allows applications to run even if part of it is blockedzResource sharing–Sharing of memory, files and other resources of the process to which the threads belongzEconomy–Much more costly and time consuming to create and manage processes than threadszUtilization of multiprocessor architectures –Each thread can run in parallel on a different processor1/31/2006CS 241 - System Programming, Klara Nahrstedt20Implementing Threads in User Space(old Linux)A user-level threads package1/31/2006CS 241 - System Programming, Klara Nahrstedt21User-level ThreadszAdvantages–Fast Context Switching: zUser level threads are implemented using user level thread libraries, rather than system calls, hence no call to OS and no interrupts to kernelzOne 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 tableitself, and (b) calls the thread scheduler to pick another thread to run.zThe 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 Scheduling1/31/2006CS 241 - System Programming, Klara Nahrstedt22User level ThreadszDisadvantages–Blocking zIf kernel is single threaded, then any user-level thread can block the entire task executing a single system call–No ProtectionzThere is no protection between threads, since the threads share memory space1/31/2006CS 241 - System Programming, Klara Nahrstedt23Implementing Threads in the Kernel (Windows 2000/XP)A threads package managed by the kernel1/31/2006CS 241 - System


View Full Document

U of I CS 241 - System Programming 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

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 System Programming 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 System Programming 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 System Programming 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?