DOC PREVIEW
LSU CSC 4103 - Threads

This preview shows page 1-2 out of 7 pages.

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

Unformatted text preview:

1CSC 4103 - Operating SystemsFall 2009Tevfik Ko!arLouisiana State UniversitySeptember 8th, 2009Lecture - IVThreads2Roadmap• Threads– Why do we need them?– Threads vs Processes– Threading Examples– Threading Implementation & Multi-threading Models– Other Threading Issues• Thread cancellation• Signal handling• Thread pools• Thread specific data3Concurrent Programming• In certain cases, a single application may need to run several tasks at the same time 1123245345sequentialconcurrent4Motivation• Increase the performance by running more than one tasks at a time.– divide the program to n smaller pieces, and run it n times faster using n processors• To cope with independent physical devices.– do not wait for a blocked device, perform other operations at the backgroundSerial vs Parallel5QPleaseCOUNTERCOUNTER 1COUNTER 26Divide and Computex1 + x2 + x3 + x4 + x5 + x6 + x7 + x8How many operations with sequential programming?7Step 1: x1 + x2Step 2: x1 + x2 + x3Step 3: x1 + x2 + x3 + x4 Step 4: x1 + x2 + x3 + x4 + x5Step 5: x1 + x2 + x3 + x4 + x5 + x6Step 6: x1 + x2 + x3 + x4 + x5 + x6 + x7 Step 7: x1 + x2 + x3 + x4 + x5 + x6 + x7 + x87Divide and Computex1 + x2 + x3 + x4 + x5 + x6 + x7 + x8Step 1: parallelism = 4Step 2: parallelism = 2Step 3: parallelism = 18Gain from parallelismIn theory:• dividing a program into n smaller parts and running on n processors results in n time speedupIn practice:• This is not true, due to– Communication costs– Dependencies between different program parts• Eg. the addition example can run only in log(n) time not 1/n9Concurrent Programming• Implementation of concurrent tasks:– as separate programs– as a set of processes or threads created by a single program• Execution of concurrent tasks:– on a single processor (can be multiple cores)! Multithreaded programming– on several processors in close proximity! Parallel computing– on several processors distributed across a network! Distributed computing10Why Threads?• In certain cases, a single application may need to run several tasks at the same time– Creating a new process for each task is time consuming– Use a single process with multiple threads• faster• less overhead for creation, switching, and termination• share the same address spaceOwnership vs Execution11"A process embodies two independent concepts:1. resource ownership2. execution & scheduling1. Resource ownership#a process is allocated address space to hold the image, and is granted control of I/O devices and files#the O/S prevents interference among processes while they make use of resources (multiplexing)2. Execution & scheduling #a process follows an execution path through a program --> Thread#it has an execution state and is scheduled for dispatchingMulti-threading12"The execution part is a “thread”Pasta for six– boil 1 quart salty water – stir in the pasta– cook on medium until “al dente”– serveProgramProcessCPUinput datathread of execution"The execution part is a “thread” that can be multipliedother threadsame CPU workingon two things13Single and Multithreaded ProcessesNew Process Description Model14"Multithreading requires changes in the process description modelstackprocess control block (PCB)programcodedata#each thread of execution receives its own control block and stack$own execution state (“Running”, “Blocked”, etc.)$own copy of CPU registers$own execution history (stack)#the process keeps a global control block listing resources currently usedprocess control block (PCB)programcodedatathread 1 stackthread 1 control block (TCB 1)thread 2 stackthread 2 control block (TCB 2)New process imagePer-process vs per-thread items15"Per-process items and per-thread items in the control block structures#process identification data$numeric identifiers of the process, the parent process, the user, etc.#CPU state information$user-visible, control & status registers$stack pointers#process control information$scheduling: state, priority, awaited event$used memory and I/O, opened files, etc.$pointer to next PCB#process identification data + thread identifiers$numeric identifiers of the process, the parent process, the user, etc.#CPU state information$user-visible, control & status registers$stack pointers#process control information$scheduling: state, priority, awaited event$used memory and I/O, opened files, etc.$pointer to next PCB16Multi-process modelProcess Spawning:Process creation involves the following four main actions:• setting up the process control block, • allocation of an address space and• loading the program into the allocated address space and • passing on the process control block to the scheduler17Multi-thread modelThread Spawning:• Threads are created within and belonging to processes• All the threads created within one process share the resources of the process including the address space• Scheduling is performed on a per-thread basis. • The thread model is a finer grain scheduling model than the process model• Threads have a similar lifecycle as the processes and will be managed mainly in the same way as processes are18Threads vs Processes• A common terminology:– Heavyweight Process = Process– Lightweight Process = ThreadAdvantages (Thread vs. Process):• Much quicker to create a thread than a process– spawning a new thread only involves allocating a new stack and a new CPU state block • Much quicker to switch between threads than to switch between processes• Threads share data easilyDisadvantages (Thread vs. Process):• Processes are more flexible– They don’t have to run on the same processor• No security between threads: One thread can stomp on another thread's data• For threads which are supported by user thread package instead of the kernel:– If one thread blocks, all threads in task block.Thread Creation•pthread_create// creates a new thread executing start_routineint pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg);•pthread_join// suspends execution of the calling thread until the target// thread terminatesint pthread_join(pthread_t thread, void **value_ptr);19Thread Exampleint main(){ pthread_t thread1, thread2; /* thread variables */ pthread_create (&thread1, NULL, (void *) &print_message_function, (void*)”hello “);


View Full Document

LSU CSC 4103 - Threads

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?